我尝试使用 python 的 PIL 应用图像过滤器。代码很简单:

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

此代码在 PNG、JPG 和 8 位 TIF 上按预期工作。但是,当我尝试在 16 位 TIF 上应用此代码时,出现以下错误
ValueError: image has wrong mode

请注意,PIL 能够加载、调整大小和保存 16 位 TIF 而不会有任何提示,所以我认为这个问题与过滤器有关。但是,ImageFilter documentation 没有提及 16 位支持

有什么办法解决吗?

最佳答案

您的 TIFF 图像的模式很可能是“I;16”。
在当前版本的ImageFilter中,内核只能应用于
“L”和“RGB”图像(参见 ImageFilter.py 的来源)

尝试先转换为另一种模式:

im.convert('L')

如果失败,请尝试:
im.mode = 'I'
im = im.point(lambda i:i*(1./256)).convert('L').filter(ImageFilter.BLUR)

备注:可能与 Python and 16 Bit Tiff 重复

关于python - 无法在 PIL 中的 16 位 TIF 上应用图像过滤器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8062564/

10-16 13:10