Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux image from clipboard

Tags:

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 (http://stackoverflow.com/questions/6817600/save-the-image-in-the-clipboatd-in-python-tkinter) 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?

like image 985
alessandro Avatar asked Jul 27 '11 08:07

alessandro


People also ask

How do I get pictures from my clipboard?

Steps to save a clipboard image as a JPG or PNG file: Press the 'S' key on your keyboard. Navigate to the image you want to save as JPG or PNG. After locating the image, click the 'New” button in the Snipping Tool app. If the image is stored on your computer, open it in photos first.

How do I copy an image to the clipboard in Linux?

scrot + xclip. You can use scrot with xclip to take a screenshot and copy it to clipboard. It will capture whole of your screen and copy the image to clipboard. If you want to capture current window then use -u flag.

Where are clipboard images saved?

Loading an image from the Windows™ clipboard copies an image from the clipboard and links it to the record displayed in the current program window. The newly copied image is stored in the images folder for the software module.

How do you copy an image in Linux terminal?

Use the cp command to copy files in the Linux terminal. To copy a file on a computer with a graphical interface, you usually either drag and drop a file from one window to another window, sometimes using a modifier key.


1 Answers

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

#!/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"
like image 149
Paweł Pałucha Avatar answered Oct 23 '22 18:10

Paweł Pałucha