Search code examples
pythonpython-imaging-libraryimage-conversion

pillow conversion increases image size


I am using the python Pillow library to do a simple image format conversion.

Here is some simple code demonstrating what I'm doing

im = Image.open("images/filename.tiff", mode="r")
im.save("images/filename.png", optimize=True)

This results in on average, a size increase of 98%. Here is the data on the input tiffs I'm trying to create:

<image mode=1 size=2544x2230>

When I explicitly convert it (by calling .convert(), the size only increases on average by 87%, but still terrible. Why is the size ballooning so much?

Here is what I have tried?

  • using the "quality" parameter on .save() seems to have little effect
  • explicitly passing the "mode" parameter to the save command has little effect
  • using the .convert("L") has little effect on overall size after conversion

EDIT

Here is a sample image that ran through what I described above and resulted in an 87% bigger png file, 907% bigger jpeg, and 907% bigger jpg.

https://share.getcloudapp.com/YEuR9LqP

Version of Pillow is latest: 8.2

Python version: 3.8

Running on MacOS: 10.15.7

Am I missing something obvious or this just my lack of knowledge of how the Image library works?


Solution

  • I believe the issue is that your image is CCITT Group4 Compressed, which is particularly well suited to your image content and has no equivalent in PNG format.

    exiftool image-1.tiff 
    

    Output

    ExifTool Version Number         : 12.00
    File Name                       : image-1.tiff
    Directory                       : .
    File Size                       : 95 kB
    File Modification Date/Time     : 2021:05:17 11:46:57+01:00
    File Access Date/Time           : 2021:05:17 11:57:15+01:00
    File Inode Change Date/Time     : 2021:05:17 11:46:58+01:00
    File Permissions                : rw-r--r--
    File Type                       : TIFF
    File Type Extension             : tif
    MIME Type                       : image/tiff
    Exif Byte Order                 : Little-endian (Intel, II)
    Image Width                     : 2544
    Image Height                    : 2230
    Bits Per Sample                 : 1
    Compression                     : T6/Group 4 Fax       <--- HERE IT IS
    Photometric Interpretation      : WhiteIsZero
    Strip Offsets                   : 180
    Orientation                     : Horizontal (normal)
    Rows Per Strip                  : 2230
    Strip Byte Counts               : 96954
    X Resolution                    : 300
    Y Resolution                    : 300
    Resolution Unit                 : inches
    Image Size                      : 2544x2230
    Megapixels                      : 5.7
    

    On my Mac, I got an output PNG from PIL of size 138,200 bytes, even using pngcrush I could only get down to 124,810 bytes:

    pngcrush -bruteforce result-opt.png crushed.png
      Recompressing IDAT chunks in result-opt.png to crushed.png
      Total length of data found in critical chunks            =    138200
      Best pngcrush method        =   4 (ws 15 fm 0 zl 9 zs 1) =    124816