用Python减去16位TIFF图像。

3
我有两个16位tiff图像,其中一个是背景,我需要从所有图像中将其移除。 我使用以下代码,但是出现以下错误:
返回值错误:图像的模式不正确。
from PIL import Image, ImageChops
    im1 = Image.open("main.tif")
    im2 = Image.open("background.tif")
    
    diff = ImageChops.difference(im2, im1)
    diff.show()

当我使用print(im1.mode)来检查模式时,我得到了:

I,16

我不明白这个错误。同时,我也不知道Pillow是否能够减去16位TIFF图像。我需要帮助来解决这个错误并得到一个减去后的图像。

这两张图片分别是:

主图:主图片

背景图片:背景图片


也许你愿意分享你的TIFF文件 - 可以使用Google Drive或Dropbox等类似工具。 - Mark Setchell
您似乎通过imgur分享了一张JPEG和一张PNG,而不是通过Google Drive分享两个TIFF文件。 - Mark Setchell
我现在已经分享了一个Dropbox链接,希望你能在那里获取到tiff图像。 - vashista
1个回答

2
我认为我会这样做:
#!/usr/bin/env python3

from PIL import Image
import numpy as np

# Open both images and make into Numpy arrays of signed 32-bit integers
main = np.array(Image.open('main.tif')).astype('int32')
back = np.array(Image.open('background.tif')).astype('int32')

# Calculate difference with saturation
diff = np.clip(main - back, 0, main.max())

# Revert to PIL Image and save
Image.fromarray(diff.astype(np.uint16)).save('result.tif')

如果您拉伸对比度,则会得到以下结果:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接