简体   繁体   English

Tkinter:将RGB值格式化为字符串

[英]Tkinter: format RGB values into string

I am new to Tkinter (and Python) and I would like to find the most efficient way to format RGB values into a string so it can be used with the PhotoImage.put() function. 我是Tkinter(和Python)的新手,我想找到将RGB值格式化为字符串的最有效方法,以便它可以与PhotoImage.put()函数一起使用。

Let's say I have a Numpy rank 3 array in which the RGB values are stored, the 3rd dimension having a length of 3 for red, green and blue respectively. 假设我有一个存储RGB值的Numpy 3级数组,第3维的红色,绿色和蓝色的长度分别为3。 The most intuitive way to proceed would be: 最直观的处理方式是:

for i in range(0, n_pixels_x):
    for j in range(0, n_pixels_y):
        hexcode = "#%02x%02x%02x" % (array[i,j,0], array[i,j,1], array[i,j,2])
        img.put(hexcode, (j,i))

Unfortunately, this is way too slow for large images. 不幸的是,这对于大图像来说太慢了。

As described in the PhotoImage Wiki , it is possible to pass one large string to put() so the function is called only once. PhotoImage Wiki中所述,可以将一个大字符串传递给put()因此该函数仅被调用一次。 Then, I need to efficiently convert my array into such a string, which should be formatted like this (for a 4x2 image): 然后,我需要将数组有效地转换为这样的字符串,该字符串的格式应如下所示(对于4x2图像):

"{#ff0000 #ff0000 #ff0000 #ff0000} {#ff0000 #ff0000 #ff0000 #ff0000}"

Again, this could easily be done with nested for loops, but I would like to avoid them for efficiency reasons. 同样,可以使用嵌套的for循环轻松完成此操作,但是出于效率考虑,我希望避免使用它们。 Is there any way to use join() in order to do what I want? 有什么方法可以使用join()来做我想要的吗?

If needed, I can store the content of my array differently, the only constraint being that I should be able to modify the color values easily. 如果需要,我可以以其他方式存储数组的内容,唯一的限制是我应该能够轻松修改颜色值。

Edit: After working on this a bit, I found a way to format my values approximately 10 times faster than by using nested loops. 编辑:经过一些努力之后,我发现了一种格式化值的方法比使用嵌套循环快约10倍。 Here is the commented piece of code: 这是注释的代码段:

# 1. Create RGB array
array = np.zeros((n_pixels_x*n_pixels_y, 3))
array = np.asarray(array, dtype = "uint32")
array[1,:] = [0, 100, 255]

# 2. Create a format string
fmt_str = "{" + " ".join(["#%06x"]*n_pixels_x) + "}"
fmt_str = " ".join([fmt_str]*n_pixels_y)

# 3. Convert RGB values to hex
array_hex = (array[:,0]<<16) + (array[:,1]<<8) + array[:,2]

# 4. Format array
img_str = fmt_str % tuple(array_hex)

For a 640x480 array, steps 3 and 4 take ~0.1s to execute on my laptop (evaluated with timeit.default_timer() ). 对于640x480的阵列,第3步和第4步需要约0.1s才能在我的笔记本电脑上执行(用timeit.default_timer()评估)。 Using nested loops, it takes between 0.9s and 1.0s. 使用嵌套循环,它需要0.9s至1.0s。

I would still like to reduce the computation time, but I'm not sure if any improvement is still possible at this point. 我仍然想减少计算时间,但是我不确定在这一点上是否仍有任何改进的可能。

I was able to find another way to format my array, and this really seems to be the quickest solution. 我能够找到另一种格式化数组的方法,这似乎是最快的解决方案。 The solution is to simply use Image and ImageTk to generate an image object directly from the array: 解决方案是简单地使用ImageImageTk直接从数组生成图像对象:

array = np.zeros((height, width, 3), 'uint8')

imageObject = Image.fromarray(array)
img = ImageTk.PhotoImage(image = imageObject, mode = 'RGB'))

This takes approximately 0.02s to run, which is good enough for my needs, and there is no need to use the put() function. 这大约需要0.02s的时间来运行,这足以满足我的需要,并且不需要使用put()函数。

I actually found this answer from another question: How do I convert a numpy array to (and display) an image? 我实际上从另一个问题找到了这个答案: 如何将numpy数组转换为(并显示)图像?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM