使用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]
),我的结果也是随机的,我做错了什么?你知道吗
谢谢你!你知道吗
附言:我可以毫无问题地做out = ImageTk.PhotoImage(start)
。你知道吗
您需要指定数组的
dtype
为np.uint8
:如果在PIL对数组使用
.tobytes()
方法时不指定此项,那么它将获取对"RGB"
没有意义的数据,因为它是针对浮点数的。你知道吗另请注意,
fromarray
逐行获取数据,因此高度需要是数组的第一个维度,您只需在将其传递给fromarray
之前获取transpose
即可解决此问题:相关问题 更多 >
编程相关推荐