Created
October 16, 2011 08:54
-
-
Save masami256/1290679 to your computer and use it in GitHub Desktop.
Take a screenshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from subprocess import Popen, PIPE | |
from optparse import OptionParser | |
import datetime | |
def get_window_id(): | |
print "Please select window which you want to take screen shot." | |
p = Popen("xwininfo", stdout = PIPE) | |
p.wait() | |
stdout = p.communicate() | |
lines = None | |
for s in stdout: | |
if s != None: | |
lines = s.split('\n') | |
break | |
for line in lines: | |
if line.startswith("xwininfo: Window id: "): | |
# print line | |
return line[len("xwininfo: Window id: "):].split(" ")[0] | |
return None | |
def take_screenshot(windowid, filename): | |
""" | |
Make command line like this. | |
import -window [window id or "root"] output | |
""" | |
args = [] | |
args.append("import") | |
args.append("-window") | |
args.append(windowid) | |
args.append(filename) | |
p = Popen(args) | |
p.wait() | |
def get_options(): | |
parser = OptionParser() | |
parser.add_option("-f", "--file", dest="filename", | |
help="PNG file name", metavar="PNG FILE NAME") | |
parser.add_option("-r", "--root", dest="root_window", | |
action="store_true", default=False, | |
help="Take whole windows screen shot") | |
(options, args) = parser.parse_args() | |
return options | |
def get_default_filename(): | |
d = datetime.datetime.today() | |
ret = "%s.png" % d.strftime("%Y%m%d%H%M%S") | |
return ret | |
if __name__ == "__main__": | |
options = get_options() | |
filename = options.filename | |
if filename == None: | |
filename = get_default_filename() | |
if filename.endswith(".png") == False: | |
filename += ".png" | |
if options.root_window == False: | |
windowid = get_window_id() | |
else: | |
windowid = "root" | |
take_screenshot(windowid, filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment