Python 索引器:图像索引超出范围?

Python 索引器:图像索引超出范围?,python,numpy,winapi,python-imaging-library,Python,Numpy,Winapi,Python Imaging Library,我试图制作一个屏幕颜色检测器,所以基本上你可以告诉它你想要找到的颜色值。但我遇到的错误是索引器错误:图像索引超出范围 我的代码: from PIL import ImageGrab from win32api import GetSystemMetrics import numpy as np def Color(R,G,B): px = ImageGrab.grab().load() x = GetSystemMetrics(0) y = GetSyst

我试图制作一个屏幕颜色检测器,所以基本上你可以告诉它你想要找到的颜色值。但我遇到的错误是
索引器错误:图像索引超出范围

我的代码:

from PIL import ImageGrab
from win32api import GetSystemMetrics
import numpy as np


def Color(R,G,B):

    px = ImageGrab.grab().load()
    
    x = GetSystemMetrics(0)
    y = GetSystemMetrics(1)  

    color = px[x, y]

    if color is R and G and B:
        print("Found The color you want!")
                
        
Color(25, 35, 45) #Telling it the RGB value of what i want to find

有更好的方法吗?

您得到了错误,因为
x
y
在基于零的系统中基本上是长度。至少你需要
color=px[x-1,y-1]
。。。如果您想检查屏幕上的最后一个像素

也许你想做更多类似的事情

from PIL import ImageGrab
from win32api import GetSystemMetrics
from dataclasses import dataclass

@dataclass
class Point:
    x :int
    y :int
    
    def __repr__(self):
        return f'x={self.x}, y={self.y}'


def ColorPositions(R,G,B):
    px   = ImageGrab.grab().load()
    x, y = GetSystemMetrics(0), GetSystemMetrics(1) 
    return [Point(n%x, n//x) for n in range(x*y) if px[n%x, n//x]==(R, G, B)]
        

positions = ColorPositions(99, 99, 99)
print(*positions[0:10], sep="\n")

之所以会出现错误,是因为
x
y
在基于零的系统中基本上是长度。至少你需要
color=px[x-1,y-1]
。。。如果您想检查屏幕上的最后一个像素

也许你想做更多类似的事情

from PIL import ImageGrab
from win32api import GetSystemMetrics
from dataclasses import dataclass

@dataclass
class Point:
    x :int
    y :int
    
    def __repr__(self):
        return f'x={self.x}, y={self.y}'


def ColorPositions(R,G,B):
    px   = ImageGrab.grab().load()
    x, y = GetSystemMetrics(0), GetSystemMetrics(1) 
    return [Point(n%x, n//x) for n in range(x*y) if px[n%x, n//x]==(R, G, B)]
        

positions = ColorPositions(99, 99, 99)
print(*positions[0:10], sep="\n")

在我看来,我编辑我的答案是为了更有用。在我看来,我编辑我的答案是为了更有用。