SVG to PNG, resize and recolor

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
j4media
Posts: 4
Joined: 2016-05-18T14:07:59-07:00
Authentication code: 1151

SVG to PNG, resize and recolor

Post by j4media »

Hi All,

I have been searching for days on how to pull this off and I just can't find the answer. Here's what I'm trying to do:

I have a folder with about 50 .svg files. Right now those .svg's are rather small, roughly 32x32. They are also all one color and that color is black. What I want to do is resize them to about 8x their current size, change their color to white with a transparency of 90% and have them output to a .png with the same name they currently have.

I was able to use mogrify to batch convert them to .png and resize them as 240x240 thumbnails, but the quality was horrible. I have no idea how to even attempt the recolor and transparency.

Can someone help me out?

Thanks in advance for your help!

John
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: SVG to PNG, resize and recolor

Post by snibgo »

The solution is probably to use "-density X" before reading the SVG file, where X is a suitable integer, say 600.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: SVG to PNG, resize and recolor

Post by fmw42 »

try adding -density 576 (8*72=576) rather than -resize. Note that 8*32=256 not 240. You can also combine the two. Pick a density that is about 4*72=288 to make the image larger, then resize to 240x240 if that is your desired size. Pick a density that gives adequate quality without enlarging too much so that it is not too slow processing.
j4media
Posts: 4
Joined: 2016-05-18T14:07:59-07:00
Authentication code: 1151

Re: SVG to PNG, resize and recolor

Post by j4media »

Thanks snibgo and fmw42. That was helpful. I was able to scale up the SVG with good quality and remove the background of the image. The only thing i can't figure out is how to change the color of my image (in this case, the SVG is a weather icon that is currently black and I want to change the color to something else, preferably white with a transparency of 0.9). I used the below command which scaled my image, removed the background, but the color of the image did not change. What am I doing wrong?

mogrify -density 576 -background none -colorize 0,0,50 -format png *.svg
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: SVG to PNG, resize and recolor

Post by glennrp »

Instead of "-colorize r,g,b", try "-fill newcolor -opaque oldcolor"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: SVG to PNG, resize and recolor

Post by fmw42 »

glennrp wrote:Instead of "-colorize r,g,b", try "-fill newcolor -opaque oldcolor"
If the existing color is perfectly constant, the above is fine, but if not, then add -fuzz XX% before -fill.
j4media
Posts: 4
Joined: 2016-05-18T14:07:59-07:00
Authentication code: 1151

Re: SVG to PNG, resize and recolor

Post by j4media »

The fill and opaque didn't give me the result I wanted. Since the original was Black, I used -negate and that successfully turned it white. Is still can't figure out how to add transparency to my image. Everything I try turns my entire image fully transparent and I only want about 10% transparency. Here's the command line I used to get to my current state. Where would I put the tranparency code?

mogrify -density 576 -background none -negate -format png *.svg
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: SVG to PNG, resize and recolor

Post by fmw42 »

Perhaps you should post an example svg file so we can see what colors are there. -fill ... -opaque should work if you want to change only one constant color. If there are color variations then add -fuzz. You also did not provide your command that tested -fill ... -opaque. Perhaps you are not using it correctly.
j4media
Posts: 4
Joined: 2016-05-18T14:07:59-07:00
Authentication code: 1151

Re: SVG to PNG, resize and recolor

Post by j4media »

Here's an example svg file:

Image

Here's the command I ran and the result I got from it:

mogrify -density 576 -background none -fuzz 90% -fill white -opaque black -format png *.svg

Image

You will notice that the resulting PNG did turn white, but the white was not 10% transparent.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: SVG to PNG, resize and recolor

Post by fmw42 »

what is the exact black color? Does it contain transparency? If so, then you must include the black transparency in the color and the desired white transparency. For example 10% black transparency (90% opacity) is "rgb(0,0,0,0.9)" and for white "rgb(255,255,255,0.9)".

Use those colors rather than black and white (and include the quotes)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: SVG to PNG, resize and recolor

Post by fmw42 »

According to identify -verbose 200.svg, you file has no transparency. All colors are fully opaque and not just black and white. You have grayscale values presumably from anti-aliasing at the black-white transition.

