import numpy as np
from PIL import Image
image1 = Image.open(
"img1")
image2 = Image.open(
"img2")
array1 = np.asarray(image1)
array2 = np.asarray(image2)
add = np.add(array1, array2)
divi = 2
output = np.divide(add, divi)
final_product = output.astype(int)
Image.fromarray(final_product).save("my.img.numpy.png")
每当我运行此程序时,我都会收到错误
类型错误:无法处理此数据类型:(1, 1, 3),
问题出现在第29行
仅供参考
改变就这么简单
final_product = output.astype(int)
到
final_product = output.astype(np.uint8)
np.uint8
是来自图像的数组的正确 dtype
。当您对数组进行除法操作时,dtype
会更改为 float64
您可以像这样检查 numpy 数组的
dtype
:
print(arr.dtype)
干杯!