我正在尝试弄清楚如何使用 Image::Resize
Perl 模块来缩小 PNG 图像。以下代码确实有效:
my $image = Image::Resize->new($read_path);
my $gd = $image->resize(1000,1000);
open (DISPLAY,">$write_path") || die "Cant write $write_path, Reason: $!";
binmode DISPLAY;
if ($read_path =~ /\.jpg$/i) {
print DISPLAY $gd->jpeg();
} elsif ($read_path =~ /\.gif$/i) {
print DISPLAY $gd->gif();
} elsif ($read_path =~ /\.png$/i) {
print DISPLAY $gd->png();
}
close DISPLAY;
但是,结果不是我想要的(转换透明 PNG 时缩放版本有黑色背景)
缩放:
我如何告诉它在图像上放置白色背景?我查看了联机帮助页,但看不到任何有用的信息:
http://search.cpan.org/dist/Image-Resize/Resize.pm
谢谢!
更新:对于任何有兴趣的人,我最后所做的只是使用 convert
;
convert "$read_path" -background white -flatten "$path/$filename"
在这种情况下实际上效果更好,因为我们不需要透明度(而且 jpg 的尺寸要小得多)
最佳答案
Image::Resize
模块只做那个,调整图像的大小。
但是,它返回一个 GD::Image
目的。然后您可以使用 gd
的全部功能为您的形象。
方法GD::Image::transparent
可能是你要找的。从文档复制
# allocate some colors
my $white = $im->colorAllocate(255,255,255);
# make the background transparent and interlaced
$im->transparent($white);
$im->interlaced('true');
$im
是 GD::Image
对象,在您的情况下由 Image::Resize::resize
返回.
具体问题没有解释,我不确定你是如何选择黑色背景的,但如果上面没有做到这一点,你会在 GD::Image
中找到具体的解决方案。 .
This post通过链接到 this post 可能会直接给出答案
: 启用 saveAlpha()
GD设定。
感谢Wick征求意见。
关于Perl Image::Resize 模块和 PNG 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38891584/