78

Is it possible to take an image from the clipboard and output it to a file (using X)?

I can do this with text easily:

$ xclip -selection c -o > file.text

But when I try the above with an image nothing is written.


The reason I want to do this is I don't have an image editor installed, and it got me thinking whether I could do this without installing one.

Lerp
  • 913
  • 1
  • 7
  • 6

2 Answers2

112

You can actually do this with xclip using -t option.

  1. See what targets are available:
$ xclip -selection clipboard -t TARGETS -o
TARGETS
image/png
text/html
  1. Note the image/png target; go ahead and get it:
$ xclip -selection clipboard -t image/png -o > /tmp/clipboard.png

Refer to the ICCCM Section 2.6.2 for further reading.

Note: xclip SVN revision 81 (from April 2010) or patches later required.

Semnodime
  • 347
derobert
  • 109,670
  • I am using 0.12, the one that is offered by pacman so I shall have to get the upstream version. Thanks! – Lerp Jul 21 '14 at 08:23
  • 1
    Error: target image/png not available this be old. Debian stretch – Stígandr Nov 14 '16 at 15:45
  • @Stígandr I confirmed this still works on my machine running Debian testing (stretch). Maybe you copied a JPEG or some other type of image? Probably best to ask a new question rather than debug it in comments. – derobert Nov 14 '16 at 16:21
  • On my machine, i needed to specify -selection primary instead. – phil294 Feb 16 '20 at 18:02
  • 1
    @phil294 that's the other X clipboard, the two used ones are primary and clipboard (there are others, but nothing uses them). Primary is normally select and middle-click, clipboard is normally copy/paste – derobert Feb 16 '20 at 18:38
  • Can I test the results of target? i.e is my image type available – zzapper Dec 01 '20 at 16:33
  • 1
    @zzapper it spits out the types available, so you can test it by piping to grep for example. – derobert Dec 01 '20 at 21:48
3

xclip doesn't work with images on my computer (svn84-4) :

$ xclip -selection clipboard -t image/png -o
Error: target image/png not available

But there is another program which take screenshot and paste it into a file, very simply : scrot :

scrot /tmp/myImage.png
Gérard
  • 47
  • 1
  • 5
    It appears 'scrot' takes screenshot of my GNOME Terminal instead of taking it from clipboard. 'man' pages appears to be indicating no flag for this – MD. Mohiuddin Ahmed Sep 15 '20 at 09:12