Skip to content

Instantly share code, notes, and snippets.

@jedy
Created April 10, 2015 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedy/fec41a9d232762ba721b to your computer and use it in GitHub Desktop.
Save jedy/fec41a9d232762ba721b to your computer and use it in GitHub Desktop.
copy figure to windows clipboard
import matplotlib.pyplot as plt
from cStringIO import StringIO
import win32clipboard
from PIL import Image
def send_to_clipboard(clip_type, data):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(clip_type, data)
win32clipboard.CloseClipboard()
def save_figure(f, file, w, h):
if not f:
f = plt.gcf()
dpi = f.get_dpi()
f.set_figwidth(w / dpi)
f.set_figheight(h /dpi)
f.savefig(file)
def copy_figure(w=600, h=400, fig=None):
file = StringIO()
save_figure(fig, file, w, h)
file.seek(0)
image = Image.open(file)
output = StringIO()
image.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:]
output.close()
send_to_clipboard(win32clipboard.CF_DIB, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment