How to copy a picture to clipboard from command line in linux?

14

3

I can copy image in Gimp and paste it to OpenOffice document.

How to do it (copy or paste image) from command line?

Vi.

Posted 2011-06-24T21:50:09.967

Reputation: 13 705

What does past "paste from clipboard from command line" mean? Probably the same but only for copy: http://unix.stackexchange.com/questions/30093/copy-image-from-command-line-to-clipboard

– Ciro Santilli 新疆改造中心法轮功六四事件 – 2014-08-13T09:35:04.630

More general question for any file type: http://superuser.com/questions/796376/is-is-possible-to-copy-binary-data-containing-a-nul-character-to-the-x-clipboard

– Ciro Santilli 新疆改造中心法轮功六四事件 – 2014-08-13T11:50:26.287

Answers

5

As found here, the key to paste binary data to a file with xclip is to tell what Media Types you have on clipboard. For PNG you can:

xclip -selection clipboard -t image/png -o > "`date '+%Y-%m-%d_%T'`.png"

Or image/jpeg and .jpg for JPEG.

So now on my ~/Dropbox/.mybashrc I add an alias (clipboard2photo) to easly paste to image file (maybe someday we'll have it on Nautilus).

pabloab

Posted 2011-06-24T21:50:09.967

Reputation: 90

As noted in first link: NOTE: Some research shows that you need xclip from SVN revision 81 (from April 2010) or later to have the required -t option. Or apply the patches yourself.

– i336_ – 2017-03-03T05:34:03.663

7

I believe the reason why Leo Alekseyev script does not work sometimes (on some systems) is explained in this answer to a similar question. Important part quoted here:

One oddity that is different from most other systems: if the program owning the selection (clipboard) goes away, so does the selection.

When i run Leo's script in python shell, it is working, as long as the shell is running. So i think the clipboard data is lost, when the script is terminated. The solution posted in the answer, is working for me:

#!/usr/bin/env python
import gtk 
import sys

count = 0
def handle_owner_change(clipboard, event):
    global count
    print 'clipboard.owner-change(%r, %r)' % (clipboard, event)
    count += 1
    if count > 1:
       sys.exit(0)

image = gtk.gdk.pixbuf_new_from_file(sys.argv[1])
clipboard = gtk.clipboard_get()
clipboard.connect('owner-change', handle_owner_change)
clipboard.set_image(image)
clipboard.store()
gtk.main()

Update from _Vi: For completeness, adding the clipboard->file script:

#!/usr/bin/python
import gtk, pygtk
pygtk.require('2.0')
import sys, os

clipboard = gtk.clipboard_get()
img = clipboard.wait_for_image()
img.save(sys.argv[1], "png", {})

wnm

Posted 2011-06-24T21:50:09.967

Reputation: 171

It's a bit annoying how to have to quit it manually, but at least it works :). Thanks! – crazy2be – 2014-06-24T00:05:28.657

1

The following python/pygtk script does the job:

#!/usr/bin/python
import gtk, pygtk
pygtk.require('2.0')
import sys, os

def copy_image(f):
    assert os.path.exists(f), "file does not exist"
    image = gtk.gdk.pixbuf_new_from_file(f)
    clipboard = gtk.clipboard_get()
    clipboard.set_image(image)
    clipboard.store()

copy_image(sys.argv[1]);

(Source: http://ubuntuforums.org/showthread.php?t=1689889)

To use this, sudo apt-get install python pygtk, paste the above code into a script, chmod +x to make executable, and you should be good to go.

Leo Alekseyev

Posted 2011-06-24T21:50:09.967

Reputation: 133

Copied little png picture using this script. Can't paste it neither to OpenOffice nor into Gimp ("There is no image data in clipboard to paste"). Don't work. After copying actual picture in Gimp and using this script the buffer reverts to text that was before that. – Vi. – 2011-12-15T14:22:25.467

I just tried: wget http://upload.wikimedia.org/wikipedia/commons/d/d9/Test.png && ./test.py Test.png, where test.py is exactly what's pasted here. Pastes fine into Gimp. – Leo Alekseyev – 2011-12-15T14:34:58.363

Tried with Test.png. "There is no image data in clipboard to paste". Does it depend on running Gnome? How to debug this? I can successfully copy image in Gimp and paste in Openoffice, so in general copying works. – Vi. – 2011-12-16T14:04:49.647

E: Unable to locate package pygtk – qed – 2014-01-04T20:00:06.467