22

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?

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
alessandro
  • 3,838
  • 8
  • 40
  • 59
  • Perhaps this helps http://forums.debian.net/viewtopic.php?f=6&t=63433 – lhf Jul 27 '11 at 09:59
  • thank you, but I hoped in something more general, able to take the clipboard content, not a kind-of snapshot utility. I still cannot believe that such utility doesn exist, though I read somewhere that X11 clipboard management is a bit of a mess... – alessandro Jul 27 '11 at 11:58
  • Related: to detect changes in the clipboard: [C API](https://stackoverflow.com/questions/8755471/x11-wait-for-and-get-clipboard-text?noredirect=1&lq=1) or [bash](https://askubuntu.com/questions/1167026/detect-clipboard-copy-paste-event-and-modify-clipboard-contents). – user202729 Jan 28 '21 at 03:50

4 Answers4

19

Copy:

xclip -selection clipboard in.png

Shorter:

xclip -se c in.png

Paste:

xclip -selection clipboard -target image/png -out > out.png

Shorter version:

xclip -se c -t image/png -o > out.png

From this Unix & Linux question:

https://unix.stackexchange.com/questions/145131/copy-image-from-clipboard-to-file

You can also use image/tiff and image/jpeg.

Community
  • 1
  • 1
  • 7
    It shows error on my computer.. `Error: target image/png not available` – Merhawi Fissehaye May 25 '15 at 14:29
  • 2
    @MerhawiFissehaye, first run `xclip -o -target TARGETS -selection clipboard` to know which all targets can be applied for the 'current' clipboard data. –  Nov 09 '17 at 15:10
  • 2
    Error: target image/png not available this is possibly a red herring message probably indicating that you don't have a proper image in clipboard .. see @rraadd88 comment – zzapper Oct 12 '20 at 14:02
16

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"
ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
Paweł Pałucha
  • 176
  • 1
  • 3
  • Thanks for the script! I hope you do not mind that I created a [public Github Gist](https://gist.github.com/orschiro/8b9a1ddadb696c2b4587#file-image-clipboard-to-file-py) for it, for distribution contributors to easily package it. – orschiro Jul 23 '14 at 06:17
0

Using pyqt is easy.

def copy_image():
    clipboard=variableofapp.clipboard()
    if (clipboard.mimeData().hasImage()):
        img=x.pixmap()
        img.save('file.png',"PNG")
0

Take a look at xsel and xclip.

Otherwise, you might find some more information on wikipedia.

mrucci
  • 4,342
  • 3
  • 33
  • 35