Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't apply image filters on 16-bit TIFs in PIL

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?

like image 602
Boris Gorelik Avatar asked Dec 07 '22 18:12

Boris Gorelik


1 Answers

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

like image 119
Hugues Fontenelle Avatar answered Dec 27 '22 07:12

Hugues Fontenelle