[英]Increasing speed performance of a pixel color checker function in Python
I am writing a python function that checks a region of the windows desktop environment for a certain pixel color, and if this color meets a certain criteria, then python returns true. 我正在编写一个python函数,该函数检查Windows桌面环境中某个像素颜色的区域,如果该颜色符合特定条件,则python返回true。
The problem is, optimally I need this pixel check to be occurring at a timing frequency of 100ms or less. 问题是,最理想的情况是我需要以100ms或更小的定时频率进行像素检查。 I have done a very crude method of measuring timing performance, and it appears I am getting a "refresh rate" of no better than ~250milliseconds.
我已经完成了一种非常粗略的计时性能测量方法,看来我得到的“刷新率”不超过约250毫秒。
Is there any possible way to improve the performance of this script and/or more accurately measure timing? 有什么可能的方法来改善此脚本的性能和/或更准确地测量计时?
Code attached below: 随附的代码如下:
import win32gui
import time
def get_pixel_colour(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)
def main():
for x in range(20):
t1 = time.time()
x = get_pixel_colour(25, 1024)
y = str(x)
if y == "(255, 255, 255)":
print "True"
else:
print "Not True"
t2 = time.time()
print t2 - t1 #To calculate time
main()
No need to split the RGB components. 无需拆分RGB组件。 That takes some time.
那需要一些时间。 And then the 3 conversion for those components from int to string take much more time.
然后,这些组件从int到string的3转换要花费更多的时间。 Just return the pixel value as is
照原样返回像素值
def get_pixel_colour(i_x, i_y):
i_desktop_window_id = win32gui.GetDesktopWindow()
i_desktop_window_dc = win32gui.GetWindowDC(i_desktop_window_id)
return win32gui.GetPixel(i_desktop_window_dc, i_x, i_y)
Then in main just compare the RGB value directly 然后主要是直接比较RGB值
for x in range(20):
t1 = time.time()
if get_pixel_colour(25, 1024) == 0xFFFFFF
print "True"
else:
print "Not True"
t2 = time.time()
print t2 - t1 #To calculate time
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.