尝试编写代码,将两张照片转换为数组,将它们加在一起,然后除以 2,然后用数组制作一张照片

问题描述 投票:0回答:1
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行

仅供参考

  1. 出于某种原因,Stackflow 不允许我将其格式化为代码。 2.我是 python 和 numpy 的新手
python numpy python-imaging-library photo
1个回答
0
投票

改变就这么简单

final_product = output.astype(int)

final_product = output.astype(np.uint8)

np.uint8
是来自图像的数组的正确
dtype
。当您对数组进行除法操作时,
dtype
会更改为
float64

您可以像这样检查 numpy 数组的

dtype

print(arr.dtype)

干杯!

© www.soinside.com 2019 - 2024. All rights reserved.