From 2f8501d82bf89e480b5ad531cf2399009bfea6cd Mon Sep 17 00:00:00 2001 From: Quentin Duchemin Date: Tue, 26 Feb 2019 00:24:02 +0100 Subject: [PATCH] Add support for screenshot and save to file (bound to Print key) --- .config/i3/config.desk | 3 +- .config/i3/scripts/screenshot.py | 79 ++++++++++++++++++++++++++++++++ .config/i3/scripts/screenshot.sh | 10 ---- .zshrc | 3 -- README.md | 2 + 5 files changed, 82 insertions(+), 15 deletions(-) create mode 100755 .config/i3/scripts/screenshot.py delete mode 100755 .config/i3/scripts/screenshot.sh diff --git a/.config/i3/config.desk b/.config/i3/config.desk index 64a0166..10e23a2 100644 --- a/.config/i3/config.desk +++ b/.config/i3/config.desk @@ -207,6 +207,5 @@ exec thunderbird exec_always --no-startup-id xinput set-prop 'pointer:Razer Razer DeathAdder Chroma' --type=float 'libinput Accel Speed' -.4 # Screenshot -# Selection, highlight, jpeg 9/10 quality, 5px selection border, hide cursor # Save to file -bindsym Print exec ~/.config/i3/scripts/screenshot.sh +bindsym Print exec ~/.config/i3/scripts/screenshot.py -s diff --git a/.config/i3/scripts/screenshot.py b/.config/i3/scripts/screenshot.py new file mode 100755 index 0000000..b497e00 --- /dev/null +++ b/.config/i3/scripts/screenshot.py @@ -0,0 +1,79 @@ +#!/usr/bin/python + +import sys +import getopt +import subprocess +import pathlib +import time +import os + +def usage(path): + print('A tiny wrapper around maim to take screenshots and save or upload them (+ copy link in clipboard).\n') + print('Usage:') + print('\t' + sys.argv[0] + ' <--upload|--save> [--path=]') + print('\t' + sys.argv[0] + ' --help') + print('\nOptions:') + print('\t-h --help \tShow this screen.') + print('\t-u --upload \tUpload to Lychee.') + print('\t-s --save\tSave locally.') + print('\t-p --path\tPath where to save locally [default: ' + path +'].') + +def screenshot(img_format): + proc = subprocess.run(['maim', '-sl', '--color=0.6,0.4,0.2,0.2', '-f', img_format, '-u', '-m', '9', '-b', '5'], capture_output=True, check=True) + return proc.stdout + +def save_img(path, img, img_format): + # Create directory if needed + pathlib.Path(path).mkdir(parents=True, exist_ok=True) + + # Get date formatted with second granularity + date = time.strftime("%Y-%m-%d_%H:%M:%S") + + # Write image to appropriate file. + # We get binary from maim, so open in binary mode + filepath = os.path.join(path, date + '.' + img_format) + print(filepath) + with open(filepath, 'wb') as f: + f.write(img) + +def main(argv): + path = '~/img/screenshots' + + try: + opts, _ = getopt.getopt(argv, "husp:", ["help","upload","save","path="]) + except getopt.GetoptError: + usage(path) + + upload = False + save = False + img_format = 'png' + + for opt, arg in opts: + if opt in ('-h', '--help'): + usage() + elif opt in ('-u', '--upload'): + upload = True + elif opt in ('-s', '--save'): + save = True + elif opt in ('-p', '--path'): + path = opt + + # Expand path if there is a ~ inside + path = os.path.expanduser(path) + + # If we upload the file, set format to jpg, even if we save the screenshot after + if upload: + img_format = 'jpg' + + if upload or save: + try: + img = screenshot(img_format) + if save: + save_img(path, img, img_format) + except subprocess.CalledProcessError as e: + print("Screenshot taking has failed {0}".format(e)) + except Exception as e: + print("Unknown error : {0}".format(e)) + +if __name__ == "__main__": + main(sys.argv[1:]) \ No newline at end of file diff --git a/.config/i3/scripts/screenshot.sh b/.config/i3/scripts/screenshot.sh deleted file mode 100755 index 247d700..0000000 --- a/.config/i3/scripts/screenshot.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -path="$SCREENSHOT_PATH" -output=`date +${path}/%Y-%m-%d_%H:%M:%S.jpg` -if [ ! -d "${path}" ]; then - mkdir "${path}" -fi - -touch "${output}" -maim -sl --color=0.6,0.4,0.2,0.2 -f jpg -u -m 9 -b 5 > "${output}" \ No newline at end of file diff --git a/.zshrc b/.zshrc index 281f63b..b4f2b63 100644 --- a/.zshrc +++ b/.zshrc @@ -113,6 +113,3 @@ alias ldap_pica='sudo ssh -L 389:localhost:389 qduchemi@monitoring.picasoft.net # ssh-add is not used for convenience # Instead "AddKeysToAgent yes" option in ~/.ssh/config for "Host *" eval "$(ssh-agent -s -t 3600)" - -# Default path for screenshots -export SCREENSHOT_PATH=~/img/screenshot \ No newline at end of file diff --git a/README.md b/README.md index 2f94c23..387da43 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ switcher * Non-Mixer, a mixer. * Ardour, a DAW. * Random things (GTK3+ theme, Redshift, taskwarrior...) +* Screenshots (area or windows), with saving or uploading to a Lychee server ([custom script here](.config/i3/scripts/screenshot.py)) Here is a screenshot of the rendition i3/polybar with this setup (and yeah, I use nano, sorry to disappoint) ![Screenshot of i3/polybar](https://pic.chosty.fr/uploads/big/22c75dc7901223204e0e9c798506b435.png) @@ -71,6 +72,7 @@ Configuration makes use of these things, just to record but not exhaustive : * xdotool, xsel * gnupg * maim, for screenshot +* Python 3.7 \#todo automatic installation of dependencies