简体   繁体   English

无法在PIL中的16位TIF上应用图像滤波器

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

I try to apply image filters using python's PIL . 我尝试使用python的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. 此代码在PNG,JPG和8位TIF上按预期工作。 However, when I try to apply this code on 16-bit TIFs, I get the following error 但是,当我尝试在16位TIF上应用此代码时,我收到以下错误

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. 请注意,PIL能够在没有抱怨的情况下加载,调整大小并保存16位TIF,因此我假设此问题与过滤器相关。 However, ImageFilter documentation says nothing about 16-bit support 但是, ImageFilter文档没有提到16位支持

Is there any way to solve it? 有什么办法可以解决吗?

Your TIFF image's mode is most likely a "I;16". 您的TIFF图像模式很可能是“I; 16”。 In the current version of ImageFilter, kernels can only be applied to "L" and "RGB" images (see source of ImageFilter.py) 在当前版本的ImageFilter中,内核只能应用于“L”和“RGB”图像(参见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 备注: Python和16 Bit Tiff可能重复

To move ahead, try using ImageMagick , look for PythonMagick hooks to the program. 要继续前进,请尝试使用ImageMagick ,查找程序的PythonMagick挂钩。 On the command prompt, you can use convert.exe image-16.tiff -blur 2x2 output.tiff . 在命令提示符下,您可以使用convert.exe image-16.tiff -blur 2x2 output.tiff Didn't manage to install PythonMagick in my windows OS as the source needs compiling. 没有设法在我的Windows操作系统中安装PythonMagick,因为源需要编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM