Skip to content
Menu
Poppy Ramblings Poppy Ramblings
  • About
Poppy Ramblings Poppy Ramblings

Quick screenshots in Python

Posted on 2024-01-022024-01-02 by herbcso

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 methodLibraryTime 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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Shoutout for a nifty blog
  • Quick screenshots in Python
  • Getting IP address on very limited Docker containers
  • CIFS mount input/output error -5 with reading SOME directories
  • Avoid collisions in ~/bin

Recent Comments

  • herbcso on Debugging bash functions
  • herbcso on Everything you (n?)ever wanted to know about Unicode
  • herbcso on SpongeBob SquarePants in Buckaroo Banzai!?
  • mcshankins on SpongeBob SquarePants in Buckaroo Banzai!?
  • herbcso on PHP ZendDebugger and WAMP

Tags

ag AJAX bash blog cifs command-line cool debugging disk diun download firefox fix frameworks gdb homeassistant javascript packaging php plex portainer proxmox python ruby running samba search shoes silliness space SquarePants tcx therubyracer tips tricks tv unicode unix useful v8 WAMP weird windows xdebug zend

Archives

  • September 2024
  • January 2024
  • August 2023
  • September 2022
  • June 2022
  • March 2022
  • January 2022
  • May 2019
  • March 2019
  • April 2014
  • February 2014
  • October 2013
  • June 2013
  • April 2013
  • December 2012
  • November 2012
  • June 2012
  • April 2012
  • September 2011
  • October 2010
  • September 2010
  • August 2010
  • June 2010
  • May 2010
  • April 2010
  • October 2009
  • August 2008
  • June 2008
  • April 2008

Categories

  • android
  • browsers
  • coding
  • homelab
  • random nifty stuff
  • silly
  • site updates
  • Uncategorized

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2025 Poppy Ramblings | Powered by Superb Themes