user7065967
user7065967

Reputation:

Get RGB value from screen pixels with python

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?

Upvotes: 5

Views: 14454

Answers (2)

Kakutstha Awasthi
Kakutstha Awasthi

Reputation: 1

you should use the pyscreeze module

import pyscreeze
#the value of pixel whose value is to be get
x=23
y=23

#screen object
screen=pyscreeze.screenshot()

rgb_values=screen.getpixel((x,y))

Upvotes: 0

Sergio
Sergio

Reputation: 864

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.

Upvotes: 5

Related Questions