Code: Select all

 Format: SVG (Scalable Vector Graphics)
  Mime type: image/svg+xml
  Class: DirectClass
  Geometry: 32x32+0+0
  Resolution: 90x90
  Print size: 0.355556x0.355556
  Units: Undefined
  Type: GrayscaleAlpha
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 16/8-bit
  Channel depth:
    gray: 8-bit
    alpha: 1-bit
  Channel statistics:
    Pixels: 1024
    Gray:
      min: 0 (0)
      max: 65535 (1)
      mean: 56939.3 (0.868838)
      standard deviation: 20127.8 (0.307131)
      kurtosis: 2.95183
      skewness: -2.15575
      entropy: 0.283423
    Alpha:
      min: 65535 (1)
      max: 65535 (1)
      mean: 65535 (1)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
      entropy: 0
  Colors: 90
  Histogram:
        60: (    0,    0,    0,65535) #000000000000FFFF graya(0,1)
         1: (  257,  257,  257,65535) #010101010101FFFF graya(1,1)
         4: (  514,  514,  514,65535) #020202020202FFFF graya(2,1)
         1: (  771,  771,  771,65535) #030303030303FFFF graya(3,1)
         1: ( 1028, 1028, 1028,65535) #040404040404FFFF graya(4,1)
         5: ( 1799, 1799, 1799,65535) #070707070707FFFF graya(7,1)
         2: ( 2056, 2056, 2056,65535) #080808080808FFFF graya(8,1)
         5: ( 2313, 2313, 2313,65535) #090909090909FFFF graya(9,1)
         2: ( 3084, 3084, 3084,65535) #0C0C0C0C0C0CFFFF graya(12,1)
         1: ( 4369, 4369, 4369,65535) #111111111111FFFF graya(17,1)
         1: ( 6682, 6682, 6682,65535) #1A1A1A1A1A1AFFFF graya(26,1)
         3: ( 6939, 6939, 6939,65535) #1B1B1B1B1B1BFFFF graya(27,1)
         2: ( 7196, 7196, 7196,65535) #1C1C1C1C1C1CFFFF graya(28,1)
         3: ( 7453, 7453, 7453,65535) #1D1D1D1D1D1DFFFF graya(29,1)
         1: ( 7710, 7710, 7710,65535) #1E1E1E1E1E1EFFFF graya(30,1)
         1: ( 8481, 8481, 8481,65535) #212121212121FFFF graya(33,1)
         1: (10023,10023,10023,65535) #272727272727FFFF graya(39,1)
         2: (10280,10280,10280,65535) #282828282828FFFF graya(40,1)
         2: (10794,10794,10794,65535) #2A2A2A2A2A2AFFFF graya(42,1)
         1: (11565,11565,11565,65535) #2D2D2D2D2D2DFFFF graya(45,1)
         1: (12079,12079,12079,65535) #2F2F2F2F2F2FFFFF graya(47,1)
         2: (12593,12593,12593,65535) #313131313131FFFF graya(49,1)
         1: (13364,13364,13364,65535) #343434343434FFFF graya(52,1)
         3: (13878,13878,13878,65535) #363636363636FFFF graya(54,1)
         1: (14906,14906,14906,65535) #3A3A3A3A3A3AFFFF graya(58,1)
         2: (15163,15163,15163,65535) #3B3B3B3B3B3BFFFF graya(59,1)
         3: (16448,16448,16448,65535) #404040404040FFFF graya(64,1)
         1: (18247,18247,18247,65535) #474747474747FFFF graya(71,1)
         1: (22616,22616,22616,65535) #585858585858FFFF graya(88,1)
         1: (22873,22873,22873,65535) #595959595959FFFF graya(89,1)
         3: (23130,23130,23130,65535) #5A5A5A5A5A5AFFFF graya(90,1)
         1: (23387,23387,23387,65535) #5B5B5B5B5B5BFFFF graya(91,1)
         2: (26985,26985,26985,65535) #696969696969FFFF graya(105,1)
         2: (27242,27242,27242,65535) #6A6A6A6A6A6AFFFF graya(106,1)
         2: (27499,27499,27499,65535) #6B6B6B6B6B6BFFFF graya(107,1)
         2: (27756,27756,27756,65535) #6C6C6C6C6C6CFFFF graya(108,1)
         1: (28013,28013,28013,65535) #6D6D6D6D6D6DFFFF graya(109,1)
         2: (28270,28270,28270,65535) #6E6E6E6E6E6EFFFF graya(110,1)
         1: (29555,29555,29555,65535) #737373737373FFFF graya(115,1)
         1: (30069,30069,30069,65535) #757575757575FFFF graya(117,1)
         1: (30840,30840,30840,65535) #787878787878FFFF graya(120,1)
         1: (31354,31354,31354,65535) #7A7A7A7A7A7AFFFF graya(122,1)
         5: (32639,32639,32639,65535) #7F7F7F7F7F7FFFFF graya(127,1)
         2: (32896,32896,32896,65535) #808080808080FFFF graya(128,1)
         1: (33153,33153,33153,65535) #818181818181FFFF graya(129,1)
         1: (37265,37265,37265,65535) #919191919191FFFF graya(145,1)
         1: (37522,37522,37522,65535) #929292929292FFFF graya(146,1)
         1: (37779,37779,37779,65535) #939393939393FFFF graya(147,1)
         1: (38293,38293,38293,65535) #959595959595FFFF graya(149,1)
         1: (39064,39064,39064,65535) #989898989898FFFF graya(152,1)
         1: (43690,43690,43690,65535) #AAAAAAAAAAAAFFFF graya(170,1)
         2: (44204,44204,44204,65535) #ACACACACACACFFFF graya(172,1)
         1: (44975,44975,44975,65535) #AFAFAFAFAFAFFFFF graya(175,1)
         1: (45746,45746,45746,65535) #B2B2B2B2B2B2FFFF graya(178,1)
         1: (46517,46517,46517,65535) #B5B5B5B5B5B5FFFF graya(181,1)
         1: (48573,48573,48573,65535) #BDBDBDBDBDBDFFFF graya(189,1)
         2: (49087,49087,49087,65535) #BFBFBFBFBFBFFFFF graya(191,1)
         1: (50115,50115,50115,65535) #C3C3C3C3C3C3FFFF graya(195,1)
         1: (50372,50372,50372,65535) #C4C4C4C4C4C4FFFF graya(196,1)
         4: (50629,50629,50629,65535) #C5C5C5C5C5C5FFFF graya(197,1)
         2: (50886,50886,50886,65535) #C6C6C6C6C6C6FFFF graya(198,1)
         2: (51657,51657,51657,65535) #C9C9C9C9C9C9FFFF graya(201,1)
         1: (52428,52428,52428,65535) #CCCCCCCCCCCCFFFF graya(204,1)
         1: (53199,53199,53199,65535) #CFCFCFCFCFCFFFFF graya(207,1)
         2: (53456,53456,53456,65535) #D0D0D0D0D0D0FFFF graya(208,1)
         2: (53713,53713,53713,65535) #D1D1D1D1D1D1FFFF graya(209,1)
         3: (54484,54484,54484,65535) #D4D4D4D4D4D4FFFF graya(212,1)
         1: (55255,55255,55255,65535) #D7D7D7D7D7D7FFFF graya(215,1)
         1: (56026,56026,56026,65535) #DADADADADADAFFFF graya(218,1)
         2: (56283,56283,56283,65535) #DBDBDBDBDBDBFFFF graya(219,1)
         1: (56540,56540,56540,65535) #DCDCDCDCDCDCFFFF graya(220,1)
         1: (56797,56797,56797,65535) #DDDDDDDDDDDDFFFF graya(221,1)
         1: (57054,57054,57054,65535) #DEDEDEDEDEDEFFFF graya(222,1)
         1: (58339,58339,58339,65535) #E3E3E3E3E3E3FFFF graya(227,1)
         2: (58853,58853,58853,65535) #E5E5E5E5E5E5FFFF graya(229,1)
         2: (60909,60909,60909,65535) #EDEDEDEDEDEDFFFF graya(237,1)
         1: (61166,61166,61166,65535) #EEEEEEEEEEEEFFFF graya(238,1)
         1: (61680,61680,61680,65535) #F0F0F0F0F0F0FFFF graya(240,1)
         1: (61937,61937,61937,65535) #F1F1F1F1F1F1FFFF graya(241,1)
         3: (62194,62194,62194,65535) #F2F2F2F2F2F2FFFF graya(242,1)
         1: (62708,62708,62708,65535) #F4F4F4F4F4F4FFFF graya(244,1)
         1: (62965,62965,62965,65535) #F5F5F5F5F5F5FFFF graya(245,1)
         1: (63222,63222,63222,65535) #F6F6F6F6F6F6FFFF graya(246,1)
         3: (63479,63479,63479,65535) #F7F7F7F7F7F7FFFF graya(247,1)
         2: (63736,63736,63736,65535) #F8F8F8F8F8F8FFFF graya(248,1)
         1: (63993,63993,63993,65535) #F9F9F9F9F9F9FFFF graya(249,1)
         2: (64250,64250,64250,65535) #FAFAFAFAFAFAFFFF graya(250,1)
         5: (64507,64507,64507,65535) #FBFBFBFBFBFBFFFF graya(251,1)
         3: (64764,64764,64764,65535) #FCFCFCFCFCFCFFFF graya(252,1)
       812: (65535,65535,65535,65535) #FFFFFFFFFFFFFFFF graya(255,1)
