python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.1k stars 2.21k forks source link

ImageFilter does not work with I;16 #6667

Open amdepott opened 1 year ago

amdepott commented 1 year ago

What did you do?

Attempted to use Image.filter(ImageFilter.BLUR) on an image of mode I or I;16

What did you expect to happen?

Image is blurred

What actually happened?

Code throws ValueError

What are your OS, Python and Pillow versions?

>>> from tkinter import filedialog
>>> from PIL import Image, ImageFilter
>>> image1 = Image.open(filedialog.askopenfilename())
>>> image1
   <PIL.BmpImagePlugin.BmpImageFile image mode=RGB size=630x514 at 0x28B7051E9B0>
>>> image2 = image1.convert('I')
>>> image2
   <PIL.Image.Image image mode=I size=630x514 at 0x28B70A8E560>
>>> image1.filter(ImageFilter.BLUR).show()
>>> image2.filter(ImageFilter.BLUR).show()
   Traceback (most recent call last):
     File "<pyshell#56>", line 1, in <module>
       image2.filter(ImageFilter.BLUR).show()
     File "C:\Users\x\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py", line 1247, in filter
       return self._new(filter.filter(self.im))
     File "C:\Users\x\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\ImageFilter.py", line 32, in filter
       return image.filter(*self.filterargs)
   ValueError: image has wrong mode
radarhere commented 1 year ago

Just an explanation from the code. https://github.com/python-pillow/Pillow/blob/1f5be894b91a00731ca328b3352eaf69eaee766c/src/libImaging/Filter.c#L326-L332 means that it doesn't support I, I;*, F or BGR;*.

radarhere commented 1 year ago

PR #7108 has been merged, fixing this for I mode.

GrayMask commented 8 months ago

Hi! When I use ImageFilter to performs a look-up table (LUT) transform on an image of mode I or I;16,

File ".../PIL/ImageFilter.py", line 560, in filter
    return image.color_lut_3d(
ValueError: image has wrong mode

still occurred.

I found that I mode has been supported for BuiltinFilter but still not supported for Color3DLUT. Related code: https://github.com/python-pillow/Pillow/blob/main/src/libImaging/ColorLUT.c#L89-L92

Would mode I be supported for Color3DLUT in the future?

radarhere commented 8 months ago

Screenshot 2024-01-10 at 8 10 24 pm

I don't see how a function that "transforms 3-channel pixels" applies to a 1 channel image.

Could you describe in more detail what you are trying to do?

GrayMask commented 8 months ago

Thanks for your reply.

I'm sorry, I misunderstood. I thought mode I supported 3-channel 32-bit images, however it only supports 1-channel.

I am trying to perform a look-up table (LUT) transform on heif images which are in 10-bit & 3-channel. Now I find that Pillow only supports 8-bit color depth for 3-channel images (<- Am I right?), so the only way is to convert the 10-bit images to 8-bit in advance, even though it will reduce the amount of color information.

radarhere commented 8 months ago

Now I find that Pillow only supports 8-bit color depth for 3-channel images

Yes, see #1888

so the only way is to convert the 10-bit images to 8-bit in advance, even though it will reduce the amount of color information.

If your goal is to use the final image with Pillow, then yes. If your goal is to use the final image with something else, then I suppose you would split 10-bit 3-channel images to 3 10-bit 1-channel images in advance, then use Pillow to handle the LUT, and then merge the images again outside of Pillow.

GrayMask commented 8 months ago

If your goal is to use the final image with something else, then I suppose you would split 10-bit 3-channel images to 3 10-bit 1-channel images in advance, then use Pillow to handle the LUT, and then merge the images again outside of Pillow.

Oh! Thanks for your suggestion!

homm commented 8 months ago

split 10-bit 3-channel images to 3 10-bit 1-channel images in advance, then use Pillow to handle the LUT

Again, this wouldn't 3D LUT, this will be three 1D LUTs.

amdepott commented 2 weeks ago

Still does not work on I;16 Also does not work with gaussian blur filters