-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Closed
Description
What did you do?
Saving the int16 image into a PNG format previously used I mode but was changed to I;16 mode, which leads to a silent data loss. The change was done in this pull request.
What did you expect to happen?
Saved int16 images should be loaded as int32 (mode I), as it was before.
What actually happened?
Image was loaded as uint16 and the data was clipped (see the example code bellow).
What are your OS, Python and Pillow versions?
- OS: Windows 10
- Python: 3.13.5
- Pillow: 11.2.1
PIL report:
--------------------------------------------------------------------
Pillow 11.2.1
Python 3.13.5 (tags/v3.13.5:6cb20a2, Jun 11 2025, 16:15:46) [MSC v.1943 64 bit (AMD64)]
--------------------------------------------------------------------
Python executable is C:\Users\factory\AppData\Local\Programs\Python\Python313\python.exe
System Python files loaded from C:\Users\factory\AppData\Local\Programs\Python\Python313
--------------------------------------------------------------------
Python Pillow modules loaded from C:\Users\factory\AppData\Local\Programs\Python\Python313\Lib\site-packages\PIL
Binary Pillow modules loaded from C:\Users\factory\AppData\Local\Programs\Python\Python313\Lib\site-packages\PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 11.2.1
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.13.3
--- LITTLECMS2 support ok, loaded 2.17
--- WEBP support ok, loaded 1.5.0
*** AVIF support not installed
--- JPEG support ok, compiled for libjpeg-turbo 3.1.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.3
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1.zlib-ng, compiled for zlib-ng 2.2.4
--- LIBTIFF support ok, loaded 4.7.0
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------
Example script:
import numpy
import PIL.Image as PilImage
shape = (1024, 1024)
data = numpy.full(shape=shape, fill_value=-5, dtype=numpy.int16)
pil_image = PilImage.fromarray(data)
print("Mode of the image before save:")
print(pil_image.mode)
print("Printing [0, 0] value of the image before save...")
print(data[0, 0])
print("Saving the image...")
path = r"C:\users\factory\desktop\foo.png"
pil_image.save(path)
print("Loading image...")
with PilImage.open(path) as loaded_image:
print("Mode of the image after save:")
print(loaded_image.mode)
print("Printing [0, 0] value of the image before save (mode is I;16 - so uint16 is assumed)...")
raw_data = loaded_image.tobytes()
raw_data_bytes = numpy.frombuffer(raw_data, dtype=numpy.uint16)
loaded_data = numpy.reshape(raw_data_bytes, (pil_image.height, pil_image.width))
print(loaded_data[0, 0])Output:
Mode of the image before save:
I
Printing [0, 0] value of the image before save...
-5
Saving the image...
Loading image...
Mode of the image after save:
I;16
Printing [0, 0] value of the image before save (mode is I;16 - so uint16 is assumed)...
0
Metadata
Metadata
Assignees
Labels
No labels