Created
August 4, 2017 04:27
-
-
Save cheshirekow/67dd4dc52e757b7095d3e43ce243f7a9 to your computer and use it in GitHub Desktop.
Gtk command line clipboard for images
This file contains hidden or 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/python | |
"""Command line control of images in your clipboard.""" | |
import argparse | |
import os | |
import sys | |
import gi | |
gi.require_version('Gtk', '3.0') | |
from gi.repository import Gtk, Gdk, GdkPixbuf | |
def main(): | |
parser = argparse.ArgumentParser(description=__doc__) | |
mutex = parser.add_mutually_exclusive_group(required=True) | |
mutex.add_argument('-i', '--input', action='store_true') | |
mutex.add_argument('-o', '--output', action='store_true') | |
parser.add_argument('-t', '--type', default='png', | |
choices=['jpeg', 'png', 'tiff', 'ico', 'bmp']) | |
parser.add_argument('filepath') | |
args = parser.parse_args() | |
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) | |
if getattr(args, 'input', False): | |
assert os.path.exists(args.filepath), "file does not exist" | |
# NOTE(josh): new_from_bytes for stdin | |
image = GdkPixbuf.Pixbuf.new_from_file(args.filepath) | |
clipboard.set_image(image) | |
clipboard.store() | |
else: | |
image = clipboard.wait_for_image() | |
assert image is not None, "No image in clipboard" | |
# NOTE(josh): save_to_bufferv for stdout | |
image.savev(args.filepath, args.type, [], []) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment