Search code examples
svgimagemagickimagemagick-convert

ImageMagick: Zoom SVG and convert it to PNG?


I have the following 8×8 SVG graphic for which I want a scaled version of as PNG. (I found no way to upload SVG so you can see it.) The SVG consist of 8×8 black/white "pixels".

<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8" viewBox="0 0 8 8">
<path fill="#fff" d="M0,0H8V8H0"/>
<path fill="#000" d="M0,0H7V1H6V0H5V3H8V2H7V5H6V5H6V4H8V6H7V8H8V7H1V8H2V5H3V6H4V7H5V4H2V1H3V2H4V3H1V4H0"
fill-rule="evenodd"/>
</svg>

When I am using ImageMagick's

> convert a3x2.svg -scale 80x80 a3x2.png

as of this answer for scaling a PNG, then I am getting the following result which has nothing to do with the input:

result of conversion

What I want is a scaled black/white PNG where each pixel has a "size" of 10×10; no smoothing or dithering. What does not work is -resize 80x80 (will blur and still has wrong pixel values).


Edit

According to this answer, a solution with Inkscape is

> inkscape -w 80 -h 80 a3x2.svg -o a3x2.png

which works, but I'd rather use ImageMagick. For reference, here is the result with Inkscape so you can see that the result of convert from above is not as expected.

PNG Result with Imagemagick

> convert -version

Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png tiff webp wmf x xml zlib

Edit 2

As asked in a comment, here is the result of

> convert -verbose a3x2.svg txt:

