Skip to content

Instantly share code, notes, and snippets.

@joedicastro
Last active December 30, 2016 08:11
Show Gist options
  • Save joedicastro/456e98d3663dbf1ee319 to your computer and use it in GitHub Desktop.
Save joedicastro/456e98d3663dbf1ee319 to your computer and use it in GitHub Desktop.
Capture successive screen shots of a given screen region
#!/usr/bin/env python2
"""
shots.py: capture successive screen shots of a given screen region
"""
__author__ = "joe di castro <joe@joedicastro.com>"
__license__ = "MIT"
__date__ = "2015-03-01"
__version__ = "0.1"
try:
import os
import re
import sys
import time
import pyscreenshot as ImageGrab
from argparse import ArgumentParser
except ImportError:
# Checks the installation of the necessary python modules
print((os.linesep * 2).join(["An error found importing one module:",
str(sys.exc_info()[1]), "You need to install it", "Stopping..."]))
sys.exit(-2)
def arguments():
"""Defines the command line arguments for the script."""
main_desc = """Capture successive screen shots of a given screen region"""
parser = ArgumentParser(description=main_desc)
parser.add_argument("-p", "--path", dest="path",
default=os.path.expanduser('~/animations'), nargs='?',
help="The path to store the captures. If none "
"is given, takes the ~/animations directory")
parser.add_argument("-n", "--number", dest="number",
default=20, nargs='?',
help="The number of captures to take. Default=20")
parser.add_argument("-i", "--interval", dest="interval",
default=0.05, nargs='?',
help="The time interval between "
"captures. Default=0.05s")
parser.add_argument("-r", "--region", dest="region",
default="0, 17, 500, 500", nargs='?',
help="The screen region to "
"capture. Default=(0, 17, 500, 500)")
parser.add_argument("-v", "--version", action="version",
version="%(prog)s {0}".format(__version__),
help="show program's version number and exit")
return parser
def main():
args = arguments().parse_args()
# the screen region to capture
region = tuple([int(i) for i in re.findall("(\d+)", args.region)])
for i in range(0, int(args.number)):
filename = str(time.time()) + '.png'
im = ImageGrab.grab(region)
im.save(os.path.join(args.path, filename))
time.sleep(int(args.interval))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment