尽快比较像素

问题描述 投票:0回答:1

我正在尝试使用 python 在屏幕上找到准确的 RGB 并尽快比较它们。 该代码使用 PIL ImageGrab 来获取屏幕上的像素。能不能再快一点?

from PIL import Image, ImageGrab
px = ImageGrab.grab().load()

for y in range(pointY, rangeY, 1):
    for x in range(pointX, rangeX, 1):
        color = px[x, y] # Screen pixel
        img = pix[0,0] # First pixel of my image

        if img[0] != color[0] or img[1] != color[1] or img[2] != color[2]:
            continue
        else:
            # Compare the whole image
python image performance pixel
1个回答
0
投票

您可以使用numpy来比较颜色。

这将返回一个屏幕大小的 2D 数组,其中每次出现的颜色都标记为 True。

import numpy as np
from PIL import Image, ImageGrab
screen_grap = ImageGrab.grab().load()
my_color = np.asarray(pix[0,0])

#returns 2D array 
result = np.all(screen_grap==my_color, axis=-1)

如果您只是想检查它是否包含颜色,那么就这样做

# returns just true if screen contains your color
result = np.any(screen_grap==my_color, axis=-1)

如果你想获得匹配的像素坐标,你可以这样做

# returns coordinates of the pixels that match
result = np.argwhere(np.all(screen_grap==my_color, axis=-1))
© www.soinside.com 2019 - 2024. All rights reserved.