tags:

views:

23

answers:

2

Ive tried the gtk method, but it is very slow and doesn't work for a 'large' image (120 kb)

import pygtk
pygtk.require('2.0')
import gtk
import os
def copy_image(f):
    assert os.path.exists(f), "file does not exist"
    clipboard = gtk.clipboard_get()
    img = gtk.Image()
    img.set_from_file(f)
    clipboard.set_image(img.get_pixbuf())
    clipboard.store()

Ive tried xclip and it only does text, so what other options are there? What does ubuntu use ?

A: 

One way of getting text from/to the clipboard is using XSel. It's not pretty and requires you to communicate with an external program. But it works and is quite fast.

Not sure if it's the best solution but I know it works :)

[edit]You're right, it seems that xsel does not support images.

In that case, how about a slightly modified GTK version.

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()

Do note that you might have to change the owner if your program exits right away because of how X keeps track of the clipboard.

WoLpH
doesnt seem to paste images... only text
josh
@josh: it seems you are right, I have added an alternative method which is a bit faster.
WoLpH
Thanks! Works faster, however this is weird, it seems to work great for pasting into gimp but when pasting into gnumeric or openoffice spreadsheet, it wont paste! Huh... Any ideas?
josh
@Josh: Just guessing here, but perhaps OpenOffice and Gnumeric aren't able to figure out the image type? Have you tried other image types (png/bmp) aswell?
WoLpH
A: 

You might want to use the set_with_data method instead, but that's slightly more work (the image data is only sent when an application requests it, so it needs callback-functions). This has also advantages when you paste in the same application instead of to another application.

JanC