Displaying an array of images in an IJulia notebook

427 views
Skip to first unread message

Yuri Vishnevsky

unread,
May 26, 2014, 4:36:45 PM5/26/14
to julia...@googlegroups.com
Hi all,

I'm playing around with some image-related stuff in Julia and am using the method recommended here to get image data to render as an actual image inside of an IJulia notebook.

I'd like to extend Julia's multimedia display functionality to handle arrays of images (here's how Mathematica does it) and am trying to figure out the best way to do it.

This is what I've written so that individual array elements (of type Png) display as images:

type Png
 data
end
Base.writemime(io::IO, ::MIME"image/png", x::Png) = write(io, x.data)

The only way I've been able to get arrays to render as images at all has been this:

Base.writemime(io::IO, ::MIME"text/plain", x::Vector{Png}) = for i in x display(i) end

which seems very wrong, conceptually, but does have the benefit of actually working.

If anyone has thoughts on the right way to approach this question, I'd love to hear them.

Cheers,
Yuri

Steven G. Johnson

unread,
May 26, 2014, 5:03:25 PM5/26/14
to julia...@googlegroups.com
The text/plain hack you implemented is definitely wrong, and will cause problems if the writemime function is used in any other context.

You need to decide what MIME type the vector of images will be.   The most natural choices would be either HTML (http://stackoverflow.com/questions/2807251/can-i-embed-a-png-image-into-an-html-page) or SVG (http://stackoverflow.com/questions/6249664/does-svg-support-embedding-of-bitmap-images), both of which support embedding PNG data.

e.g. for HTML, you could do something like the following (untested, may have typos):

function Base.writemime(io::IO, ::MIME"text/html", x::AbstractVector{Png})
    print(io, "<p>", length(x), "-element ", typeof(x), "<ul>)
    png = MIME("image/png")
    for i in x
        print(io, "<li><img src=\"data:image/png;base64,", base64(writemime, png, i), "\" /></li>")
    end
    print(io, "</ul>")
end

Yuri Vishnevsky

unread,
May 26, 2014, 5:29:21 PM5/26/14
to julia...@googlegroups.com
Woohoo. Thanks!

Jingpeng Wu

unread,
May 16, 2015, 10:58:47 PM5/16/15
to julia...@googlegroups.com
I would suggest to try PyPlot, it uses matplotlib of python.
using PyPlot
imshow
(randn(10,10))
Reply all
Reply to author
Forward
0 new messages