imager icon indicating copy to clipboard operation
imager copied to clipboard

image save optimization

Open tonycoz opened this issue 2 years ago • 1 comments

Imager generally tries to make the output file match the structure of the original image, if the Imager image is direct colour RGB 16-bit/sample, then the image handlers try to produce a 16-bit RGB direct colour file, if the format supports it.

For many uses being able to produce a smaller image is preferred over matching the structure of the image, so we should provide some support for that. Imager itself could include an API for testing whether specific optimizations are possible, based on what the user requests, and what the image file supports. e.g. when writing to PNG it might be good to write a paletted image for an RGB image, while that's pointless to check for with JPEG.

Some possible lossless optimizations:

  • save as greyscale if all of the image is grey
  • save as paletted if the image has a small number of colors
  • for paletted images, eliminate duplicate or unused palette entries
  • if the source image has an alpha channel, but the image is completely opaque

Some possible lossy optimizations:

  • save as grey if all colours are close to grey
  • limit bits saved, e.g. saving a 16-bit/sample image as 8-bit/sample, or 8-bit as 7-bit to improve compression

Image format specific optimizations are outside the the scope of this, but:

  • we already support mozjpeg with Imager::File::JPEG.
  • the various PNG crushers tend to not be packaged as libraries, so to support PNG specific optimizations we'd need to copy (license permitting) code from them.

General image format specific optimizations:

  • remove metadata, including exif data, comments
  • remove image meaning metadata, including color space information, especially icc profiles

tonycoz avatar May 16 '22 23:05 tonycoz

This would be very nice!

For PNG, there are libraries like libimagequant, zopfli/zopflipng, libimageflow and opnglib (created and used by optipng). OptiPNG also provides interesting documentation on how they optimize PNG images.

Hope this helps!

garu avatar May 31 '22 15:05 garu