python读取图片rgb值,使用python从屏幕像素获取RGB值

To get the RGB values from a pixel of the screen with coordinates (x,y) in Python, I do:

import PIL.ImageGrab

rgb = PIL.ImageGrab.grab().load()[x,y]

It was working as I expected until I did:

rgb = PIL.ImageGrab.grab().load()[1673,0]

Instead of the RGB values of the pixel, I received:

IndexError: image index out of range

I don't understand why because my screen has 1920x1080 resolution.

How can I fix this?

解决方案

If you do:

import PIL.ImageGrab

PIL.ImageGrab.grab().size

You will see the resolution that python is detecting in your system. If you are using Windows, you have to go to Screen Configuration and then change: text, aplications and another elements size to 100%.

If your resolution is 1920x1080 ,as you said, this should fix the problem.

你可能感兴趣的:(python读取图片rgb值)