I'm working on PyGTK application, that can generate graphs. This achieved by matplotlib. How can I add Copy-To-Clipboard functionality? How to copy figure to clipboard?
Asked
Active
Viewed 3,415 times
2
-
I suppose you want the contents as a plain image? – deinonychusaur May 17 '12 at 12:34
1 Answers
3
This will do it in Linux (just mouse-click on the image and it is copied to clipboard ready to paste in e.g. GIMP):
import matplotlib.pyplot as plt
from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
import gtk
import numpy as np
class W(gtk.Window):
def __init__(self):
gtk.Window.__init__(self)
img = np.random.uniform(0,1,(200,200))
fig = plt.Figure()
fig.gca().imshow(img)
self.image_canvas = FigureCanvas(fig)
self.image_canvas.connect('button_press_event', self.do_clip)
self.add(self.image_canvas)
self.show_all()
def do_clip(self, widget=None, event=None):
snap = self.image_canvas.get_snapshot()
pixbuf = gtk.gdk.pixbuf_get_from_drawable(None, snap,
snap.get_colormap(),0,0,0,0,
snap.get_size()[0], snap.get_size()[1])
clip = gtk.Clipboard()
clip.set_image(pixbuf)
w=W()
gtk.main()

deinonychusaur
- 7,094
- 3
- 30
- 44
-
Thank you. This code shows me everything what I need. And it also works flawless on windows – Lixas May 18 '12 at 05:35