Get RGB value from screen pixels with python

江枫思渺然 提交于 2019-12-09 01:02:23

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/42636933/get-rgb-value-from-screen-pixels-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!