Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displaying videos in IJulia? #107

Closed
jiahao opened this issue Nov 6, 2013 · 7 comments
Closed

Displaying videos in IJulia? #107

jiahao opened this issue Nov 6, 2013 · 7 comments

Comments

@jiahao
Copy link
Member

jiahao commented Nov 6, 2013

I am interested in using matplotlib.animation to generate animations embedded in IJulia notebooks. However, my first attempt to translate a simple matplotlb into Julia code did not work:

using PyCall
using PyPlot
@pyimport matplotlib.animation as anim
fig=figure()
ims = [[imshow(randn(300,300,3))] for i=1:100]
ani = anim.ArtistAnimation(fig, ims, interval=25, blit=true)
show()

Next, I tried rendering the animation to a video file and loading it in IJulia. Using display("video/mp4",...) or display("video/mpeg",...) did not work. But embedding the HTML video tag almost worked...

using PyCall
using PyPlot
@pyimport matplotlib.animation as anim
fig=figure()
ims = [[imshow(randn(300,300,3))] for i=1:100]
ani = anim.ArtistAnimation(fig, ims, interval=25, blit=true)
ani[:save]("noise.mpg")
display("text/html", """<video autoplay controls><source src="noise.mpg" type="video/mpeg"></video>""")

This code renders an empty box and video playback controls, but the controls don't do anything, nor is a video displayed.

untitled

Am I on the right track to get embedded animations?

@Carreau
Copy link
Contributor

Carreau commented Nov 6, 2013

Almost, for now display as HTML and base 64 encode in URL, as IPython does.

Or you can try to prepend 'files/' to URL.

There is not much more IJulia can do about arbitrary mime type. Fix is on its way on python side.

Envoyé de mon iPhone

Le 6 nov. 2013 à 07:03, Jiahao Chen notifications@github.com a écrit :

I am interested in using matplotlib.animation to generate animations embedded in IJulia notebooks. However, my first attempt to translate a simple matplotlb into Julia code did not work:

using PyCall
using PyPlot
@pyimport matplotlib.animation as anim
fig=figure()
ims = [[imshow(randn(300,300,3))] for i=1:100]
ani = anim.ArtistAnimation(fig, ims, interval=25, blit=true)
show()
Next, I tried rendering the animation to a video file and loading it in IJulia. Using display("video/mp4",...) or display("video/mpeg",...) did not work. But embedding the HTML video tag almost worked...

using PyCall
using PyPlot
@pyimport matplotlib.animation as anim
fig=figure()
ims = [[imshow(randn(300,300,3))] for i=1:100]
ani = anim.ArtistAnimation(fig, ims, interval=25, blit=true)
ani:save
display("text/html", """""")

Am I on the right track to get embedded animations?


Reply to this email directly or view it on GitHub.

@stevengj stevengj closed this as completed Nov 6, 2013
@jiahao
Copy link
Member Author

jiahao commented Nov 6, 2013

Thanks for the tip. Posting the code that worked for me here in case someone later on might find it useful.

The last two lines in my example should be replaced with:

# If matplotlib complains, ensure that
# a) ffmpeg is installed with libx264 support, and
# b) matplotlib is built with ffmpeg support enabled
ani[:save]("noise.mp4", extra_args=["-vcodec", "libx264", "-pix_fmt", "yuv420p"])
display("text/html", string("""<video autoplay controls><source src="data:video/x-m4v;base64,""",
                            base64(open(readbytes,"noise.mp4")),"""" type="video/mp4"></video>"""))

@mbeltagy
Copy link

Thanks @jiahao for this thread and your final post. It helped me recreate the first example in this excellent blog on IPython notebook animation:
http://jakevdp.github.io/blog/2013/05/12/embedding-matplotlib-animations/

I am posting the code below as others might find it useful.

using PyCall
using PyPlot
@pyimport matplotlib.animation as anim# First set up the figure, the axis, and the plot element we want to animate
fig = figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
global line = ax[:plot]([], [], lw=2)[1]

# initialization function: plot the background of each frame
function init()
    global line
    line[:set_data]([], [])
    return (line,None)
end
# animation function.  This is called sequentially
function animate(i)
    x = linspace(0, 2, 1000)
    y = sin(2 * pi * (x - 0.01 * i))
    global line
    line[:set_data](x, y)
    return (line,None)
end

# call the animator.  blit=True means only re-draw the parts that have changed.
myanim = anim.FuncAnimation(fig, animate, init_func=init,
                               frames=100, interval=20, blit=true)

myanim[:save]("/tmp/sinplot.mp4", extra_args=["-vcodec", "libx264", "-pix_fmt", "yuv420p"])

# call our new function to display the animation
display("text/html", string("""<video autoplay controls><source src="data:video/x-m4v;base64,""",
                            base64(open(readbytes,"/tmp/sinplot.mp4")),"""" type="video/mp4"></video>"""))

@lbenet
Copy link

lbenet commented Nov 14, 2014

@mbeltagy Thanks for the nice approach. Any ideas how to extend this to animate (many points) using different colors?

@Omer80
Copy link

Omer80 commented Jan 6, 2016

Hi, I am also trying to animate a simulation using Julia. I would like to try to use the example given in matplotlib for dynamic image animation like here:

ims = []
for i in range(60):
x += np.pi / 15.
y += np.pi / 20.
im = plt.imshow(f(x, y), cmap='viridis', animated=True)
ims.append([im])

ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True, repeat_delay=1000)

(taken from http://matplotlib.org/examples/animation/dynamic_image2.html)

Is there an equivalent way to make it in Julia?

@JobJob
Copy link
Contributor

JobJob commented Jan 8, 2016

Adapting the above, this works though the loop is a little slow:

using PyCall
using PyPlot
@pyimport matplotlib.animation as anim# First set up the figure, the axis, and the plot element we want to animate
f(x,y) = sin(x) .+ cos(y)
fig = figure()
ims = []
nx = 120
ny = 100
x = repmat(linspace(0, 2pi, nx), 1, ny)
y = repmat(linspace(0, 2pi, ny)', nx, 1)
@show size(x) size(y)
for i in 1:20
    x += pi ./ 15
    y += pi ./ 20
    im = imshow(f(x, y); cmap="viridis")
    push!(ims, PyCall.PyObject[im])
end

ani = anim.ArtistAnimation(fig, ims, interval=50, blit=true, repeat_delay=1000)
ani[:save]("ani.mp4", extra_args=["-vcodec", "libx264", "-pix_fmt", "yuv420p"]);

# # call our new function to display the animation
display("text/html", string("""<video autoplay controls><source src="data:video/x-m4v;base64,""",
        base64encode(open(readbytes,"ani.mp4")),"""" type="video/mp4"></video>"""));

@Omer80
Copy link

Omer80 commented Jan 8, 2016

@JobJob - Thanks for the guidance. I have some problem in using the code, it tries to install matplotlib, and then exit with importerror - http://dpaste.com/0PB2GKV. Someone knows what could be the problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants