【问题标题】:Set Where ImageGrab Gets Pixel Color From设置 ImageGrab 从何处获取像素颜色
【发布时间】:2020-10-27 18:12:40
【问题描述】:

我正在使用 PIL 的 ImageGrab 来获取屏幕上特定像素的 RGB 值。我无法理解我在哪里设置 x 和 y 坐标,因为它从哪里获取像素。到目前为止,这是模式代码:

from PIL import ImageGrab
import threading

def getcol():
    global pxcolor
    threading.Timer(0.5, getcol).start()
    pixel=ImageGrab.grab().load()
    for y in range(0,1,1):
        for x in range(0,1,1):
            pxcolor=pixel[x,y]
            print(pxcolor)

getcol()

我尝试更改 xyrange 值,但这改变了它的打印方式。我是初学者,很抱歉这个愚蠢的问题,但我已经研究了很长时间,无法弄清楚。目前它只是从我的屏幕左上角获取像素。我想在我的 1920x1080 屏幕上捕捉单个中间像素。

谢谢。

【问题讨论】:

  • 你的两个循环实际上都不是循环。您的[x,y] 就是[0,0],这将是您屏幕的左上角。试试pxcolor = pixel[960, 540]

标签: python python-imaging-library coordinates imagegrab


【解决方案1】:

未经测试,但是像这样指定 bounding box 仅 1x1 像素:

pixel=ImageGrab.grab((960,540,961,541)) 

如果你检查它会给你这个:

<PIL.Image.Image image mode=RGBA size=1x1 at 0x120668748>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多