Search code examples
pythonpython-imaging-librarypng

Pillow's save method ignores info parameters


I want to read, manipulate then save a png image while keeping its original info properties mainly gamma. I'm using Pillow version 9.0.1

Code from SO mentions it can be done like this:

from PIL import Image
img = Image.open("sample.png") # Sample image provided below code block
info = img.info
img.save("output.png", **info)

Sample image

But info does not carry over; the images no longer look alike due to the loss of the gamma info and further evident using the test:

# Test
output_info = Image.open("output.png").info
print(info)                  # {'gamma': 2147.48365}
print(output_info)           # {}
print(output_info == info)   # False, should be True

The question: Why does PIL's Image.save not write gamma?

It's not specific to gamma, no other chunk get written like chromaticity and text.


sample.png compared to output.png (as viewed using a gAMA-aware viewer, like chromium) original.png on the left, output.png on the right


Solution

  • Looks like my expectations were wrong. PIL's PNG file writer does not support a gamma keyword argument therefore it is being silently ignored. Exactly as documented:

    The save() method supports the following options:

    • Image.save ignores unrecognized keyword arguments:

    Keyword options can be used to provide additional instructions to the writer. If a writer doesn’t recognise an option, it is silently ignored.