Last Updated: February 25, 2016
·
1.272K
· cloudcray

Display clipboard image in iPython notebook

Often when I'm using the iPython notebook, I have to take a screenshot and place it inline. I created the following code to quickly display the contents of my clipboard, assuming I just Alt+PrintScreen'd or have some other image copied.

from PIL import ImageGrab
from IPython.display import display, Image

def save_show_clip(ratio=1.0):
    im_data = ImageGrab.grabclipboard()
    new_size = tuple([int(i*ratio) for i in im_data.size])
    thumb = im_data.resize(new_size)
    fn = "temp.PNG"
    thumb.save(fn)
    img = Image(filename=fn)
    display(img)

save_show_clip(.3)