Search code examples
imageimagemagickimagemagick-convert

Encoding PNG image with specific filter type using Imagemagick


Just trying to re-encode a PNG file with a specific filtering method using image magick command line tool ('convert' tool to be specific).

As per this page , we can set the quality parameter using two decimal figures , first figure being the zlib compression level used when 1-9 is used and the second figure indicates filter method to use (which is used before the compression)

But with the above option used always filter type '0' (i.e no filter) is used when encoding , even when I specify filter type other than '0'.

eg with below command

convert -quality 11 in.png out.png

I should get output image with zlib compression level 1 (which I am not bothered about) and filter type '1' ('sub' type of filtering).

But the out image is always with filter type '0' , even if I change quality level to 11 , 12 ..15 etc (corresponding to filters '1' , '2' ...'5' etc).

Also tried with controls as per this page , but results are same.

Helps are appreciated!


Solution

  • The "filter method" in the IHDR chunk of any PNG file is always 0. What ImageMagick/GraphicsMagick's "quality" digit controls is the filter type used in each scanline. So "-quality 11" will produce a PNG file that has filter_method=0 in IHDR, and "filter_type=1" as the first byte of each row.

    If you have "pngtest" that comes with libpng, you can find out which filter types are used:

    pngtest -m out.png
    

    yields for example

    PASS (3396 zero samples)
    Filter 1 was used 24 times
    Filter 2 was used 478 times
    Filter 3 was used 2 times
    Filter 4 was used 241 times
    libpng passes test
    

    EDIT: There does seem to have been a fleeting problem with ImageMagick. Under version 7.0.2-9, "-quality 11" is producing an image with all rows having filter_type 0 as you noted. "-quality 15" does work properly, with all four filter_types being present. GraphicsMagick, ImageMagick-7.0.3-0, and ImageMagick-6.9.5-9 are working properly.