Imager::File::WEBP - read and write WEBP files
use Imager; my $img = Imager->new; $img->read(file=>"foo.webp") or die $img->errstr; # type won't be necessary if the extension is webp from Imager 1.008 $img->write(file => "foo.webp", type => "webp") or die $img->errstr;
Implements .webp file support for Imager.
Due to the limitations of webp grayscale images are written as RGB images.
webp
webp_mode - set when reading an image and used when writing. Possible values:
webp_mode
lossy - write in lossy mode. This is the default.
lossy
lossless - write in lossless mode.
lossless
webp_quality - the lossy compression quality, a floating point number from 0 (bad) to 100 (better). Default: 80.
webp_quality
If Imager::File::WEBP was built with Imager 1.010 then EXIF metadata will also be read from the file. See the description at "JPEG" in Imager::Files.
These only have meaning for files with more than one image.
Tags that can be set for the whole file, only the tag value from the first image is used when writing:
webp_loop_count - the number of times to loop the animation. When writing an animation this is fetched only from the first image. When reading, the same file global value is set for every image read. Default: 0.
webp_loop_count
webp_background - the background color for the animation. When writing an animation this is fetched only from the first image. When reading, the same file global value is set for every image read. Default: white.
webp_background
The following can be set separately for each image in the file:
webp_left, webp_top - position of the frame within the animation frame. Only has meaning for multiple image files. Odd numbers are stored as the even number just below. Default: 0.
webp_left
webp_top
webp_duration - duration of the frame in milliseconds. Default: 100.
webp_duration
webp_dispose - the disposal method for the frame:
webp_dispose
background - restore to the background before displaying the next frame.
background
none - leave the canvas as is when drawing the next frame.
none
Default: background.
webp_blend - the blend method for the frame:
webp_blend
alpha - alpha combine the frame with canvas.
alpha
none - replace the area under the image with the frame.
If the frame has no alpha channel this option makes no difference.
Default: alpha.
If you want more control over the work and parameters libwebp uses to compress your image, you can supply a several other tags, or encapsulate those parameters in a configuration object.
libwebp
At the simplest level you can just supply parameters to $image->write() or Imager->write_multi(), which are then set as tags:
$image->write()
Imager->write_multi()
$im->write(file => "foo.webp", type => "webp", webp_target_size => 10_000);
You can also create a configuration object and supply that:
my $cfg = Imager::File::WEBP::Config->new(webp_target_size => 10_000); $im->write(file => "foo.webp", type => "webp", webp_config => $cfg);
If you do supply a configuration object then any tags supplied are used to modify a copy of the configuration object rather than creating a new configuration for the image. In particular this means that the webp_preset parameter/tag will be ignored if you supply webp_config.
webp_preset
webp_config
You can also initialize the configuration object from an image:
my $cfgim = Imager->new(xsize => 1, ysize => 1); $cfgim->settag(name => "webp_target_size", value => 10_000); my $cfg = Imager::File::WEBP::Config->new($cfgim);
Once you have a configuration object you can fetch or update its members:
my $old_target_size = $cfg->target_size; $cfg->target_size(20_000);
Note that when called as a setter these methods return a success flag, not the old value.
Parameters only available when creating a new configuration object:
webp_preset - a string that can be any of "default", "picture", "photo", "drawing", "icon" or "text". This adjusts the default values for sns_strength, filter_sharpness, filter_strength and preprocessing.
sns_strength
filter_sharpness
filter_strength
preprocessing
Any other value can be supplied as either an initialization parameter (with the webp_ prefix, or modified or retrieved with an accessor on the configuration object (without the webp_ prefix.
webp_
These are:
webp_quality - the quality of conversion, a floating point value between 0 and 100 inclusive.. This is exactly the same as the simpler webp_quality parameter above.
webp_method - quality/speed trade-off (0=fast, 6=slower-better)
webp_method
webp_image_hint - any of default, picture, photo, graph. This is unrelated to webp_preset and it only used for lossless compression at this point. Only graph appears to act any differently at this point going by the source.
webp_image_hint
default
picture
photo
graph
webp_target_size - if non-zero, set the desired target size in bytes. Takes precedence over the webp_quality parameter.
webp_target_size
webp_target_psnr - if non-zero, specifies the minimal distortion to try to achieve. Takes precedence over webp_target_size.
webp_target_psnr
webp_segments - maximum number of segments to use, in [1..4].
webp_segments
webp_sns_strength - Spatial Noise Shaping. Integer 0=off, 100=maximum.
webp_sns_strength
webp_filter_strength - integer range: [0 = off .. 100 = strongest].
webp_filter_strength
webp_filter_sharpness - integer range: [0 = off .. 7 = least sharp].
webp_filter_sharpness
webp_filter_type - filtering type: integer 0 = simple, 1 = strong (only used if filter_strength > 0 or autofilter > 0).
webp_filter_type
webp_autofilter - Auto adjust filter's strength [0 = off, 1 = on].
webp_autofilter
webp_alpha_compression - Algorithm for encoding the alpha plane (0 = none, 1 = compressed with WebP lossless). Default is 1.
webp_alpha_compression
webp_alpha_filtering - Predictive filtering method for alpha plane. 0: none, 1: fast, 2: best. Default is 1.
webp_alpha_filtering
webp_alpha_quality Between 0 (smallest size) and 100 (lossless). Default is 100.
webp_alpha_quality
webp_pass - number of entropy-analysis passes (in [1..10]).
webp_pass
webp_preprocessing - preprocessing filter (0=none, 1=segment-smooth)
webp_preprocessing
webp_partitions - log2(number of token partitions) in [0..3]. Default is set to 0 for easier progressive decoding.
webp_partitions
webp_partition_limit - quality degradation allowed to fit the 512k limit on prediction modes coding (0: no degradation, 100: maximum possible degradation).
webp_partition_limit
webp_emulate_jpeg_size - If true, compression parameters will be remapped to better match the expected output size from JPEG compression. Generally, the output size will be similar but the degradation will be lower. Requires encoder ABI version 0x200 or higher.
webp_emulate_jpeg_size
webp_thread_level - If non-zero, try and use multi-threaded encoding. Requires encoder ABI version 0x201 or higher.
webp_thread_level
webp_low_memory - If set, reduce memory usage (but increase CPU use). Requires encoder ABI version 0x201 or higher.
webp_low_memory
webp_near_lossless - Near lossless encoding [0 = max loss .. 100 = off (default)]. Requires encoder ABI version 0x205 or higher.
webp_near_lossless
webp_exact - if non-zero, preserve the exact RGB values under transparent area. Otherwise, discard this invisible RGB information for better compression. The default value is 0. Must be 0 or 1. Requires encoder ABI version 0x209.
webp_exact
webp_use_sharp_yuv - if needed, use sharp (and slow) RGB->YUV conversion. Requires encoder ABI version 0x20e or higher.
webp_use_sharp_yuv
The above is largely from:
https://developers.google.com/speed/webp/docs/api#advanced_encoding_api
with some typo fixes and adjustments.
To install Imager::File::WEBP you need Imager installed and you need libwebp 0.5.0 or later and the libwebpmux distributed with libwebp.
These aren't intended immediately, but are possible future enhancements.
The simple lossless API doesn't include a compression level parameter, which complicates this. It may not be worth doing anyway.
To fix this I'd probably pull imexif.* out of Imager::File::JPEG and make it part of the Imager API.
Maybe also add extended EXIF/Geotagging via libexif or Exiftool.
I think this is largely done.
Tony Cook <tonyc@cpan.org>
Imager, Imager::Files.
To install Imager::File::WEBP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Imager::File::WEBP
CPAN shell
perl -MCPAN -e shell install Imager::File::WEBP
For more information on module installation, please visit the detailed CPAN module installation guide.