我有两个16位tiff图像,其中一个是背景,我必须将其从所有图像中删除.我使用以下代码,但是我得到的错误是

return image1._new(image1.im.chop_difference(image2.im))
ValueError: image has wrong mode

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)判断模式时,我得到

一、 16

我不理解这个错误.此外,我不知道枕头是否能够减go 16位tiff图像.我需要帮助来解决这个错误并得到一个减影图像.

这两个图像是

背景图像:background

推荐答案

我想我会这样做:

#!/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

Python相关问答推荐

具有多个选项的计数_匹配

Pandas 在最近的日期合并,考虑到破产

替换字符串中的多个重叠子字符串

ModuleNotFound错误:没有名为flags.State的模块; flags不是包

什么相当于pytorch中的numpy累积ufunc

计算组中唯一值的数量

数据抓取失败:寻求帮助

如何创建一个缓冲区周围的一行与manim?

将JSON对象转换为Dataframe

如何指定列数据类型

Python Tkinter为特定样式调整所有ttkbootstrap或ttk Button填充的大小,适用于所有主题

如何排除prefecture_related中查询集为空的实例?

将标签移动到matplotlib饼图中楔形块的开始处

为什么调用函数的值和次数不同,递归在代码中是如何工作的?

Python日志(log)库如何有效地获取lineno和funcName?

在round函数中使用列值

时长超过24小时如何从Excel导入时长数据

在Pandas 中以十六进制显示/打印列?

是否将Pandas 数据帧标题/标题以纯文本格式转换为字符串输出?

Scipy.linprog的可行性有问题吗?(A_ub@x0<;=b_ub).all()为True-但是-linprog(np.zeros_like(X0),A_ub=A_ub,b_ub=b_ub)不可行