gpt4 book ai didi

python - 将图像转换为其他图像

转载 作者:太空宇宙 更新时间:2023-11-03 16:32:34 25 4
gpt4 key购买 nike

使用 PIL 和 tkinter,我尝试在图像的像素矩阵中进行一些操作,例如:

start = Image.open("gua.jpg")
sta = start.load()
i,j = start.size
current = np.zeros((i,j,3))

for ix in range(i):
for jx in range(j):
current[ix,jx] = [elem*0.5 for elem in sta[ix,jx]]

current = np.asarray(current)
current = Image.fromarray(current, "RGB")
out = ImageTk.PhotoImage(current)

panel.configure(image = out)
panel.image = out

但即使我只是将信息从图像的像素矩阵传递到我的矩阵(current[ix,jx] = sta[ix,jx]),我的结果是随机的,我在做什么错了?

谢谢!

enter image description here

P.S:我可以毫无问题地执行out = ImageTk.PhotoImage(start)

最佳答案

您需要指定数组的dtypenp.uint8:

current = np.zeros((i,j,3),dtype=np.uint8)

如果当 PIL 在数组上使用 .tobytes() 方法时没有指定此项,它会获取对 "RGB" 没有意义的数据,因为它是对于 float 。

另请注意,fromarray 逐行获取数据,因此高度需要是数组的第一个维度,您可以通过简单地在之前进行转置 来解决此问题将其传递给 fromarray:

img = Image.fromarray(current.transpose(1,0,2))

关于python - 将图像转换为其他图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37485310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com