'inkscape' '/tmp/magick-445746DLWe3-eb4nrF' --export-filename='/tmp/magick-445746FrMsKiicip2C' --export-dpi='96,96' --export-background='rgb(100%,100%,100%)' --export-background-opacity='1' > '/tmp/magick-445746cd-3Bn1wlmb8' 2>&1
mvg:/tmp/magick-445746UpfR0goE2RRA=>/tmp/magick-445746UpfR0goE2RRA MVG 8x8 8x8+0+0 16-bit sRGB 445B 0.000u 0:00.000
a3x2.svg MVG 8x8 8x8+0+0 16-bit sRGB 445B 0.000u 0:00.000
# ImageMagick pixel enumeration: 8,8,65535,srgba
0,0: (0,0,0,65535)  #000000000000FFFF  black
1,0: (0,0,0,65535)  #000000000000FFFF  black
2,0: (0,0,0,65535)  #000000000000FFFF  black
3,0: (0,0,0,65535)  #000000000000FFFF  black
4,0: (0,0,0,65535)  #000000000000FFFF  black
5,0: (0,0,0,65535)  #000000000000FFFF  black
6,0: (0,0,0,65535)  #000000000000FFFF  black
7,0: (0,0,0,65535)  #000000000000FFFF  black
0,1: (0,0,0,65535)  #000000000000FFFF  black
1,1: (0,0,0,65535)  #000000000000FFFF  black
2,1: (0,0,0,65535)  #000000000000FFFF  black
3,1: (0,0,0,65535)  #000000000000FFFF  black
4,1: (0,0,0,65535)  #000000000000FFFF  black
5,1: (0,0,0,65535)  #000000000000FFFF  black
6,1: (0,0,0,65535)  #000000000000FFFF  black
7,1: (0,0,0,65535)  #000000000000FFFF  black
0,2: (0,0,0,65535)  #000000000000FFFF  black
1,2: (0,0,0,65535)  #000000000000FFFF  black
2,2: (0,0,0,65535)  #000000000000FFFF  black
3,2: (0,0,0,65535)  #000000000000FFFF  black
4,2: (0,0,0,65535)  #000000000000FFFF  black
5,2: (0,0,0,65535)  #000000000000FFFF  black
6,2: (61439,61439,61439,65535)  #EFFFEFFFEFFFFFFF  srgba(94%,94%,94%,1)
7,2: (0,0,0,65535)  #000000000000FFFF  black
0,3: (0,0,0,65535)  #000000000000FFFF  black
1,3: (0,0,0,65535)  #000000000000FFFF  black
2,3: (0,0,0,65535)  #000000000000FFFF  black
3,3: (0,0,0,65535)  #000000000000FFFF  black
4,3: (0,0,0,65535)  #000000000000FFFF  black
5,3: (0,0,0,65535)  #000000000000FFFF  black
6,3: (0,0,0,65535)  #000000000000FFFF  black
7,3: (0,0,0,65535)  #000000000000FFFF  black
0,4: (0,0,0,65535)  #000000000000FFFF  black
1,4: (0,0,0,65535)  #000000000000FFFF  black
2,4: (0,0,0,65535)  #000000000000FFFF  black
3,4: (0,0,0,65535)  #000000000000FFFF  black
4,4: (0,0,0,65535)  #000000000000FFFF  black
5,4: (0,0,0,65535)  #000000000000FFFF  black
6,4: (0,0,0,65535)  #000000000000FFFF  black
7,4: (0,0,0,65535)  #000000000000FFFF  black
0,5: (65535,65535,65535,65535)  #FFFFFFFFFFFFFFFF  white
1,5: (61439,61439,61439,65535)  #EFFFEFFFEFFFFFFF  srgba(94%,94%,94%,1)
2,5: (0,0,0,65535)  #000000000000FFFF  blaca3x2.svg=> MVG 8x8 8x8+0+0 16-bit sRGB 445B 0.000u 0:00.000
k
3,5: (0,0,0,65535)  #000000000000FFFF  black
4,5: (61439,61439,61439,65535)  #EFFFEFFFEFFFFFFF  srgba(94%,94%,94%,1)
5,5: (0,0,0,65535)  #000000000000FFFF  black
6,5: (0,0,0,65535)  #000000000000FFFF  black
7,5: (0,0,0,65535)  #000000000000FFFF  black
0,6: (65535,65535,65535,65535)  #FFFFFFFFFFFFFFFF  white
1,6: (65535,65535,65535,65535)  #FFFFFFFFFFFFFFFF  white
2,6: (0,0,0,65535)  #000000000000FFFF  black
3,6: (0,0,0,65535)  #000000000000FFFF  black
4,6: (0,0,0,65535)  #000000000000FFFF  black
5,6: (0,0,0,65535)  #000000000000FFFF  black
6,6: (0,0,0,65535)  #000000000000FFFF  black
7,6: (0,0,0,65535)  #000000000000FFFF  black
0,7: (65535,65535,65535,65535)  #FFFFFFFFFFFFFFFF  white
1,7: (0,0,0,65535)  #000000000000FFFF  black
2,7: (0,0,0,65535)  #000000000000FFFF  black
3,7: (0,0,0,65535)  #000000000000FFFF  black
4,7: (0,0,0,65535)  #000000000000FFFF  black
5,7: (0,0,0,65535)  #000000000000FFFF  black
6,7: (0,0,0,65535)  #000000000000FFFF  black
7,7: (0,0,0,65535)  #000000000000FFFF  black

Solution

  • Mmmm... I don't know what's going on, but I can confirm that if I use the rsvg delegate in place of the MVG delegate, I get the correct result:

    enter image description here

    I find it simplest to use the docker alpine image to demonstrate because it is very current and has an extensive set of delegates without any user configuration. So, I just did:

    # Start latest alpine image, bind mounting current working directory to /work
    docker run -it -v "$(pwd)":/work alpine
    
    apk update && apk add imagemagick
    
    identify -version
    Version: ImageMagick 7.1.0-52 Q16-HDRI aarch64 20549 https://imagemagick.org
    Copyright: (C) 1999 ImageMagick Studio LLC
    License: https://imagemagick.org/script/license.php
    Features: Cipher DPC HDRI Modules 
    Delegates (built-in): bzlib cairo fontconfig freetype gslib heic jng jpeg jxl lcms ltdl lzma png ps rsvg tiff webp x xml zlib
    Compiler: gcc (12.2)
    
    magick image.svg -scale 80x80 result.png