Skip to content

Improve OSX screenshot performance by immediately screenshot given region#82

Open
kkoomen wants to merge 1 commit intoasweigart:masterfrom
kkoomen:master
Open

Improve OSX screenshot performance by immediately screenshot given region#82
kkoomen wants to merge 1 commit intoasweigart:masterfrom
kkoomen:master

Conversation

@kkoomen
Copy link
Copy Markdown

@kkoomen kkoomen commented Aug 15, 2022

Hi,

I was using pyautogui which is using this module and I noticed that on my 4K monitor it took a long time for a small region to be captured, especially my script, which takes multiple screenshots in a row. The performance decrease really stacks up here. This was because the whole screen was captured and then cropped, rather than an immediate screenshot of the given region.

Here's the improvement on my 4K monitor:

Full screen capture:

$ time screencapture -x screen.png

real	0m0.220s
user	0m0.164s
sys	0m0.016s

Screenshot region at (100, 100) with a width/height of 500/400:

$ time screencapture -x -R 100,100,500,400 region.png

real	0m0.047s
user	0m0.017s
sys	0m0.007s

As you see, the original implementation would take 0.22s while the new implementation can take ±0.05s, roughly 4 times faster.

It is also a lot smaller when screenshotting specific regions:

$ ls -l
-rw-r--r--@ 1 koomen  staff   3.4M Aug 15 10:25 screen.png
-rw-r--r--@ 1 koomen  staff   200K Aug 15 10:25 region.png

P.s. I also noticed your # TODO - use tmp name for this file. comment, which I also resolved.

@Avasam
Copy link
Copy Markdown

Avasam commented Aug 21, 2024

@kkoomen This now has conflicts

@kkoomen
Copy link
Copy Markdown
Author

kkoomen commented Aug 21, 2024

It didn't have conflicts when I made this. If you want this to be merged, you can solve the conflict yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants