4

I try to apply image filters using python's PIL. The code is straight forward:

im = Image.open(fnImage)
im = im.filter(ImageFilter.BLUR)

This code works as expected on PNGs, JPGs and on 8-bit TIFs. However, when I try to apply this code on 16-bit TIFs, I get the following error

ValueError: image has wrong mode

Note that PIL was able to load, resize and save 16-bit TIFs without complains, so I assume that this problem is filter-related. However, ImageFilter documentation says nothing about 16-bit support

Is there any way to solve it?

Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
  • Not very helpful, but I'd wager the blur filter just doesn't support it. Not even Photoshop supports every operation in 16-bit mode (sadly.) – Skurmedel Nov 09 '11 at 09:08
  • @Skurmedel ImageFilter documentation says nothing about 16-bit support, which makes me think that this is a solvable problem – Boris Gorelik Nov 09 '11 at 09:41

2 Answers2

16

Your TIFF image's mode is most likely a "I;16". In the current version of ImageFilter, kernels can only be applied to "L" and "RGB" images (see source of ImageFilter.py)

Try converting first to another mode:

im.convert('L')

If it fails, try:

im.mode = 'I'
im = im.point(lambda i:i*(1./256)).convert('L').filter(ImageFilter.BLUR)

Remark: Possible duplicate from Python and 16 Bit Tiff

Community
  • 1
  • 1
Hugues Fontenelle
  • 5,275
  • 2
  • 29
  • 44
  • 1
    If you run into this problem when trying to do logical operators from `ImageChops` then you should use `im.convert("1")`. – Nick Larsen Feb 01 '17 at 22:07
  • 5
    In case it's not obvious: note that `L` and `I` modes are both 8-bit modes (ref: http://pillow.readthedocs.io/en/latest/handbook/concepts.html#modes) and so the code shown here will reduce the depth of your image from 16 bit to 8 bit. – Mark Amery Feb 20 '18 at 22:41
0

To move ahead, try using ImageMagick, look for PythonMagick hooks to the program. On the command prompt, you can use convert.exe image-16.tiff -blur 2x2 output.tiff. Didn't manage to install PythonMagick in my windows OS as the source needs compiling.

Alvin K.
  • 4,329
  • 20
  • 25