Pillow
Pillow copied to clipboard
ImageEnhance is not working on 16 bit images
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")
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?
https://ibb.co/0G8c4sr
here is the sample image
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")
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.
@ghoshben the previous comment suggests that converting is actually the ultimate solution here. Does that resolve this for you?
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
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")
aah okay. But that will reduce quality right? as we are almost reduction the information by 1/2?
@homm did you have any further thoughts?
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.