Perl Image::Resize 模块和 PNG 图像

标签 perl

我正在尝试弄清楚如何使用 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 时缩放版本有黑色背景)

原文: enter image description here

缩放:

enter image description here

我如何告诉它在图像上放置白色背景?我查看了联机帮助页,但看不到任何有用的信息:

http://search.cpan.org/dist/Image-Resize/Resize.pm

谢谢!

更新:对于任何有兴趣的人,我最后所做的只是使用 convert;

将它们从 .png 转换为 .jpg
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');

$imGD::Image对象,在您的情况下由 Image::Resize::resize 返回.

具体问题没有解释,我不确定你是如何选择黑色背景的,但如果上面没有做到这一点,你会在 GD::Image 中找到具体的解决方案。 .


This post通过链接到 this post 可能会直接给出答案 : 启用 saveAlpha() GD设定。

感谢Wick征求意见。

关于Perl Image::Resize 模块和 PNG 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38891584/

相关文章:

perl - 通过 Perl 的 DBI $sth->execute() 函数将字符串插入数据库时​​将其转换为数字

perl - 为什么将这个匿名子例程声明在Perl中解析为间接对象方法?

perl - 即使使用 utf8 编码也能打印宽字符

php - 正面回顾与匹配重置 (\K) 正则表达式功能

perl - 库文件和模块有什么区别?

perl - 如何使用 App::Cmd 模块实现子命令?

mysql 非 native ,仅当表存在时才选择

perl - 为什么我的 Perl CGI 脚本提示 "Can' tlocate CGI/Simple.pm”?

perl - 防止 SQL 表名注入(inject) - 多远?

arrays - 将 Perl 数组放入哈希中