Add support for screenshot and save to file (bound to Print key)

master
Quentin Duchemin 2019-02-26 00:24:02 +01:00
parent 24b7b25eae
commit 2f8501d82b
Signed by: Chosto
GPG Key ID: 0547178FEEDE7D6B
5 changed files with 82 additions and 15 deletions

View File

@ -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 exec_always --no-startup-id xinput set-prop 'pointer:Razer Razer DeathAdder Chroma' --type=float 'libinput Accel Speed' -.4
# Screenshot # Screenshot
# Selection, highlight, jpeg 9/10 quality, 5px selection border, hide cursor
# Save to file # Save to file
bindsym Print exec ~/.config/i3/scripts/screenshot.sh bindsym Print exec ~/.config/i3/scripts/screenshot.py -s

View File

@ -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=<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:])

View File

@ -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}"

3
.zshrc
View File

@ -113,6 +113,3 @@ alias ldap_pica='sudo ssh -L 389:localhost:389 qduchemi@monitoring.picasoft.net
# ssh-add is not used for convenience # ssh-add is not used for convenience
# Instead "AddKeysToAgent yes" option in ~/.ssh/config for "Host *" # Instead "AddKeysToAgent yes" option in ~/.ssh/config for "Host *"
eval "$(ssh-agent -s -t 3600)" eval "$(ssh-agent -s -t 3600)"
# Default path for screenshots
export SCREENSHOT_PATH=~/img/screenshot

View File

@ -51,6 +51,7 @@ switcher
* Non-Mixer, a mixer. * Non-Mixer, a mixer.
* Ardour, a DAW. * Ardour, a DAW.
* Random things (GTK3+ theme, Redshift, taskwarrior...) * 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) 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) ![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 * xdotool, xsel
* gnupg * gnupg
* maim, for screenshot * maim, for screenshot
* Python 3.7
\#todo automatic installation of dependencies \#todo automatic installation of dependencies