我一直在试着截取屏幕的截图,并找到特定颜色的每个x,y坐标。
from PIL import ImageGrab
import numpy as np
image = ImageGrab.grab()
indices = np.all(image == (209, 219, 221), axis=-1)
print(indices)
print(zip(indices[0], indices[1]))
当我运行我的代码时,我收到一个坐标,然后是一个错误消息。
(1126, 555)
[1126, 555]
False
print(zip(indices[0], indices[1]))
IndexError: invalid index to scalar variable.
为什么它不工作?颜色显示在屏幕上
我相信你在下面这行写错了:
indices = np.all(image == (209, 219, 221), axis=-1)
您可以直接迭代像素并获得您想要的结果:
from PIL import ImageGrab
import numpy as np
image = ImageGrab.grab()
color = (43, 43, 43)
indices = []
width, height = image.size
for x in range(width):
for y in range(height):
if image.getpixel((x, y)) == color:
indices.append((x, y))
print(indices)