Reputation: 19329
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?
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
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...
Upvotes: 4
Views: 9041
Reputation: 11
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.
Upvotes: 1
Reputation: 1
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.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 18
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
Upvotes: -1
Reputation: 19329
/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)
Upvotes: 0
Reputation: 129001
You can use PyObjC to access Quartz Window Services, which can be used to create screenshots.
Upvotes: -1
Reputation: 2918
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
Upvotes: 9