How To Take Screenshot In Mac Os X From Inside Of Python: Aka Command-control-shift-4
Solution 1:
There seems to be a standard utility called screencapture
on the Mac, you could use subprocess
to call that, like so:
from subprocess import callcall(["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
Solution 2:
/Users/MYNAME/
should be existent for example to work:
import subprocess
defrunAppleScript(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)
Solution 3:
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.
Solution 4:
I find these these commands work for me on my Mac OS X
from subprocess import callcall(["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.
Solution 5:
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.
Post a Comment for "How To Take Screenshot In Mac Os X From Inside Of Python: Aka Command-control-shift-4"