Pillow: ImageGrab on mac ( coordinate problem ? )

Created on 5 Aug 2018  路  5Comments  路  Source: python-pillow/Pillow

Hello !
I'm actually watching a tutorial on making a bot with Python using AI M, I want to take a snapshot of a specific area on my screen ( the little flash video game ) :

capture d ecran 2018-08-05 a 21 26 31

so I did this code :

 from PIL import ImageGrab
import os
import time

def screenGrab():
    x = 351
    y = 237
    im = ImageGrab.grab((x,y,x+645,y+484))
    im.save(os.getcwd() + "\\full_snap_" + str(int(time.time()) ) + ".png", "PNG")

if __name__ == '__main__':
    screenGrab() 

Normally the program should be doing this :

capture d ecran 2018-08-05 a 21 14 53

But for whatever reason he took this instead :

sushigoround full_snap_1533497364

As you can see, the coordinates does not correspond to what I've wrote and it looks like he zoom on the screen too.. Weird..

and I use a macbook pro with High Sierra :
capture d ecran 2018-08-05 a 21 33 00

I use :
Pillow 5.2.0
pip 10.0.1
python (from my terminal ) 2.7.10
and Pycharm

Can someone help me please ?? I'm desesperate...
thank you very much !

Most helpful comment

Okay ! I found the solution !
After reading the link you gave me http://pillow.readthedocs.io/en/5.2.x/handbook/concepts.html#coordinate-system
It says :

The Python Imaging Library uses a Cartesian pixel coordinate system

I was taking the coordinate of my screen, and not the pixel coordinate !
the internal mac screen capture ( cmd+shift+4 ), is not taking the pixel coordinate!
Now it works perfectly !

All 5 comments

I can't replicate your problem, but here are some thoughts.

I would suggest running im = ImageGrab(), with no coordinates, and then looking at the resulting image to create the co-ordinates. Internally, ImageGrab.grab works by taking a screenshot of the whole screen, and then running crop - https://github.com/python-pillow/Pillow/blob/master/src/PIL/ImageGrab.py#L48 - https://pillow.readthedocs.io/en/5.2.x/reference/Image.html#PIL.Image.Image.crop

For me, starting with your initial image, the following coordinates produced the desired image -

x = 708
y = 497
im = im.crop((x,y,x+1272,y+960))

Note also that the coordinate system in Pillow starts at the upper left corner - http://pillow.readthedocs.io/en/5.2.x/handbook/concepts.html#coordinate-system

Ok, than you so much for your answer ! So I did im = ImageGrab() with no coordinates, and here's the result :
sushigoround full_snap_1533501286

He screen capture the hole screen.
and for the coordinate system, I originally started with the upper left corner ( 351,237,993,738 ).
If it works with you, I don't really know what to do..

hello !
So, I enter your coordinates into my code and here's what he capture :

sushigoround full_snap_1533530650

from PIL import ImageGrab
import os
import time

def screenGrab():
    x = 708
    y = 497
    im = ImageGrab.grab(bbox=(x,y,x+1272,y+960))
    im.save(os.getcwd() + "\\full_snap_" + str(int(time.time()) ) + ".png", "PNG")

if __name__ == '__main__':
    screenGrab() 

It seems good ! But Why with my coordinate it doesn't work.. ?
I take the coordinate with my mac internal screen capture ( cmd+shift+4 ) is it wrong ?

img_2508

How did you get those coordinate.. ?

Thank you very much!

Okay ! I found the solution !
After reading the link you gave me http://pillow.readthedocs.io/en/5.2.x/handbook/concepts.html#coordinate-system
It says :

The Python Imaging Library uses a Cartesian pixel coordinate system

I was taking the coordinate of my screen, and not the pixel coordinate !
the internal mac screen capture ( cmd+shift+4 ), is not taking the pixel coordinate!
Now it works perfectly !

shift+ctrl+cmd+4

Was this page helpful?
0 / 5 - 0 ratings