简体   繁体   English

如何使用PerlMagick编写无损WebP文件

[英]How to write lossless WebP files with PerlMagick

I'm trying to convert PNG files to lossless WebP in Perl with Graphics::Magick. 我正在尝试使用Graphics :: Magick将PNG文件转换为Perl中的无损WebP。 Command line for that is: 命令行是:

$ gm convert in.png -define webp:lossless=true out.webp

My Perl code looks something like that: 我的Perl代码如下所示:

use Graphics::Magick;

my $image = Graphics::Magick->new();
$image->Read("in.png");
my $image_data = $image->ImageToBlock(magick => "webp");
print $out_fh $image_data;

This code writes lossy WebP files perfectly, but how can I express the "-define" thing in terms of Perl API? 这段代码完美地编写了有损WebP文件,但是如何用Perl API表示“ -define”呢?

Thanks, 谢谢,

Update: looks like I need to call AddDefiniton API function ( http://www.graphicsmagick.org/api/image.html#adddefinition ). 更新:看来我需要调用AddDefiniton API函数( http://www.graphicsmagick.org/api/image.html#adddefinition )。 Looks like it's not exported via Perl API as of now. 到目前为止,它似乎尚未通过Perl API导出。

I know it is of no help to you, but for those interested in how to do it in PHP, here is how: 我知道这对您没有帮助,但是对于那些对如何使用PHP感兴趣的人来说,方法如下:

$im = new \Gmagick($src);
$im->setimageformat('WEBP');

// Not completely sure if setimageoption() has always been there, so lets check first.
if (method_exists($im, 'setimageoption')) {
    $im->setimageoption('webp', 'lossless', 'true');
}
$imageBlob = $im->getImageBlob();
$success = @file_put_contents($destination, $imageBlob);

For more webp options, check out this code 有关更多webp选项,请查看此代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM