Skip to main content
 首页 » 编程设计

python之如何从 image.pixel 中找到像素位置

2024年11月24日12pander-it

所以,我有这个有效的脚本,它打印出所有具有和 rgb 值 (102,102,102) 的像素,但我不知道我现在如何能够获得该像素位置并单击它......任何建议?

编辑:像素位置是指像素 x,y 坐标

import pyautogui 
import time 
from PIL import Image 
import mss 
import mss.tools 
import cv2 
import numpy as np 
from PIL import ImageGrab 
import colorsys 
 
 
time.sleep(3) 
 
 
def shootfunc(xc, yc): 
    pyautogui.click(xc, yc) 
 
gameregion = [71, 378, 328, 530] 
 
foundpxl = 0 
 
xx = 0 
while xx <= 300: 
    with mss.mss() as sct: 
        region = {'top': 0, 'left': 0, 'width': 1920, 'height': 1080} 
        imgg = sct.grab(region) 
        pxls = imgg.pixels 
 
 
        for pxl in pxls: 
            for pxll in pxl: 
                if pxll == (102, 102, 102) or pxl == "(255, 255, 255)" or pxl == [255, 255, 255]: 
                    foundpxl = pxll 
                    print(foundpxl) 
        xx = xx + 1 
        time.sleep(.1) 

请您参考如下方法:

您可以 enumerate您迭代的任何序列。这将返回元素和元素的索引:

>>> for i, e in enumerate('abc'): 
...     print(i, e) 
0 a 
1 b 
2 c 

因此,您可以利用它来查找像素的行和列:

for row, pxl in enumerate(pxls): 
    for col, pxll in enumerate(pxl): 
        ...