Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageEnhance is not working on 16 bit images #7397

Open
ghoshben opened this issue Sep 15, 2023 · 10 comments
Open

ImageEnhance is not working on 16 bit images #7397

ghoshben opened this issue Sep 15, 2023 · 10 comments

Comments

@ghoshben
Copy link

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")
@radarhere
Copy link
Member

radarhere commented Sep 15, 2023

This is hitting

if (!imIn1 || !imIn2 || imIn1->type != IMAGING_TYPE_UINT8 || imIn1->palette ||
strcmp(imIn1->mode, "1") == 0 || imIn2->palette ||
strcmp(imIn2->mode, "1") == 0) {
return ImagingError_ModeError();
}

Could you upload a copy of the image?

@radarhere radarhere changed the title ImageEnhance is not working on 16 bit tiff image ImageEnhance is not working on 16 bit image Sep 16, 2023
@ghoshben
Copy link
Author

https://ibb.co/0G8c4sr

here is the sample image

@radarhere
Copy link
Member

radarhere commented Sep 18, 2023

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")

@homm
Copy link
Member

homm commented Sep 18, 2023

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.

@radarhere
Copy link
Member

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

@ghoshben
Copy link
Author

ghoshben commented Sep 20, 2023

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

@radarhere
Copy link
Member

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")

@ghoshben
Copy link
Author

ghoshben commented Sep 20, 2023

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

@radarhere
Copy link
Member

@homm did you have any further thoughts?

@homm
Copy link
Member

homm commented Sep 29, 2023

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.

@radarhere radarhere changed the title ImageEnhance is not working on 16 bit image ImageEnhance is not working on 16 bit images Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants