1

I am using ImageGrab from PIL to get the RGB value of specific pixels on my screen. I am having trouble understanding where I set the x and y coordinates for it get the pixels from. Here is mode code so far:

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()

I tried changing the range values for x and y but this changed how it got printed out. I am a beginner and I am sorry for the stupid question but I have been at this for quite long and can't figure it out. Currently it just gets the pixel from the top left of my screen. I would like to capture the single middle pixel on my 1920x1080 screen.

Thanks.

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
Mihkel
  • 689
  • 7
  • 34
  • Both your loops are actually not loops. your `[x,y]` is simply `[0,0]`, which would be the top left corner of your screen. Try `pxcolor = pixel[960, 540]`. – John Anderson Jan 24 '19 at 18:00

1 Answers1

1

Untested, but something like this where you specify a bounding box just 1x1 pixel:

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

which will give you this if you inspect it:

<PIL.Image.Image image mode=RGBA size=1x1 at 0x120668748>
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432