This will be very anecdotal and specific to my setup, but here are some recent findings I had on my M2 Pro Mac Mini running MacOS Sonoma 14.1.2 and how long it takes to grab a screenshot with Python via various methods.
Screenshot method | Library | Time taken in ms |
pyautogui.screenshot() | pyautogui | ~1300-1500 |
ImageGrab.grab() | pillow | ~1300-1500 |
mss.grab() | MSS | ~45-65 |
Yep, you’re reading that right – MSS is ~30x faster for me than the other 2 methods!
Turns out that pyautogui
and pillow
use the screencapture
CLI command under the hood (pyautogui
actually does this in the pyscreeze
library, see here, and pillow
does it here) and write the screenshot to a file and then read it back out of that. Before finding MSS, I was all confident and said to myself “surely I can improve on that by using the clipboard instead of writing to a silly file!” – famous last words™. So still running the screencapture
CLI command but having it write to the clipboard using the -c
option and then using ImageGrab.grabclipboard()
from the pillow library. Going that route ended up taking me ~4500ms to grab a screenshot – 3x WORSE than before! Turns out THAT method delegates to osascript
on the Mac (see here), which now makes sense that it would be horrendously slow…
MSS however uses the CoreGraphics
native Mac library under the hood (see here), so that makes it WAY faster. At least it does on my system.
It’s interesting to me since several SO posts around this seem to suggest that pyautogui and pillow’s method should be faster, but for me, at least, that was definitely not the case! They probably are faster on Windows or Linux since those use different methods to grab the screenshots, but on Mac they’re not so great speed-wise. Functionally they’re totally fine, just too slow for my use case.
Don’t get me wrong, I’m not knocking pyautogui
and pillow
, they’re two great libraries, I’m just saying for my particular use case they didn’t serve my specific purpose.
Anyway, just thought I’d share that here.