将C或Numpy数组转换为具有最小副本数的Tkinter PhotoImage [英] Convert a C or numpy array to a Tkinter PhotoImage with a minimum number of copies

查看:41
本文介绍了将C或Numpy数组转换为具有最小副本数的Tkinter PhotoImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道通过Tkinter将MxNx3 Numpy数组显示为RGB图像的秘诀,但我的秘诀在此过程中制作了数组的几个副本:

a = np.random.randint(low=255, size=(100, 100, 3), dtype=np.uint8) # Original
ppm_header = b'P6
%i %i
255
'%(a.shape[0], a.shape[1])
a_bytes = a.tobytes() # First copy
ppm_bytes = ppm_header + a_bytes # Second copy https://en.wikipedia.org/wiki/Netpbm_format
root = tk.Tk()
img = tk.PhotoImage(data=ppm_bytes) # Third and fourth copies?
canvas = tk.Canvas(root, width=a.shape[0], height=a.shape[1])
canvas.pack()
canvas.create_image(0, 0, anchor=tk.NW, image=img) # Fifth copy?
root.mainloop()

如何才能用最少的份数达到同等效果?

理想情况下,我会创建一个numpy数组,它是TkinterPhotoImage对象使用的相同字节的视图,有效地为我提供了一个像素值可变的PhotoImage,并使更新Tkinter显示变得又便宜又快。我不知道如何从Tkinter中提取此指针。

也许可以通过ctype实现,如hinted at here

PhotoImage.put()方法似乎很慢,但可能我错了,这就是前进的道路吗?

我尝试制作包含ppm头和图像像素值的bytearray(),然后使用numpy.frombuffer()将图像像素值作为数值数组查看,但我认为PhotoImage构造函数需要的是bytes()对象,而不是bytearray()对象,而且我认为Tkinter将其data输入的字节复制为其内部格式(32位RGBA?)。我想这比上面的食谱省了我一份?

推荐答案

我可以使用pil和标签将其减少到1(可能2)个副本:

import numpy as np
import tkinter as tk
from PIL import Image, ImageTk

a = np.random.randint(low=255, size=(100, 100, 3), dtype=np.uint8) # Original
root = tk.Tk()
img = ImageTk.PhotoImage(Image.fromarray(a)) # First and maybe second copy.
lbl = tk.Label(root, image=img)
lbl.pack()
root.mainloop()

然而,这仍然不是可变的。如果你想这样做,我认为你需要自己在画布上放置一个像素来重新创造一个图像。我用this project做过一次,发现更新速度最快的是matplotlib动画,它非常适合您,因为您已经在使用NP数组了。

我使用tk.Canvas、aPIL Image(使用putPixel())和matplotlib的代码。

这篇关于将C或Numpy数组转换为具有最小副本数的Tkinter PhotoImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