You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
16-bit integer TIFF fails to load as expected #5248
edit: the actual issue might have to do with 16-bit-int TIFF loading; see next comment. At first I thought that the issue was application of the color profile, but now I think probably not.
What did you do?
I am trying to work with this image (55 MiB TIFF) in Pillow. It's an astronomical image of a galaxy.
What did you expect to happen?
I want to convert the image to sRGB. If I do the transformation using GIMP, most of the image comes out close to black as you'd expect:
# before running code: convert image to PNG in GIMP, save as `gimp-good.png`importnumpyasnpfromPILimportImage, ImageCmsimg_cnv=Image.open('gimp-good.png')
arr_cnv=np.asarray(img_cnv)
print(arr_cnv[200,200]) # corners of image are full black<<< [731]
What actually happened?
Doing the conversion myself, nothing happens, and the resulting values are very wrong:
importnumpyasnpfromPILimportImage, ImageCmsfromioimportBytesIOimg_orig=Image.open('gemini1210a.tif')
img_orig=img_orig.convert('RGB') # image reads as RGBX mode, but X channel is all 255s; seems to be unrelatedarr_orig=np.asarray(img_orig)
in_prof=ImageCms.getOpenProfile(BytesIO(img_orig.info['icc_profile']))
out_prof=ImageCms.createProfile('sRGB')
xform=ImageCms.buildTransform(in_prof, out_prof, 'RGB', 'RGB')
img_xform=ImageCms.applyTransform(img_orig, xform)
arr_xform=np.asarray(img_xform)
print(arr_orig[200,200], arr_xform[200,200])
<<< [13210120] [13210120]
I'm not an expert in this stuff but I've experimented with various mode conversions, rendering intents, etc. and can't figure out a way to get the profile transform to actually do anything.
What are your OS, Python and Pillow versions?
OS: Linux, Fedora 33
Python: 3.8.6 (conda-forge)
Pillow: 8.1.0 (conda-forge)
Side note
The image's profile is labeled as being "sRGB IEC61966-2.1", so I am surprised that the values that GIMP obtains are so far off from the ones in the raw image as read by Pillow. Maybe an indication that there's a data format issue?
Oh, of course, after filing one makes progress. GIMP reports that the image is in 16-bit integer format. The 4.3.0 release notes say:
"Pillow now can read 16-bit multichannel TIFF files including files with alpha transparency. The image data is truncated to 8-bit precision."
For my sample pixel at x,y = 200,200, GIMP reports RGB = 1924, 738, 376 = 0x0784, 0x02E2, 0x0178, which truncates to (about) the 7, 3, 1 as found by GIMP. It's not obvious to me where the Pillow values (132, 10, 120 = 0x84, 0x0A, 0x78) are coming from although you can see overlaps in the hex representations.
@radarhere I've had cases where I've had comparable differences between what I've seen in an image viewer and what I've gotten out of Pillow, and the ultimate culprit has been color profiles, but yes, now I think the issue is the handling of the 16-bit integer data.
For my sample pixel at x,y = 200,200, GIMP reports RGB = 1924, 738, 376 = 0x0784, 0x02E2, 0x0178, which truncates to (about) the 7, 3, 1 as found by GIMP. It's not obvious to me where the Pillow values (132, 10, 120 = 0x84, 0x0A, 0x78) are coming from although you can see overlaps in the hex representations.
Well it's pretty clear this is just truncation and just the LSB is taken by Pillow. I've tested loading via imageio and the three possible backends: Pillow, FreeImage, and default tifffile:
FI/tifffile return a uint16 array and for your pixel at (200,200) the value ['0x0784', '0x030a', '0x0178']
Pillow returns uint8 and ['0x84', '0x0a', '0x78'], i.e. exactly just the LSB.
@kmilos I had the G channel being 738 = 0x02E2, not ending in 0x0A. Probably I just made a typo above, though — I remember that I had at least one other one.
Activity
pkgw commentedon Feb 3, 2021
Oh, of course, after filing one makes progress. GIMP reports that the image is in 16-bit integer format. The 4.3.0 release notes say:
"Pillow now can read 16-bit multichannel TIFF files including files with alpha transparency. The image data is truncated to 8-bit precision."
For my sample pixel at x,y = 200,200, GIMP reports RGB = 1924, 738, 376 = 0x0784, 0x02E2, 0x0178, which truncates to (about) the 7, 3, 1 as found by GIMP. It's not obvious to me where the Pillow values (132, 10, 120 = 0x84, 0x0A, 0x78) are coming from although you can see overlaps in the hex representations.
radarhere commentedon Feb 4, 2021
If I take your image (converted to a smaller PNG for display purposes)

and run
then the result is

So, perhaps in line with your second comment, your problem is more fundamental than image profiles, yes?
pkgw commentedon Feb 4, 2021
@radarhere I've had cases where I've had comparable differences between what I've seen in an image viewer and what I've gotten out of Pillow, and the ultimate culprit has been color profiles, but yes, now I think the issue is the handling of the 16-bit integer data.
[-]Image's color profile fails to apply[/-][+]16-bit integer TIFF fails to load as expected[/+]kmilos commentedon Feb 5, 2021
Well it's pretty clear this is just truncation and just the LSB is taken by Pillow. I've tested loading via
imageioand the three possible backends: Pillow, FreeImage, and default tifffile:FI/tifffile return a uint16 array and for your pixel at (200,200) the value ['0x0784', '0x030a', '0x0178']
Pillow returns uint8 and ['0x84', '0x0a', '0x78'], i.e. exactly just the LSB.
pkgw commentedon Feb 5, 2021
@kmilos I had the G channel being 738 = 0x02E2, not ending in 0x0A. Probably I just made a typo above, though — I remember that I had at least one other one.
radarhere commentedon Jan 17, 2022
Testing, this image now loads correctly in Pillow 8.2.0 and later, thanks to #5364.
gives
[ 7 3 1 255].