diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..225c381 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.png +*.jpg diff --git a/README.md b/README.md index b30296e..e819d75 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,11 @@ Options: capture. -f, --filename TEXT Filename to save the captured PNG as. -a, --all_windows Capture all windows matching parameters + -t, --type TEXT Image format to create, default is png + (other options include pdf, jpg, tiff) + -i, --interval INTEGER Capture interval in seconds. Do not + duplicate similar images + -o, --no_shadow Do not capture the shadow of the window. --help Show this message and exit. ``` diff --git a/screencapture.py b/screencapture.py index a87cc67..ba53b90 100755 --- a/screencapture.py +++ b/screencapture.py @@ -1,5 +1,9 @@ -#!/usr/bin/python3 - +#!/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 +import os +import tempfile +import time +import imagehash as imagehash +from PIL import Image from itertools import chain from datetime import datetime from subprocess import getstatusoutput @@ -8,9 +12,8 @@ from get_window_id import gen_window_ids, options, user_options_str - -FILE_EXT = '.png' -COMMAND = 'screencapture -l %s "%s"' +TOLERANCE = 3 +COMMAND = 'screencapture %s -l %s "%s"' STATUS_OK = 0 @@ -18,8 +21,9 @@ class ScreencaptureEx(Exception): pass -def take_screenshot(window: int, filename: str, **kwargs) -> str: - rc, output = getstatusoutput(COMMAND % (window, filename)) +def take_screenshot(opts: str, window: int, filename: str, **kwargs) -> str: + command = COMMAND % (' '.join(opts), window, filename) + rc, output = getstatusoutput(command) if rc != STATUS_OK: raise ScreencaptureEx("Error in screenccapture command '%s'; Return code: %s Output: %s" % (COMMAND, rc, output)) @@ -27,8 +31,8 @@ def take_screenshot(window: int, filename: str, **kwargs) -> str: return filename -def _filename(*args) -> str: - return '_'.join(map(str, args + (datetime.now(),))) + FILE_EXT +def _filename(file_ext: str, *args) -> str: + return '_'.join(map(str, args + (datetime.now(),))) + file_ext @click.command() @@ -37,9 +41,39 @@ def _filename(*args) -> str: @click.option('-t', '--title', default='', help="Title of window from APPLICATION_NAME to capture.") @click.option('-f', '--filename', default=None, help="Filename to save the captured PNG as.") @click.option('-a', '--all_windows', is_flag=True, default=False, help="Capture all windows matching parameters.") +@click.option('-t', '--type', default=None, + help="Image format to create, default is png (other options include pdf, jpg, tiff)") +@click.option('-i', '--interval', default=0, help="Capture interval in seconds. Do not duplicate similar images") +@click.option('-o', '--no_shadow', is_flag=True, default=False, help="Do not capture the shadow of the window.") @click.argument('application_name') -def screenshot_window(application_name: str, title: str='', filename: str='', window_selection_options: str='', - all_windows: bool=False, **kwargs): +def screenshot_window(application_name: str, + title: str = '', + filename: str = '', + window_selection_options: str = '', + all_windows: bool = False, + interval: int = None, + no_shadow: bool = False, + type: str = 'png', + **kwargs): + + opts = [] + file_ext = '.png' + + if no_shadow: + opts.append("-o") + + if type == 'jpg': + opts.append("-t jpg") + file_ext = '.jpg' + + if type == 'tiff': + opts.append("-t tiff") + file_ext = '.tiff' + + if type == 'pdf': + opts.append("-t pdf") + file_ext = '.pdf' + windows = gen_window_ids(application_name, title, window_selection_options) try: @@ -52,12 +86,32 @@ def screenshot_window(application_name: str, title: str='', filename: str='', wi windows = chain((window,), windows) for window in windows: - filename = _filename(application_name, title) - print(take_screenshot(window, filename)) - - else: - filename = filename if filename else _filename(application_name, title) - print(take_screenshot(window, filename)) + filename = _filename(file_ext, application_name, title) + print(take_screenshot(opts, window, filename)) + exit() + + if interval > 0: + prev_hash = '' + while True: + base_filename = _filename(file_ext, application_name, title) + temp_filename = "%s/%s" % (tempfile.gettempdir(), base_filename) + + take_screenshot(opts, window, temp_filename) + + if os.path.isfile(temp_filename): + image_hash = imagehash.average_hash(Image.open(temp_filename)) + if not prev_hash or abs(prev_hash - image_hash) > TOLERANCE: + os.rename(temp_filename, base_filename) + print("Captured %s" % base_filename) + prev_hash = image_hash + else: + os.remove(temp_filename) + + time.sleep(interval) + exit() + + filename = filename if filename else _filename(file_ext, application_name, title) + print(take_screenshot(opts, window, filename)) if __name__ == "__main__":