在Python或C ++中获得像素颜色的最快方法? [英] Fastest way to get pixel color in Python or C++?
问题描述
我正在尝试向正在制作的透明Tkinter小部件中添加模糊效果.
I'm trying to add a blur effect to a transparent Tkinter widget I'm making.
我使用此行(代码段)使小部件部分透明
I made the widget partially transparent with this line (snippet)
self.attributes("-alpha", 0.85)
要添加效果,我需要获取小部件中每个像素的RGB值.看到小部件只是部分不透明,我不能使用 .cget 方法,因为它返回它的填充RGB值(将其变为部分透明之前的颜色),而不是它的实际RGB值).
In order to add the effect I desire I need to get the RGB values of each individual pixel in the widget. Seeing as the widget is only partially opaque I can not use the .cget method because it returns it's fill RGB value (the color before it's made partially transparent), not it's actual RGB value).
我当前正在使用此功能(摘要)
I currently am using this function (snippet)
def get_pixel_colour(self, i_x, i_y):
i_desktop_window_id = win32gui.GetDesktopWindow()
i_desktop_window_dc = win32gui.GetWindowDC(i_desktop_window_id)
long_colour = win32gui.GetPixel(i_desktop_window_dc, i_x, i_y)
i_colour = int(long_colour)
return (i_colour & 0xff), ((i_colour >> 8) & 0xff), ((i_colour >> 16) & 0xff)
但是,对于我来说,太远太慢了.有没有更快的方法来获得像素颜色?
However, it is far too slow for my purposes. Is there any faster way to get pixel color?
如果Python为此目的太慢,我也可以使用C ++. 我只需要获取不同x,y坐标下像素的RGB值.
If Python is just too slow for this purpose, I can also use C++. I just need to get the RGB values of pixels at different x, y coordinates.
推荐答案
您不妨看一看Python Image Library,您可以将其命名为
You may wish to take a look at the Python Image Library you could call something like this
import Image
im = Image.open("name_of_file.jpg")
list_of_pixels = list(im.getdata())
print list_of_pixels[0]
这将以RGB格式输出第一个像素,例如(255,255,255) 它相当快,但是我相信它永远不会击败C ++
That would output the very first pixel in a RGB format like (255,255,255) It is pretty fast but it wont ever beat C++ I believe
这篇关于在Python或C ++中获得像素颜色的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!