简体   繁体   中英

How to write lossless WebP files with PerlMagick

I'm trying to convert PNG files to lossless WebP in Perl with Graphics::Magick. Command line for that is:

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

My Perl code looks something like that:

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?

Thanks,

Update: looks like I need to call AddDefiniton API function ( http://www.graphicsmagick.org/api/image.html#adddefinition ). Looks like it's not exported via Perl API as of now.

I know it is of no help to you, but for those interested in how to do it in PHP, here is how:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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