Imager::File::WEBP - read and write WEBP files


  use Imager;

  my $img = Imager->new;
    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_mode - set when reading an image and used when writing. Possible values:

    • lossy - write in lossy mode. This is the default.

    • lossless - write in lossless mode.

  • webp_quality - the lossy compression quality, a floating point number from 0 (bad) to 100 (better). Default: 80.

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.

Animation tags

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_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.

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_duration - duration of the frame in milliseconds. Default: 100.

  • webp_dispose - the disposal method for the frame:

    • background - restore to the background before displaying the next frame.

    • none - leave the canvas as is when drawing the next frame.

    Default: background.

  • webp_blend - the blend method for the frame:

    • alpha - alpha combine the frame with canvas.

    • none - replace the area under the image with the frame.

    If the frame has no alpha channel this option makes no difference.

    Default: alpha.

More complex compression configuration

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.

At the simplest level you can just supply parameters to $image->write() or Imager->write_multi(), which are then set as tags:

  $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.

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;

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.

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.

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_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_target_size - if non-zero, set the desired target size in bytes. Takes precedence over the webp_quality parameter.

  • webp_target_psnr - if non-zero, specifies the minimal distortion to try to achieve. Takes precedence over webp_target_size.

  • webp_segments - maximum number of segments to use, in [1..4].

  • webp_sns_strength - Spatial Noise Shaping. Integer 0=off, 100=maximum.

  • webp_filter_strength - integer range: [0 = off .. 100 = strongest].

  • webp_filter_sharpness - integer range: [0 = off .. 7 = least sharp].

  • webp_filter_type - filtering type: integer 0 = simple, 1 = strong (only used if filter_strength > 0 or autofilter > 0).

  • webp_autofilter - Auto adjust filter's strength [0 = off, 1 = on].

  • webp_alpha_compression - Algorithm for encoding the alpha plane (0 = none, 1 = compressed with WebP lossless). Default is 1.

  • webp_alpha_filtering - Predictive filtering method for alpha plane. 0: none, 1: fast, 2: best. Default is 1.

  • webp_alpha_quality Between 0 (smallest size) and 100 (lossless). Default is 100.

  • webp_pass - number of entropy-analysis passes (in [1..10]).

  • webp_preprocessing - preprocessing filter (0=none, 1=segment-smooth)

  • webp_partitions - log2(number of token partitions) in [0..3]. Default is set to 0 for easier progressive decoding.

  • webp_partition_limit - quality degradation allowed to fit the 512k limit on prediction modes coding (0: no degradation, 100: maximum possible degradation).

  • 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_thread_level - If non-zero, try and use multi-threaded encoding. Requires encoder ABI version 0x201 or higher.

  • webp_low_memory - If set, reduce memory usage (but increase CPU use). Requires encoder ABI version 0x201 or higher.

  • webp_near_lossless - Near lossless encoding [0 = max loss .. 100 = off (default)]. Requires encoder ABI version 0x205 or higher.

  • 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_use_sharp_yuv - if needed, use sharp (and slow) RGB->YUV conversion. Requires encoder ABI version 0x20e or higher.

The above is largely from:

with some typo fixes and adjustments.

Imager::File::WEBP::encoder_abi_version() - returns the encoder ABI version.


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.

Compression level support for lossless images

The simple lossless API doesn't include a compression level parameter, which complicates this. It may not be worth doing anyway.

Parse EXIF metadata

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.

Error handling tests (and probably implementation)

I think this is largely done.


Tony Cook <>


Imager, Imager::Files.