Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

ImageEnhance is not working on 16 bit images

Open ghoshben opened this issue 1 year ago • 10 comments

What did you do?

Simply wanted to increase contrast

What did you expect to happen?

contrast would increase

What actually happened?

it thrown an error saying image has wrong mode

What are your OS, Python and Pillow versions?

  • OS: windows 11
  • Python: 3.9
  • Pillow: 10.0.0
from PIL import ImageEnhance

im =Image.open('img/13291_20_30_08-22-2023-10-50-05.tif')
print(im.info)
enh = ImageEnhance.Contrast(im)
enh.enhance(1.3).show("30% more contrast")

ghoshben avatar Sep 15 '23 15:09 ghoshben

This is hitting https://github.com/python-pillow/Pillow/blob/fce23ddce8a7e5be7625367678803db0288c3c8e/src/libImaging/Blend.c#L26-L30

Could you upload a copy of the image?

radarhere avatar Sep 15 '23 15:09 radarhere

https://ibb.co/0G8c4sr

here is the sample image

ghoshben avatar Sep 18 '23 08:09 ghoshben

Ok. Be aware that's a PNG file with a palette, rather than a 16-bit TIFF, so that's different.

If you would like an immediate solution, you can convert it to a different mode,

from PIL import Image, ImageEnhance

im = Image.open('in.png').convert('RGB')
enh = ImageEnhance.Contrast(im)
enh.enhance(1.3).show("30% more contrast")

radarhere avatar Sep 18 '23 08:09 radarhere

Currently Pillow doesn't coerce images to required mode for most operations. This is not unique to ImageEnhance.Contrast. The application should resolve this on it's own.

homm avatar Sep 18 '23 09:09 homm

@ghoshben the previous comment suggests that converting is actually the ultimate solution here. Does that resolve this for you?

radarhere avatar Sep 20 '23 05:09 radarhere

my bad that webserver converted that tif to png . here is the link to the file - https://drive.google.com/file/d/16bN5o_1WoRCHElC1sSPDpiZ82GGWwikr/view?usp=sharing

ghoshben avatar Sep 20 '23 06:09 ghoshben

The same solution applies, just with some scaling in the middle to account for the fact that it is an I;16 image and Pillow doesn't scale automatically when converting.

from PIL import Image, ImageEnhance
im = Image.open("in.tif").point(lambda x: x / 256).convert("L")
enh = ImageEnhance.Contrast(im)
enh.enhance(2).show("30% more contrast")

radarhere avatar Sep 20 '23 06:09 radarhere

aah okay. But that will reduce quality right? as we are almost reduction the information by 1/2?

ghoshben avatar Sep 20 '23 06:09 ghoshben

@homm did you have any further thoughts?

radarhere avatar Sep 29 '23 01:09 radarhere

as we are almost reduction the information by 1/2?

Worse, it will reduce amount of information in 256 times. But still, Pillow doesn't have 16-bit-per-channel support in the first place.

homm avatar Sep 29 '23 06:09 homm