Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@orschiro
Last active October 11, 2023 23:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save orschiro/8b9a1ddadb696c2b4587 to your computer and use it in GitHub Desktop.
Save orschiro/8b9a1ddadb696c2b4587 to your computer and use it in GitHub Desktop.
Python script that saves an image from clipboard to a file.

Introduction

Saves an image from clipboard to a file.

Dependencies:

  • python2
  • PyGTK

Installation

Download image-clipboard-to-file.py and run it with python image-clipboard-to-file.py.

Note: Some distributions such as Arch Linux renamed Python 2 to python2 instead of python.

Arch Linux users can install the image-clipboard-to-file package from the AUR.

Credits

Thanks to Paweł Pałucha who posted this script on this stackoverflow discussion. I share it here to allow package maintainers to easily package it for their distribution.

#!/usr/bin/python
"""
Save image from clipboard to file
"""
import sys
import glob
from optparse import OptionParser
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)
usage = """%prog [option] [filename]
Save image from clipboard to file in PNG format."""
op = OptionParser(usage=usage)
op.add_option("-o", "--open", action="store_true", dest="open", default=False,
help="Open saved file with default program (using xdg-open)")
(options, args) = op.parse_args()
if len(args) > 1:
parser.error("Only one argument expected")
sys.exit(1)
elif len(args) == 1:
fname = args[0]
else:
fname = def_file()
import gtk
clipboard = gtk.clipboard_get()
image = clipboard.wait_for_image()
if image is not None:
image.save(fname, "png")
print "PNG image saved to file", fname
if options.open:
import subprocess
subprocess.call(["xdg-open", fname])
else:
print "No image in clipboard found"
sys.exit( 1 )
@jippiegithub
Copy link

I added a single line to the end of the script to make it even more useful:

else:
    print "No image in clipboard found"
    sys.exit( 1 )

By adding the sys.exit it is now possible to issue an alternative command when there is no image in the clipboard buffer. I used this to create a "Launcher" button on my desktop:

~/bin/image-clipboard-to-file.py -o || gimp

When an image is in the clipboard buffer and I click the button, gimp will open with the image. Otherwise gimp will open as normal with an empty document.

@orschiro
Copy link
Author

@jippiegithub

Thanks for your contribution! I just saw your comment and added your line to the script above.

Regards,

Robert

@benzen3
Copy link

benzen3 commented Oct 11, 2023

Does this script work in Google Colab? I'm getting errors I don't know how to resolve.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment