Last active
July 16, 2018 02:18
-
-
Save szero/3d0670aff67cfac57ae67493867bd25d to your computer and use it in GitHub Desktop.
python script for saving clipboard selection as image or uploading the selection to volafile and then discarding it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Save image from clipboard to file | |
""" | |
import sys | |
import glob | |
import argparse | |
from gi import require_version | |
require_version('Gtk', '3.0') | |
# pylint: disable=wrong-import-position | |
from gi.repository import Gtk, Gdk | |
# pylint: enable=wrong-import-position | |
def def_file(): | |
""" | |
Return default file name | |
""" | |
files = glob.glob("img???.png") | |
if len(files) < 1: | |
return 'img001.png' | |
maxf = 0 | |
for f in files: | |
try: | |
n = int(f[3:6]) | |
maxf = max(n, maxf) | |
except ValueError: | |
pass | |
return 'img{:03d}.png'.format(maxf+1) | |
def parse_args(): | |
usage = """%prog [option] [filename] | |
Save image from clipboard to file in PNG format.""" | |
parser = argparse.ArgumentParser(description=usage) | |
parser.add_argument("-o", "--open", dest="open", action="store_true", default=False, \ | |
help="Open saved file with default program (using xdg-open)") | |
parser.add_argument("-d", "--dont-save", dest="dont_save", action="store_true", default=False, \ | |
help="Set this option if you doesn't want to save the file.") | |
parser.add_argument("fname", metavar='FNAME', nargs="?", type=str, help="Filename") | |
parser.add_argument("-r", "--vola-room", dest="vola", metavar='ROOM_NAME', nargs="?", \ | |
type=str, const="default", help="Upload clipboard to given vola room.") | |
parser.add_argument("-p", "--pomfload", dest="pomf", \ | |
action="store_true", default=False, help="Upload clipboard to random pomf clone.") | |
args = parser.parse_args() | |
if args.dont_save and not args.vola and not args.pomf: | |
parser.error("You don't want the image to be saved and you didn't specify volaupload " + \ | |
"option so I have nothing to do!") | |
if not args.fname and not args.dont_save: | |
args.fname = def_file() | |
elif not args.fname: | |
args.fname = "Clipboard.png" | |
return args | |
args = parse_args() | |
atom = Gdk.atom_intern("CLIPBOARD", False) | |
clipboard = Gtk.Clipboard.get(atom) | |
image = clipboard.wait_for_image() | |
if image is not None: | |
from subprocess import call | |
if args.vola or args.pomf: | |
from tempfile import NamedTemporaryFile | |
res, buff = image.save_to_bufferv("png", "", "") | |
with NamedTemporaryFile(suffix=".png") as fp: | |
fp.write(buff) | |
fp.seek(0) | |
if args.vola == "default": | |
call(["volaupload.sh", fp.name, "-a", args.fname]) | |
elif args.vola is not None: | |
call(["volaupload.sh", "-r", args.vola, fp.name, "-a", args.fname]) | |
if args.pomf: | |
call(["pomfload.sh", fp.name]) | |
if not args.dont_save: | |
image.savev(args.fname, "png", "", "") | |
print("\nYour image was saved as {}".format(args.fname), file=sys.stdout) | |
if args.open: | |
call(["xdg-open", args.fname]) | |
else: | |
print("No image in clipboard found", file=sys.stderr) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Script is based on this SO answer, I ported it to python3 and replaced optparse with argparse.
Dependencies