kcott has asked for the wisdom of the Perl Monks concerning the following question:
I'm generating some fairly basic SVG files and will want to convert them to PNG format. Each SVG file will be used to create a number of PNG files of different sizes (it's only a size change; aspect ratio, colours, etc. all remaining the same). I'd also like the solution to be as portable as possible (at least working on *nix and MSWin platforms). I've included my investigations so far, my questions are at the end.
Here's the test SVG file, test.svg, that I've been playing with and referenced in various examples below:
I had the ImageMagick® convert utility available, so I tried that first. This seemed to work fine; I tested at two reduced sizes:
$ convert test.svg -resize 16x16 test016.png $ convert test.svg -resize 32x32 test032.png
However, when I looked into installing the Image::Magick module for use in my scripts, I was amazed to find only failures (UNKNOWN: 1276; FAIL: 412) in the "CPAN Testers Matrix: PerlMagick 6.89-1". I followed the links to the previous five releases, thinking maybe there was some problem with just 6.89-1; unfortunately, all were exactly the same except for 6.86 which had just one PASS amongst hundreds of failures. I don't know if that's some sort of reporting error. I chose not to follow that path any further.
I had previously, successfully installed GD, so I decided to look into that further. I browsed the GD::Convert, GD::SVG and SVG modules. While this looked like it might work (after a fashion), it didn't seem particularly straightforward and even the documentation says:
"GD::SVG may provide a short-term solution while scripts are re-written to enable more direct output of SVG."
There were also too many caveats for my liking, so I abandoned this avenue also.
SVG has a small section on converting to PNG. It mentions Image::Magick and also Image::LibRSVG. I hadn't heard of this other module before and decided to try it. It installed without any fuss and usage was totally uncomplicated:
$ perl -wMstrict -MImage::LibRSVG -e 'Image::LibRSVG::->new()->convert +AtSize("test.svg", "test016.png", 16, 16)' $ perl -wMstrict -MImage::LibRSVG -e 'Image::LibRSVG::->new()->convert +AtSize("test.svg", "test032.png", 32, 32)'
Not only that, but it produced PNG files that were substantially smaller than those produced by convert (without any noticable degradation of image quality; in fact, the 16x16 one looked a little crisper). I had renamed the files I produced earlier (*.png to *_via_convert.png), here's a size comparison:
-rw-r--r-- 1 ken staff 678 Apr 23 13:03 test.svg -rw-r--r-- 1 ken staff 124 Apr 23 14:01 test016.png -rw-r--r-- 1 ken staff 316 Apr 23 13:10 test016_via_convert.png -rw-r--r-- 1 ken staff 186 Apr 23 13:57 test032.png -rw-r--r-- 1 ken staff 415 Apr 23 13:11 test032_via_convert.png
My only concerns with Image::LibRSVG are its age (no update in 11 years) and no successful builds on MSWin. The age may not be a huge issue: SVG has been stable for many years and, as an interface to the librsvg library, it may rely on that library being current, especially if its API hasn't changed. I randomly checked many failures on "CPAN Testers Matrix: Image-LibRSVG 0.07": all (going back to Perl 5.10) were UNKNOWNs, and all failed due to the librsvg library not being found — this may not be a problem either.
So, after all that, I have these questions:
— Ken
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Image Conversion: SVG to PNG
by syphilis (Archbishop) on Apr 23, 2017 at 09:44 UTC | |
by kcott (Archbishop) on Apr 23, 2017 at 18:01 UTC | |
by syphilis (Archbishop) on Apr 24, 2017 at 09:10 UTC | |
by kcott (Archbishop) on Apr 25, 2017 at 08:37 UTC | |
by syphilis (Archbishop) on Apr 25, 2017 at 11:37 UTC | |
| |
by Anonymous Monk on Apr 23, 2017 at 09:48 UTC | |
Re: Image Conversion: SVG to PNG
by haukex (Archbishop) on Apr 23, 2017 at 09:47 UTC | |
by kcott (Archbishop) on Apr 23, 2017 at 19:17 UTC | |
Re: Image Conversion: SVG to PNG
by Corion (Patriarch) on Apr 24, 2017 at 08:37 UTC | |
by vrk (Chaplain) on Apr 24, 2017 at 10:26 UTC | |
by Anonymous Monk on Apr 24, 2017 at 11:19 UTC | |
by kcott (Archbishop) on Apr 25, 2017 at 09:52 UTC | |
Re: Image Conversion: SVG to PNG
by RonW (Parson) on Apr 24, 2017 at 21:47 UTC | |
by kcott (Archbishop) on Apr 25, 2017 at 10:32 UTC | |
by RonW (Parson) on Apr 26, 2017 at 21:34 UTC | |
Re: Image Conversion: SVG to PNG
by Anonymous Monk on Apr 23, 2017 at 08:37 UTC | |
by kcott (Archbishop) on Apr 23, 2017 at 18:18 UTC | |
Re: Image Conversion: SVG to PNG
by vrk (Chaplain) on Apr 24, 2017 at 08:26 UTC |