如何将svg映像转换为png,将其保存到文件中,并收集有关它的基本信息?
#!/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";当我运行这个时,我得到:
(undef) x (undef),(undef)字节
没有错误,没有test.png,图像尺寸也是没有定义的。
如何在PerlMagick中将svg映像转换为png?
至于这是否是重复的:大多数其他问题、博客文章和教程都使用命令行ImageMagick convert工具。我想避免那样做。我目前调用Inkscape进行转换,但分析器将这些调用显示为代码库中的热点之一。我正在处理~320 svg文件,它需要15分钟来转换它们。我希望通过一个库,我可以获得更好的性能,因为我不需要创建新的进程和编写临时文件。我也在调查Inkscape shell。
发布于 2022-01-09 13:41:29
必须指定SVG图像的宽度和高度。以下几点对我来说是可行的:
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";输出
300 x 200, 1379 byteshttps://stackoverflow.com/questions/70637220
复制相似问题