-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Allow window id to be passed to ImageGrab.grab() on macOS #9070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Hi. Just for the record, how would a Python user get the window id? |
here is a example script: # pip install pyobjc
# pip install pyobjc-core
# pip install pyobjc-framework-quartz
import Quartz
windows = Quartz.CGWindowListCopyWindowInfo(
Quartz.kCGWindowListExcludeDesktopElements, Quartz.kCGNullWindowID
)
for window in windows:
window_id = window["kCGWindowNumber"]
window_name = window["kCGWindowName"]
window_x = window["kCGWindowBounds"]["X"]
window_y = window["kCGWindowBounds"]["Y"]
window_width = window["kCGWindowBounds"]["Width"]
window_height = window["kCGWindowBounds"]["Height"]
window_owner_name = window["kCGWindowOwnerName"]
window_owner_pid = window["kCGWindowOwnerPID"] user can determine which window id to use there is also a tool: |
src/PIL/ImageGrab.py
Outdated
im_resized = im.resize((right - left, bottom - top)) | ||
im.close() | ||
return im_resized | ||
# manual crop for windowed mode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an interesting problem here.
Originally, we did just crop the image using Pillow. However, that changed in #6152, because #6144 reported that it wasn't accounting for scaling on retina displays.
So if you pick a specific window, how would we know whether it was scaled or not? It might be on a retina display or it might not. If this were Swift, I'd say we want the backingScaleFactor
for the window. I'm not convinced AppleScript could easily get information about a window just from the id.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will look into whether there is any information about Retina in the EXIF data of the generated images.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems no good solution, may be we should just put this on hold.
However, using the -l parameter when capturing a window does have value, as it can create a complete screenshot of an obscured window compared to taking a full screen capture and then cropping it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought of a less elegant method, using -R0,0,100,100 to sample an image and then calculate the ratio, but I cannot ensure that the window obtained with -l and the image sampled with -R are on the same display.
Another option is to behave like the screencapture
command line, ignoring the bbox parameter when passing the window parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created yankeguo#2
One of the screencapture
flags is
-o In window capture mode, do not capture the shadow of the window.
Excluding the shadow changes the size of the window. So if we take two screenshots of a window, we can measure the size of the shadow.
On a retina screen, the shadow is twice as wide as it is on a non-retina screen.
It is possible that a window doesn't have a shadow, but perhaps that can be considered an edge case.
…shot capturing on macOS
Co-authored-by: Andrew Murray <[email protected]>
docs/reference/ImageGrab.rst
Outdated
|
||
:param window: | ||
HWND, to capture a single window. Windows only. | ||
Capture a single window. On Windows, this is a HWND. On macOS, it uses windowid. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's windowid? There's no other mention in the docs or code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've pushed a commit to describe it as a CGWindowID instead.
Co-authored-by: Hugo van Kemenade <[email protected]>
Changes proposed in this pull request:
-l
argument toscreencapture
for macOS, allowing capture a window