alessandro alessandro - 1 year ago 183
Linux Question

linux image from clipboard

I'd like to access the graphics in the linux clipboard, to save it as a file.
I'm doing this in a Python/Tkinter program, so I asked about it ( but internally (in python) there's no hope.

Instead, I can accept to use an external utility to do it - yet I cannot find one.

Do you know of any terminal-based utility able to take the clipboard content and save it as an image file?

Answer Source

I couldn't find any tool to do it, so I wrote this small Python script. It requires pygtk.

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:
            n = int(f[3:6])
            maxf = max(n, maxf)
        except ValueError:
    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")
elif len(args) == 1:
    fname = args[0]
    fname = def_file()

import gtk
clipboard = gtk.clipboard_get()
image = clipboard.wait_for_image()
if image is not None:, "png")
    print "PNG image saved to file", fname
        import subprocess["xdg-open", fname])
    print "No image in clipboard found"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download