You can set the background to none and get transparency where it is white.

This converts the image into a black background and various transparencies.

Code: Select all

convert -background none 200.svg -verbose info:

Code: Select all

Image: 200.svg
  Format: SVG (Scalable Vector Graphics)
  Mime type: image/svg+xml
  Class: DirectClass
  Geometry: 32x32+0+0
  Resolution: 90x90
  Print size: 0.355556x0.355556
  Units: Undefined
  Type: Bilevel
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 16/8-bit
  Channel depth:
    gray: 1-bit
    alpha: 8-bit
  Channel statistics:
    Pixels: 1024
    Gray:
      min: 0 (0)
      max: 0 (0)
      mean: 0 (0)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
      entropy: nan
    Alpha:
      min: 0 (0)
      max: 65535 (1)
      mean: 8595.7 (0.131162)
      standard deviation: 20127.8 (0.307131)
      kurtosis: 2.95183
      skewness: -2.15575
      entropy: nan
  Alpha: graya(0,0)   #0000000000000000
  Colors: 90
  Histogram:
       812: (    0,    0,    0,    0) #0000000000000000 graya(0,0)
        60: (    0,    0,    0,65535) #000000000000FFFF graya(0,1)
         5: (    0,    0,    0,63736) #000000000000F8F8 graya(0,1)
         5: (    0,    0,    0,32896) #0000000000008080 graya(0,1)
         5: (    0,    0,    0,63222) #000000000000F6F6 graya(0,1)
         5: (    0,    0,    0, 1028) #0000000000000404 graya(0,0)
         4: (    0,    0,    0,65021) #000000000000FDFD graya(0,1)
         4: (    0,    0,    0,14906) #0000000000003A3A graya(0,0)
         3: (    0,    0,    0,58082) #000000000000E2E2 graya(0,1)
         3: (    0,    0,    0, 2056) #0000000000000808 graya(0,0)
         3: (    0,    0,    0, 3341) #0000000000000D0D graya(0,0)
         3: (    0,    0,    0,58596) #000000000000E4E4 graya(0,1)
         3: (    0,    0,    0,42405) #000000000000A5A5 graya(0,1)
         3: (    0,    0,    0,11051) #0000000000002B2B graya(0,0)
         3: (    0,    0,    0,  771) #0000000000000303 graya(0,0)
         3: (    0,    0,    0,49087) #000000000000BFBF graya(0,1)
         3: (    0,    0,    0,51657) #000000000000C9C9 graya(0,1)
         2: (    0,    0,    0,52942) #000000000000CECE graya(0,1)
         2: (    0,    0,    0,58339) #000000000000E3E3 graya(0,1)
         2: (    0,    0,    0, 4626) #0000000000001212 graya(0,0)
         2: (    0,    0,    0,32639) #0000000000007F7F graya(0,0)
         2: (    0,    0,    0,50372) #000000000000C4C4 graya(0,1)
         2: (    0,    0,    0, 1799) #0000000000000707 graya(0,0)
         2: (    0,    0,    0, 1285) #0000000000000505 graya(0,0)
         2: (    0,    0,    0, 6682) #0000000000001A1A graya(0,0)
         2: (    0,    0,    0,21331) #0000000000005353 graya(0,0)
         2: (    0,    0,    0,63479) #000000000000F7F7 graya(0,1)
         2: (    0,    0,    0,62451) #000000000000F3F3 graya(0,1)
         2: (    0,    0,    0,38550) #0000000000009696 graya(0,1)
         2: (    0,    0,    0,16448) #0000000000004040 graya(0,0)
         2: (    0,    0,    0,38036) #0000000000009494 graya(0,1)
         2: (    0,    0,    0,38293) #0000000000009595 graya(0,1)
         2: (    0,    0,    0,14649) #0000000000003939 graya(0,0)
         2: (    0,    0,    0,13878) #0000000000003636 graya(0,0)
         2: (    0,    0,    0,55255) #000000000000D7D7 graya(0,1)
         2: (    0,    0,    0,37779) #0000000000009393 graya(0,1)
         2: (    0,    0,    0,11822) #0000000000002E2E graya(0,0)
         2: (    0,    0,    0,12079) #0000000000002F2F graya(0,0)
         2: (    0,    0,    0,37265) #0000000000009191 graya(0,1)
         2: (    0,    0,    0, 9252) #0000000000002424 graya(0,0)
         2: (    0,    0,    0,54741) #000000000000D5D5 graya(0,1)
         1: (    0,    0,    0, 8481) #0000000000002121 graya(0,0)
         1: (    0,    0,    0, 7196) #0000000000001C1C graya(0,0)
         1: (    0,    0,    0, 4369) #0000000000001111 graya(0,0)
         1: (    0,    0,    0, 3598) #0000000000000E0E graya(0,0)
         1: (    0,    0,    0, 2827) #0000000000000B0B graya(0,0)
         1: (    0,    0,    0, 2570) #0000000000000A0A graya(0,0)
         1: (    0,    0,    0, 2313) #0000000000000909 graya(0,0)
         1: (    0,    0,    0, 1542) #0000000000000606 graya(0,0)
         1: (    0,    0,    0, 3855) #0000000000000F0F graya(0,0)
         1: (    0,    0,    0,65278) #000000000000FEFE graya(0,1)
         1: (    0,    0,    0,64764) #000000000000FCFC graya(0,1)
         1: (    0,    0,    0,64507) #000000000000FBFB graya(0,1)
         1: (    0,    0,    0,61166) #000000000000EEEE graya(0,1)
         1: (    0,    0,    0,58853) #000000000000E5E5 graya(0,1)
         1: (    0,    0,    0,57825) #000000000000E1E1 graya(0,1)
         1: (    0,    0,    0,57054) #000000000000DEDE graya(0,1)
         1: (    0,    0,    0,55512) #000000000000D8D8 graya(0,1)
         1: (    0,    0,    0,53970) #000000000000D2D2 graya(0,1)
         1: (    0,    0,    0,53456) #000000000000D0D0 graya(0,1)
         1: (    0,    0,    0,52171) #000000000000CBCB graya(0,1)
         1: (    0,    0,    0,50629) #000000000000C5C5 graya(0,1)
         1: (    0,    0,    0,47288) #000000000000B8B8 graya(0,1)
         1: (    0,    0,    0,42919) #000000000000A7A7 graya(0,1)
         1: (    0,    0,    0,42662) #000000000000A6A6 graya(0,1)
         1: (    0,    0,    0,42148) #000000000000A4A4 graya(0,1)
         1: (    0,    0,    0,37522) #0000000000009292 graya(0,1)
         1: (    0,    0,    0,35980) #0000000000008C8C graya(0,1)
         1: (    0,    0,    0,35466) #0000000000008A8A graya(0,1)
         1: (    0,    0,    0,34695) #0000000000008787 graya(0,1)
         1: (    0,    0,    0,34181) #0000000000008585 graya(0,1)
         1: (    0,    0,    0,32382) #0000000000007E7E graya(0,0)
         1: (    0,    0,    0,28270) #0000000000006E6E graya(0,0)
         1: (    0,    0,    0,28013) #0000000000006D6D graya(0,0)
         1: (    0,    0,    0,27756) #0000000000006C6C graya(0,0)
         1: (    0,    0,    0,27242) #0000000000006A6A graya(0,0)
         1: (    0,    0,    0,26471) #0000000000006767 graya(0,0)
         1: (    0,    0,    0,21845) #0000000000005555 graya(0,0)
         1: (    0,    0,    0,20560) #0000000000005050 graya(0,0)
         1: (    0,    0,    0,19789) #0000000000004D4D graya(0,0)
         1: (    0,    0,    0,19018) #0000000000004A4A graya(0,0)
         1: (    0,    0,    0,16962) #0000000000004242 graya(0,0)
         1: (    0,    0,    0,15420) #0000000000003C3C graya(0,0)
         1: (    0,    0,    0,15163) #0000000000003B3B graya(0,0)
         1: (    0,    0,    0,13107) #0000000000003333 graya(0,0)
         1: (    0,    0,    0,12336) #0000000000003030 graya(0,0)
         1: (    0,    0,    0,10280) #0000000000002828 graya(0,0)
         1: (    0,    0,    0, 9509) #0000000000002525 graya(0,0)
         1: (    0,    0,    0, 8738) #0000000000002222 graya(0,0)
         1: (    0,    0,    0, 8995) #0000000000002323 graya(0,0)
So to change the color, you could do

Code: Select all

convert -density 288 -background none 200.svg -alpha off -fill red -opaque black -alpha on 200.png
to change the color from black to red
Post Reply