简体   繁体   English

在Python或C ++中获得像素颜色的最快方法?

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

I'm trying to add a blur effect to a transparent Tkinter widget I'm making. 我正在尝试为正在制作的透明Tkinter小部件添加模糊效果。

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. 为了添加我想要的效果,我需要获得小部件中每个单独像素的RGB值。 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). 看到小部件只是部分不透明,我不能使用.cget方法,因为它返回它的填充RGB值(将其变为部分透明之前的颜色),而不是它的实际RGB值)。

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++. 如果Python为此目的太慢,我也可以使用C ++。 I just need to get the RGB values of pixels at different x, y coordinates. 我只需要获取不同x,y坐标下像素的RGB值。

You may wish to take a look at the Python Image Library you could call something like this 您可能希望看一下Python图像库,您可以这样称呼它

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 这将以(255,255,255)的RGB格式输出第一个像素,虽然速度非常快,但我相信它永远不会击败C ++

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

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