Fastest way to get pixel color in Python or C++?

纵然是瞬间 提交于 2020-02-08 05:14:32

问题


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)

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?

Edit:

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.


回答1:


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]

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



来源:https://stackoverflow.com/questions/4836925/fastest-way-to-get-pixel-color-in-python-or-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!