[IPython-dev] [Notebook 1.0.0] Display two png images side by side

Zoltán Vörös zvoros@gmail....
Mon Sep 2 12:15:13 CDT 2013


Hi Francesco,

I have tried your code in python 2.7, and I don't encounter the problem 
that you describe. However, it seems that there are some complications 
in python 3. See this thread: 
http://stackoverflow.com/questions/16740188/base64-b64encode-error

I hope this helps,

Zoltán

On 02/09/13 17:14, Francesco Montesano wrote:
> Dear Zoltan,
>
> I've split the line with the append
>
>             fig_base64 = base64.b64encode(f.read())  # this works: so 
> file read and encoded without problems
>             full_html = 'data:image/png;base64,' + fig_base64   # 
> error TypeError: Can't convert 'bytes' object to str implicitly
>             pngs.append(full_html)
>
> Now if I try to print(fig_base64) before the second line I get:
>
> TypeError: Can't convert 'bytes' object to str implicitly
>
> b'iVBORw0KGgoAAAANS
>
>
>
>
>
>
> 2013/9/2 Zoltán Vörös <zvoros@gmail.com <mailto:zvoros@gmail.com>>
>
>
>     Hi Francesco,
>>
>>         import base64
>>         pngs = []
>>         for fn in images:  # loop over the input file names
>>             with open(fn, "rb") as f:
>>     pngs.append('data:image/png;base64,' + base64.b64encode(f.read()))
>>
>>     But I get the following error: on the last line:
>>     TypeError: Can't convert 'bytes' object to str implicitly
>>
>>     Is there a way to disable table lines in HTML?
>     From this, it seems to me that you encounter the problem on the
>     png-to-base64 conversion. Is that correct? Because afterwards the
>     image is encoded in base64, so it's not binary anymore, and I
>     don't see why html should fail at that point. But if it is an
>     error in b64encode, then you probably don't have a valid input file.
>
>     Cheers,
>     Zoltán
>
>>
>>
>>     2013/9/2 Zoltán Vörös <zvoros@gmail.com <mailto:zvoros@gmail.com>>
>>
>>         Hi Francesco,
>>
>>         It might be a hack, but this works:
>>
>>         with open("roche1.png", "rb") as image_file:
>>             im1 = 'data:image/png;base64,' +
>>         base64.b64encode(image_file.read())
>>
>>         with open("roche2.png", "rb") as image_file:
>>             im2 = 'data:image/png;base64,' +
>>         base64.b64encode(image_file.read())
>>
>>         s = """<table>
>>         <tr>
>>         <th><img src="%s"/></th>
>>         <th><img src="%s"/></th>
>>         </tr></table>"""%(im1, im2)
>>         t=HTML(s)
>>         display(t)
>>
>>
>>         Cheers,
>>         Zoltán
>>
>>
>>         On 02/09/13 15:32, Francesco Montesano wrote:
>>>         Dear List,
>>>
>>>         I have a script that produces some image and I'm creating a
>>>         notebook for testing/logging.
>>>
>>>         For comparison reasons, I would like to load to images in a
>>>         cell and show them side by side.
>>>
>>>             from IPython.display import Image
>>>             constr = Image(filename="fig1.png")
>>>             sigma = Image(filename="fig2.png")
>>>             display(constr, sigma)
>>>
>>>
>>>         put the figures one after the other. I've tried to play with
>>>         the width keyword, but that does not help.
>>>
>>>         Making an HTML table with the output of "Image" does not
>>>         work (I don't know if I should add "of course")
>>>
>>>         s = """<table>
>>>         <tr>
>>>         <th>{f1}</th>
>>>         <th>{f2}</th>
>>>         </tr>""".format(f1=constr, f2=sigma)
>>>         t=HTML(s)
>>>         display(t)
>>>
>>>         Is there any way to do what I want directly in the notebook?
>>>         (I guess that there is some python library to do it, but I
>>>         think that the notebook should be able to do it)
>>>
>>>         Cheers,
>>>
>>>         Fra
>>>
>>>
>>>         _______________________________________________
>>>         IPython-dev mailing list
>>>         IPython-dev@scipy.org  <mailto:IPython-dev@scipy.org>
>>>         http://mail.scipy.org/mailman/listinfo/ipython-dev
>>
>>
>>         _______________________________________________
>>         IPython-dev mailing list
>>         IPython-dev@scipy.org <mailto:IPython-dev@scipy.org>
>>         http://mail.scipy.org/mailman/listinfo/ipython-dev
>>
>>
>>
>>
>>     _______________________________________________
>>     IPython-dev mailing list
>>     IPython-dev@scipy.org  <mailto:IPython-dev@scipy.org>
>>     http://mail.scipy.org/mailman/listinfo/ipython-dev
>
>
>     _______________________________________________
>     IPython-dev mailing list
>     IPython-dev@scipy.org <mailto: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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-dev/attachments/20130902/bafeaae6/attachment.html 


More information about the IPython-dev mailing list