1

How do I convert a svg image to png, save it to a file, and collect basic information about it?

#!/usr/bin/perl 
use strict;
use warnings;
use Image::Magick;

my $svg = <<'SVG';
<?xml version="1.0" encoding="utf-8" ?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <rect fill="white" height="87" rx="10" ry="10" stroke="black" stroke-width="1" width="56" x="0" y="0"/>
</svg>
SVG

my $im = Image::Magick->new();
$im->Read(blob => $svg) or die "Could not read: $!";

$im->Write(filename => 'test.png') or die "Cannot write: $!";
my $width = $im->Get('height') || '(undef)';
my $height = $im->Get('width') || '(undef)';
my $size = $im->Get('filesize') || '(undef)';

print "$height x $width, $size bytes\n";

When I run this, I get:

(undef) x (undef), (undef) bytes

No errors, no test.png, and image dimensions are undefined.

How do I convert an svg image to png in PerlMagick?

As for whether this is a duplicate: Most other questions, blog posts, and tutorials use the command line ImageMagick convert tool. I want to avoid that. I currently call Inkscape for conversion, but the profiler shows these calls as one of the hot spots in my code base. I am dealing with ~320 svg files and it takes ~15 minutes to convert them. I hope with a library I can get better performance cause I don't need to create new processes and write temp files. I am also looking into Inkscape shell.

Robert
  • 7,394
  • 40
  • 45
  • 64
  • Does this answer your question? [How to convert a SVG to a PNG with ImageMagick?](https://stackoverflow.com/questions/9853325/how-to-convert-a-svg-to-a-png-with-imagemagick) – Jim Garrison Jan 09 '22 at 00:58
  • Did you do a web search for _imagemagick svg to png_? Lots of hits. – Jim Garrison Jan 09 '22 at 00:59
  • @JimGarrison Yes, I saw questions like the one you linked, but want to avoid calling an external program. I've edited the question to clarify. – Robert Jan 09 '22 at 01:48
  • If I save your inline svg image to a file `sample.svg` and run the command `display sample.svg` I get the error *"display-im6.q16: must specify image size"* I think your inline svg is missing `width` and `height` parameters ? – Håkon Hægland Jan 09 '22 at 13:19

1 Answers1

2

You must specify a width and height of the SVG image. The following works for me:

use strict;
use warnings;
use Image::Magick;

my $svg = <<'SVG';
<?xml version="1.0" encoding="utf-8" ?>
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="200" version="1.1">
  <rect fill="white" height="87" rx="10" ry="10" stroke="black" stroke-width="1" width="56" x="0" y="0"/>
</svg>
SVG
my $im = Image::Magick->new(magick => 'svg');
my $status;
$status = $im->BlobToImage($svg) and warn $status;
$status = $im->Write(filename => 'test.png') and warn $status;
my $width = $im->Get('height') || '(undef)';
my $height = $im->Get('width') || '(undef)';
my $size = $im->Get('filesize') || '(undef)';
print "$height x $width, $size bytes\n";

Output:

300 x 200, 1379 bytes
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174