PIL.ImageChops.subtract 和首先转换为 numpy 数组然后减去它们之间有什么区别?

What is the difference between PIL.ImageChops.subtract and first converting to numpy arrays and then subtract those?

所以我想减去Image2 from Image1 using Pillow. I first tried to do this by converting them to numpy arrays, subtract those and then converting the arrays back to Images. But this gave me very strange behavior s.t. large parts of the difference picture就变成了白色。 我发现我可以直接使用 PIL.IamgeChops.subtract 和 works fine 做同样的事情,但我想了解为什么数组方式不起作用。
这是我的代码:

import os.path
from PIL import Image, ImageShow, ImageChops

root = os.path.abspath(os.path.curdir)

Image1 = Image.open(
    root + "\Messwerte\Messungen_20_11\AOM_Copy\1.bmp"
).convert("L")

Image2 = Image.open(
    root + "\Messwerte\Messungen_20_11\B Feld Abh\hintergrund_B_3.bmp"
).convert("L")

DiffImage = ImageChops.subtract(Image1, Image2)  #this way works perfectly
DiffImage.show()
DiffImageArray = np.array(DiffImage)

arr1 = np.array(Image1)
arr2 = np.array(Image2)
diff = arr1 - arr2   #this doesn't work
Image.fromarray(diff).show()

print(np.max(DiffImageArray)) #this gives me 77
print(np.max(diff))  # and this 255

我也检查了错误不在于从数组到图像对象的转换。

这是因为 unit8 中的图像类型以及当您进行减法运算时,任何负值都会被圈回至 255,因为数据格式无法处理负整数。也就是说,7-8 将存储为 255 而不是 -1,7-9 将存储为 254 而不是 -2 等等。您将需要更大的数据类型,并且还需要取减法结果的绝对值(或根据用例将负值裁剪为零),而不是简单的减法。

你可以简单地解决这个问题

arr1 = np.array(Image1).astype(np.int32)
arr2 = np.array(Image2).astype(np.int32)
diff = np.abs(arr1 - arr2)

创建数组时。