Repository navigation
Labels
Description
What did you do?
Used image grab on Mac OS, and the region input seems to not handle anything above a certain y1 value
What did you expect to happen?
I expect the region of the ImageGrab.grab() to return an image of the screen of the region specified, but it throws an error
What actually happened?
It threw an rect (836.0, 982.0, 500.0, 218.0) does not intersect any displays error
What are your OS, Python and Pillow versions?
- OS: Mac OS 13.4, M1 Pro
- Python: 3.11.1
- Pillow: 10.1.0
This code takes an image of the region [836, i, 1336, 1200]. where i is incremented upward until i=982, where it throws an error. The result of print(image1.size) is (3024, 1964) BTW. Not sure what is going on here, please reach out if you have further questions.
from PIL import ImageGrab
image1 = ImageGrab.grab()
print(image1.size)
for i in range(950, 1050):
image2 = ImageGrab.grab([836, i, 1336, 1200])
print(i)
print(image2.size)output:
973
(500, 227)
974
(500, 226)
975
(500, 225)
976
(500, 224)
977
(500, 223)
978
(500, 222)
979
(500, 221)
980
(500, 220)
981
(500, 219)
rect (836.0, 982.0, 500.0, 218.0) does not intersect any displays
Traceback (most recent call last):
File "/Users/jacobhagberg/Documents/PythonCode/EveOnlineAutopilot/QuickTest.py", line 7, in <module>
image2 = ImageGrab.grab([836, i, 1336, 1200])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/PIL/ImageGrab.py", line 38, in grab
im = Image.open(filepath)
^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/PIL/Image.py", line 3305, in open
raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file '/var/folders/2t/fyymv8314fn56nytzjvtq93r0000gn/T/tmpxroi8hx0.png'
Activity
[-]ImageGrab.grab() reigon issue[/-][+]ImageGrab.grab() region issue[/+]radarhere commentedon Jan 2, 2024
Your script runs fine on my macOS machine without a problem.
Internally, Pillow is just calling
screencaptureon macOS. So if you runscreencapture -R 836,982,500,219 -x out.pngin your Terminal, does the same "rect (836.0, 982.0, 500.0, 218.0) does not intersect any displays" error appear?JacobHagberg commentedon Jan 2, 2024
When I run "screencapture -R 836,982,1336,1200 -x out.png" same as I did with the problematic one in the python script, had the same error. 836,982,500,219 was not the region I was trying to capture, it was 836,982,1336,1200. The result is: rect (836.0, 982.0, 1336.0, 1200.0) does not intersect any displays
radarhere commentedon Jan 2, 2024
So, on the one hand, we have macOS's
screencaptureerror. I'm able to replicate the error if I sufficiently increase the height. I don't think there's anything to fix there as such - it looks like you're trying to take a screenshot of an area outside of your monitor.On the other hand, let's talk about
ImageGrab.grab()'s box argument.Pillow receives co-ordinate boxes as
(x1, y1, x2, y2).man screencapturewill tell you thatscreencapture's-Rflag can "Capture rectangle using format x,y,width,height". So that is why the error message is different, as it is coming fromscreencapture.Pillow is aware of this, and adjusts the co-ordinates to this format.
JacobHagberg commentedon Jan 2, 2024
I think I figured it out, when I go into the command shift 4 screenshot region thing on mac os, the screenshot has a y max of 981. The cords for the screen capture are half of what they are for the image resolution of a screenshot on my computer, I thought that the image grab region was x1, y1, x2, y2 of the image resolution, but it is of the mouse coordinates instead. I just misunderstood the coordinate system here, my mistake. Might be something to clarify in the documentation.
radarhere commentedon Jan 2, 2024
I've created #7678 to update the documentation. You can see a preview of it at https://pillow--7678.org.readthedocs.build/en/7678/reference/ImageGrab.html#PIL.ImageGrab.grab
JacobHagberg commentedon Jan 2, 2024
Looks good, that is much clearer.
JacobHagberg commentedon Jan 2, 2024
Wait, I read the code here:
Pillow/src/PIL/ImageGrab.py
Line 43 in ded2c8b
When I ran the code:
[0,0,1512, 982] being the full-screen box, image1 size was (3024, 1964), while image2 size was (1512, 982). Not sure if this is intended.
radarhere commentedon Jan 2, 2024
If you consider this from the perspective of the rest of Pillow, such as
im.crop(box=...), I think it is more expected to have the rectangle co-ordinates produce an image that has dimensions(x2 - x1, y2 - y1).I've created #7688 for this.
JacobHagberg commentedon Jan 3, 2024
I guess this makes sense so, cropping after the grab would be the method used to get a portion of the screen without quality reduction.
lifei commentedon Oct 9, 2024
when the process is running under super user permission, the system blocks pillow to grab image every time even after clicking allow one month.
radarhere commentedon Oct 9, 2024
I'm not able to replicate your problem on macOS Sequoia. If you'd like to discuss your problem further, please open a new issue with more detail.