使用python从屏幕像素中获取RGB值

5 rgb pixel python-imaging-library python-3.x

要在 Python 中使用坐标 (x,y)从屏幕像素中获取 RGB 值,我执行以下操作:

import PIL.ImageGrab
rgb = PIL.ImageGrab.grab().load()[x,y]
Run Code Online (Sandbox Code Playgroud)

它按我的预期工作,直到我做到了:

rgb = PIL.ImageGrab.grab().load()[1673,0]
Run Code Online (Sandbox Code Playgroud)

我收到的不是像素的 RGB 值,而是:

IndexError: image index out of range
Run Code Online (Sandbox Code Playgroud)

我不明白为什么,因为我的屏幕分辨率为 1920x1080。

我怎样才能解决这个问题?

Ser*_*gio 5

如果你这样做:

import PIL.ImageGrab
PIL.ImageGrab.grab().size
Run Code Online (Sandbox Code Playgroud)

您将看到 python 在您的系统中检测到的分辨率。如果您使用的是 Windows,则必须转到屏幕配置,然后将:文本、应用程序和其他元素大小更改为 100%。

如果您的分辨率是 1920x1080,正如您所说,这应该可以解决问题。