I'm having trouble with saving a 16 bit grayscale TIFF as 16 bit PNG. The mode is set to I;16 by Pillow, which is an outmode as far as I can see: PngImagePlugin.py
Here is a file for testing: 16bit.ome.tif
This code will reproduce the error:
from PIL import Image
img = Image.open('16bit.ome.tif')
img.save('16bit.png')
A more verbose debugging is done here.
Adding the error message:
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-22-bed9526a441a> in <module>()
----> 1 i.save('test.png')
/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/PIL/Image.py in save(self, fp, format, **params)
1680
1681 try:
-> 1682 save_handler(self, fp, filename)
1683 finally:
1684 # do what we can to clean up
/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/PIL/PngImagePlugin.py in _save(im, fp, filename, chunk, check)
676 rawmode, mode = _OUTMODES[mode]
677 except KeyError:
--> 678 raise IOError("cannot write mode %s as PNG" % mode)
679
680 if check:
OSError: cannot write mode I;16 as PNG
convert works without loosing data:
img = img.convert(mode='I')
img.save('16bit.png')
Is this the The Right Way To Do Itâ„¢?
I;16 is not a valid outmode for PNG, as you've found. I mode images are written as I;16B, which is a 16bit big endian PNG. (potentially losing information if you've actually got a 32 bit I image, but ... not important for this operation). Convert's your best bet.
The I;16 family is kind of a weird format, we support it for reading, but it's not really a fully supported format for all image operations where we'd support I.
First I thought that I;16 was only a rawmode, and it was a simple err not setting the correct "Pillow mode".
Have it been discussed to implicit convert between modes if the conversion will give no dataloss?
Please try Stack Overflow if you don't get an answer here.
I did use convert. But I guess this should be documented (might already be and I've missed it). https://github.com/arve0/leicaexperiment/blob/master/leicaexperiment/experiment.py#L429-431
Close issue if this is a not-fix?
Thanks, added Documentation label and closing.