在Windows 7上的Python中快速获取屏幕上某些像素的颜色 [英] Quickly getting the color of some pixels on the screen in Python on Windows 7

查看:888
本文介绍了在Windows 7上的Python中快速获取屏幕上某些像素的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取屏幕上或活动窗口中某些像素的颜色,并且需要 迅速 .我试过使用win32gui和ctypes/windll,但是它们太慢了.这些程序的每个颜色均为100像素:

I need to get the color of some pixels on the screen or from the active window, and I need to do so quickly. I've tried using win32gui and ctypes/windll, but they're much too slow. Each of these programs gets the color of 100 pixels:

import win32gui
import time
time.clock()
for y in range(0, 100, 10):
    for x in range(0, 100, 10):
        color = win32gui.GetPixel(win32gui.GetDC(win32gui.GetActiveWindow()), x , y)
print(time.clock())

from ctypes import windll
import time
time.clock()
hdc = windll.user32.GetDC(0)
for y in range(0, 100, 10):
    for x in range(0, 100, 10):
        color = windll.gdi32.GetPixel(hdc, x, y)
print(time.clock())

每一个过程大约需要1.75秒.我需要一个这样的程序,需要不到0.1秒的时间.是什么让它这么慢?

Each of these takes about 1.75 seconds. I need a program like this to take less than 0.1 seconds. What's making it so slow?

我正在使用Python 3.x和Windows7.如果您的解决方案要求我使用Python 2.x,请将我链接到一篇文章,该文章显示如何同时安装Python 3.x和2.x.我看了一下,但不知道该怎么做.

I'm working with Python 3.x and Windows 7. If your solution requires I use Python 2.x, please link me to an article showing how to have Python 3.x and 2.x both installed. I looked, but couldn't figure out how to do this.

谢谢!

推荐答案

我也遇到了同样的问题,并解决了这个问题(在C#中).解决方案背后的主要思想是屏幕上的GetPixel运行缓慢,您无法解决该问题.但是,由于需要一些像素,您可以一次获得一大堆像素.

I had this same exact problem, and solved it (in Java, in C#). The main idea behind the solution is GetPixel from screen is slow, and you can't fix that. But as you need some pixels, you can get a bunch of them all at once.

获取64个像素所需的时间快了98倍.

The time that it took to get 64 pixels was 98 times faster.

这篇关于在Windows 7上的Python中快速获取屏幕上某些像素的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