将图像转换为oth

2024-06-25 05:27:16 发布

您现在位置:Python中文网/ 问答频道 /正文

使用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

附言:我可以毫无问题地做out = ImageTk.PhotoImage(start)。你知道吗


Tags: in图像imagefornprange矩阵像素
1条回答
网友
1楼 · 发布于 2024-06-25 05:27:16

您需要指定数组的dtypenp.uint8

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

如果在PIL对数组使用.tobytes()方法时不指定此项,那么它将获取对"RGB"没有意义的数据,因为它是针对浮点数的。你知道吗

另请注意,fromarray逐行获取数据,因此高度需要是数组的第一个维度,您只需在将其传递给fromarray之前获取transpose即可解决此问题:

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

相关问题 更多 >