4

I have a SVG file similar to

http://www.fileformat.info/info/unicode/char/00c1/latin_capital_letter_a_with_acute.svg

When I use ImageMagick to convert it to a PNG like this:

convert latin_capital_letter_a_with_acute.svg tmp.png

then only the top portion of the character is visible.

The SVG file doesn't specify any height and width. If I manually specify width="25cm" height="25cm" in the XML file then the full character in a corner as small image with a large background filling the rest of the image. I have searched in Stackexchange and various forums but couldn't find a solution for this.

I have tried the following using PHP as well. But it is still producing the cropped image.

I think this is an issue with detecting the correct size of the SVG image. But can't think of an easy way to do this.

Community
  • 1
  • 1
dors
  • 1,152
  • 11
  • 19

1 Answers1

3

I converted the SVG from your link sucessfully using this command:

convert    \
 http://www.fileformat.info/info/unicode/char/00c1/latin_capital_letter_a_with_acute.svg \
 latin_capital_letter_a_with_acute.png

Here is the result, which looks OK to me:

PNG, generated from SVG

My version of ImageMagick is (according to convert -version):

 Version: ImageMagick 6.9.0-0 Q16 x86_64 2014-12-06 http://www.imagemagick.org
 Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
 Features: DPC Modules
 Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gslib gvc \
                       jbig jng jp2 jpeg lcms lqr ltdl lzma openexr \
                       pangocairo png ps rsvg tiff webp wmf x xml zlib

However, it is not ImageMagick directly and all by itself that does the heavy-lifting of interpreting the SVG. My ImageMagick uses an external delegate to achieve this. How exactly this works can be made visible by adding the -verbose setting to the command line:

convert -verbose letter_a_with_acute.svg letter_a_with_acute.png

 "/opt/local/bin/inkscape" "/var/tmp/magick-12470O5QRRVap0Ub4" \
     --export-eps="/var/tmp/magick-124705G1EV-reRRrb" --export-dpi="90,90" \
     --export-background="rgb(100%,100%,100%)" --export-background-opacity="1" \
       > "/var/tmp/magick-12470W9feuKm0JHUA" 2>&1

 /var/tmp/magick-12470qEPdaNM3mKYw1 PNG 155x209 155x209+0+0 8-bit sRGB 3.04KB 0.000u 0:00.000
 /var/tmp/magick-124705G1EV-reRRrb PS 155x209 155x209+0+0 16-bit sRGB 3.04KB 0.000u 0:00.000

 letter_a_with_acute.svg=>/var/tmp/magick-124705G1EV-reRRrb PS 155x209 155x209+0+0 16-bit sRGB 3.04KB 0.000u 0:00.000
 letter_a_with_acute.svg=>latin_capital_letter_a_with_acute.png PS 155x209 155x209+0+0 8-bit sRGB 17c 2.93KB 0.000u 0:00.000

 [ghostscript library] -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT \
                       -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 \
                      "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 \
                      "-r72x72" -g155x209  "-sOutputFile=/var/tmp/magick-12470qEPdaNM3mKYw%d" \
                      "-f/var/tmp/magick-12470Xt3b3Qubkxx2" \
                      "-f/var/tmp/magick-12470VRcUz0MbmdiC"

As you can clearly see, convert employs two delegates in two different steps to bring about the conversion:

  1. First, it runs an inkscape command. This command exports the SVG to EPS.

  2. Second, it runs a Ghostscript command. This command converts the EPS from step 1 to the final PNG.

  3. In between the above two steps, it runs some other command, probably identify, in order to find out the dimensions of the EPS that was produced.

In other words, if you are not able to set up your ImageMagick delegates correctly so that they will process SVG files for you, you could always use Inkscape directly on the command line to create the PNG:

inkscape                                    \
  --without-gui                             \
  --export-png=out.png                      \
  --export-dpi="90,90"                      \
  --export-background="rgb(100%,100%,100%)" \
  --export-background-opacity="1"           \
    input.svg

Looking at the result, out.png, it seems like it may be using too much white space around the letter:

  identify out.png  
   out.png PNG 744x1052 744x1052+0+0 8-bit sRGB 13.1KB 0.000u 0:00.000

This can be rectified by ImageMagick:

  convert out.png -trim +repage trimmed.png

Check:

  identify trimmed.png 
   trimmed.png PNG 193x260 193x260+0+0 8-bit sRGB 256c 3.53KB 0.000u 0:00.000
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • On the server I'm at I have a different version with different delegates. Since I'm on a shared host upgrading is going to be tricky. Thanks for the explanation. $ convert --version Version: ImageMagick 6.6.9-7 2014-03-06 Q16 http://www.imagemagick.org Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC Features: OpenMP – dors Dec 16 '14 at 03:31