Image is cropped when converting from SVG to PNG using ImageMagick
I have an 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 PNG like this:
convert latin_capital_letter_a_with_acute.svg tmp.png
then only the top of the symbol is visible.
The SVG file does not define height and width. If I manually specify width = "25cm" height = "25cm" in the XML file, then the full character in the corner is like a small image with a large background filling the rest of the image. I've searched Stackexchange and various forums but couldn't find a solution for this.
I have also tried using PHP. But it still creates a cropped image.
I think this is a problem in determining the correct size of the SVG image. But I can't think of an easy way to do this.
source to share
I have successfully converted SVG from your link 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 that looks good to me:
My ImageMagick version (as per 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, this is not ImageMagick directly, but does the hard work of interpreting SVG by itself. To achieve this, My ImageMagick uses an external delegate. How exactly this works can be made visible by adding a parameter -verbose
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
uses two delegates in two different steps for conversion:
-
The command is executed first
inkscape
. This command will export SVG to EPS. -
Second, it runs the Ghostscript command. This command will convert EPS from step 1 to the final PNG.
-
In between the previous two steps, another command is executed, possibly
identify
to find out the dimensions of the generated EPS.
In other words, if you can't properly configure your ImageMagick delegates to handle SVG files for you, you can always use Inkscape directly on the command line to create a 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 looks like it might 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 fixed with 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
source to share