在 Windows 7 上用 Python 快速获取屏幕上某些像素的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3800458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Quickly getting the color of some pixels on the screen in Python on Windows 7
提问by dln385
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:
我需要获取屏幕上或活动窗口中某些像素的颜色,而且我需要快速完成。我试过使用 win32gui 和 ctypes/windll,但它们太慢了。这些程序中的每一个都获得 100 像素的颜色:
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())
and
和
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())
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?
每一个都需要大约 1.75 秒。我需要一个这样的程序来花费不到 0.1 秒。是什么让它这么慢?
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.
我正在使用 Python 3.x 和 Windows 7。如果您的解决方案需要我使用 Python 2.x,请将我链接到一篇文章,该文章展示了如何同时安装 Python 3.x 和 2.x。我看了看,但不知道如何做到这一点。
Thanks!
谢谢!
采纳答案by Margus
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.
我遇到了同样的问题,并解决了它(在 Java 中,在 C# 中)。该解决方案背后的主要思想是屏幕上的 GetPixel 速度很慢,而且您无法解决这个问题。但是当你需要一些像素时,你可以一次得到一堆。
The time that it took to get 64 pixels was 98 times faster.
获得 64 像素所需的时间快了 98 倍。
回答by dln385
Thanks to Margus' direction, I focused on getting the image before extracting the pixel information. Here's a workable solution using the Python Imaging Library (PIL), which requires Python 2.x.
感谢 Margus 的指导,我在提取像素信息之前专注于获取图像。这是使用Python Imaging Library (PIL)的可行解决方案,它需要 Python 2.x。
import ImageGrab
import time
time.clock()
image = ImageGrab.grab()
for y in range(0, 100, 10):
for x in range(0, 100, 10):
color = image.getpixel((x, y))
print(time.clock())
I don't think it gets any simpler than that. This takes (on average) 0.1 seconds, which is a little slower than I'd like but fast enough.
我认为没有比这更简单的了。这需要(平均)0.1 秒,这比我想要的要慢一点,但足够快。
As for having Python 3.x and 2.x both installed, I separated that into a new question. I'm still having some trouble with it, but it's generally working.
至于同时安装 Python 3.x 和 2.x,我把它分成了一个新问题。我仍然遇到一些问题,但它通常可以正常工作。
回答by Oleh Prypin
This is better than using getpixelall the time and works faster.
这比一直使用要好getpixel,而且工作得更快。
import ImageGrab
px=ImageGrab.grab().load()
for y in range(0,100,10):
for x in range(0,100,10):
color=px[x,y]
Reference: Image.load
参考:Image.load
回答by Jaakko
Disabling Windows Desktop Composition speeds pixel up reading A LOT.
禁用Windows桌面组合加快了像素读了很多。
Computer -> Properties -> Advanced system settings -> Performance -> desktop composition [ ](warning this disables Windows's transparency effects)
计算机 -> 属性 -> 高级系统设置 -> 性能 -> 桌面组合 [ ](警告这会禁用 Windows 的透明效果)
Python 2.7 (Should be same for 3.x)
Python 2.7(3.x 应该相同)
win32gui.GetPixel() #1.75s => 20ms
windll.gdi32.GetPixel() #1.75s => 3ms (fastest)
image.getpixel() # 0.1s => 50ms
px[] # 0.1s => 50ms
AutoIt for comparison
AutoIt 进行比较
$timer = TimerInit()
For $x = 0 To 100 Step 10
For $y = 0 To 100 Step 10
PixelGetColor($x,$y) ;slow => 1ms
Next
Next
ConsoleWrite("Time: " & TimerDiff($timer)/1000 & @CRLF)

