Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add periodic capture and no shadow flags #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.png
*.jpg
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```

Expand Down
88 changes: 71 additions & 17 deletions screencapture.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,27 +12,27 @@

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


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))

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()
Expand All @@ -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:
Expand All @@ -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__":
Expand Down