[IPython-dev] Paste images into the notebook

Abraham D. Flaxman abie@uw....
Sat Feb 8 10:32:50 CST 2014


This works for me, and it is something I've thought would be useful for a while now.  So thanks!  I put an example notebook together using it here: http://nbviewer.ipython.org/gist/aflaxman/8886274

I agree that it is slow, but I want it so much I'm willing to wait.  But do you have any thoughts on how it can go faster?

--Abie


-----Original Message-----
From: ipython-dev-bounces@scipy.org [mailto:ipython-dev-bounces@scipy.org] On Behalf Of Juergen Hasch
Sent: Friday, February 07, 2014 1:57 PM
To: IPython developers list
Subject: Re: [IPython-dev] Paste images into the notebook

This is my hack. Slow as hell for anything larger than some 10k.

window.addEventListener('paste', function(event){
    var cell = IPython.notebook.get_selected_cell();
    var items = event.clipboardData.items;
    for (var i = 0; i < items.length; ++i) {
        if (items[i].kind == 'file' && items[i].type.indexOf('image/') !== -1) {
            var blob = items[i].getAsFile();
            window.URL = window.URL || window.webkitURL;
            var blobUrl = window.URL.createObjectURL(blob);
            var img = document.createElement('img');
            img.src = blobUrl;

            var reader = new FileReader();
            reader.onload = ( function(evt) {
                var new_cell = IPython.notebook.insert_cell_below('markdown');
                var str = '<img src="' + evt.target.result + '">';
                new_cell.set_text(str);
                new_cell.edit_mode();
                new_cell.execute();
                } );
            reader.readAsDataURL(blob);
        }
    }
});



Am 07.02.2014 22:48, schrieb Raymond Yee:
> Juergen,
> 
> Can you publish  your code?  I'm definitely interested in this feature 
> even if it's hacky.
> 
> Thanks,
> 
> -Raymond
> 
> On 2/7/14 1:47 PM, Juergen Hasch wrote:
>> Am 07.02.2014 22:29, schrieb Matthias BUSSONNIER:
>>> Hi there,
>>>
>>>
>>> Le 7 févr. 2014 à 21:04, Edeler, Torsten a écrit :
>>>
>>>> Hi everybody.
>>>> I'm playing around with the notebook.
>>>> It is fantastic! Thank you for this amazing piece of software!
>>>>  
>>>> I'm just missing the ability to paste images directly from the clipboard (e.g. screenshots) into the notebook. My OS is Win7. How do I make that work?
>>>>  
>>> Short answer is you can't. 
>> Actually, it works :-)
>>
>> It only takes around 15 lines of Javascript code to implement this as clipboard paste event for Chrome.
>> All you have to do is receive the image from clipboard, create a markdown cell, and paste the image as base64.
>>
>> Not efficient and slow as hell for larger images, so I won't say this 
>> is a viable solution. Still, technically it can be done.
>>
>>
>>
>> _______________________________________________
>> IPython-dev mailing list
>> IPython-dev@scipy.org
>> http://mail.scipy.org/mailman/listinfo/ipython-dev
> 
> _______________________________________________
> IPython-dev mailing list
> IPython-dev@scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
> 

_______________________________________________
IPython-dev mailing list
IPython-dev@scipy.org
http://mail.scipy.org/mailman/listinfo/ipython-dev


More information about the IPython-dev mailing list