How to take Screenshot in Mac OS X from inside of Python: aka Command-Control-Shift-4

StackOverflow https://stackoverflow.com/questions/23667794

  •  22-07-2023
  •  | 
  •  

Pregunta

With Command-Control-Shift-4 the screenshot area can be taken/then saved to a clipboard. I wonder if same would be possible from inside of Python. Ideally an image file format could be specified as well as to where to save it all programmatically within Python. Any ideas?

edited later 1

I've located applescript that does almost what's needed except this script captures an entire screen. I would like to limit it to a region instead.

tell application "System Events"
    keystroke (ASCII character 36) using {command down}
    delay 1
    keystroke space
end tell

edited later 2

The apple script below does a region screen capture quite well (there is still no control on to where exactly the captured image is saved (it is saved by default to the user's Desktop).

tell application "System Events"
    keystroke (ASCII character 36) using {command down}
    key code 4 using {command down, shift down}
end tell

The question is how to customize the output file path location...

¿Fue útil?

Solución

There seems to be a standard utility called screencapture on the Mac, you could use subprocess to call that, like so:

from subprocess import call
call(["screencapture", "screenshot.jpg"])

There are options to screencapture to specify a specific rectangle as well:

$ screencapture -h
. . . 
  -R<x,y,w,h> capture screen rect

Otros consejos

Mac users who experience difficulty getting these functions to work - namely, when you screen capture it only shows the desktop wallpaper - the reason is that you need to give permissions to relevant software (e.g. Terminal, PyCharm CE, etc.) you are using for the screen capture commands as found in System Preferences > Security & Privacy > Privacy > Screen Recording.

/Users/MYNAME/ should be existent for example to work:

import subprocess

def runAppleScript(appleScript=None):   
    AppleProcess=subprocess.Popen(['osascript', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    AppleProcess.communicate(appleScript)[0]

appleScript="""on run
    set shellCommand to "/usr/sbin/screencapture -i \\"" & "/Users/MYNAME/Pictures/Screenshot.png\\""
    do shell script shellCommand
end run"""

runAppleScript(appleScript)

Following works well for me just by importing from selenium import webdriver:

self.now = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
self.driver.save_screenshot('login-%s.png' % self.now)

It creates files in the same directory from where script was executed with time stamp.

I find these these commands work for me on my Mac OS X

from subprocess import call

call(["screencapture", "screenshot.jpg"])   # Captures full screen
call(["screencapture", "-R 100,100,500,500", "screenshot.jpg"]) # Captures area of screen

The first call captures a full screen. The second call captures a section of screen. This screenshot.jpg is saved automatically in the directory the python code runs.

You can use PyObjC to access Quartz Window Services, which can be used to create screenshots.

screencapture -h works perfectly for me.

$ screencapture -h . . . -R<x,y,w,h> capture screen rect

$ screencapture -R100,100,500,500 asdf.png

This line save a partial screenshot 400x400 pixels wide with file name asdf on the directory where i run the command.

Thanks oldgeeksguide

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top