Get individual frames of an `Animation` object

Here is an example from the Plots documentation:

using Plots

@userplot CirclePlot
@recipe function f(cp::CirclePlot)
    x, y, i = cp.args
    n = length(x)
    inds = circshift(1:n, 1 - i)
    linewidth --> range(0, 10, length = n)
    seriesalpha --> range(0, 1, length = n)
    aspect_ratio --> 1
    label --> false
    x[inds], y[inds]
end

n = 150
t = range(0, 2π, length = n)
x = sin.(t)
y = cos.(t)

anim = @animate for i ∈ 1:n
    circleplot(x, y, i)
end
gif(anim, "anim_fps15.gif", fps = 15)

image

Suppose that instead of a .gif file, I want to create a folder of .pngs named frame1.png, frame2.png, etc.

In the code above, I could do this by removing the @animate macro and saving each file:

for i ∈ 1:n
    circleplot(x, y, i)
    savefig("frame$i.png")
end

But how do I do this if I only have access to the object anim and not the underlying code that generates it?

I noticed that calling frame() on anim produces some sort of iterable that appears to index the frames:

for f in frame(anim)
    @show f
end

yields

f = "000001.png"
f = "000002.png"
f = "000003.png"
f = "000004.png"
f = "000005.png"
f = "000006.png"
f = "000007.png"
f = "000008.png"
...

But this is as far as I’ve gotten.

My ultimate goal is to embed the animation in a LaTeX/Beamer presentation, using the method given in this StackExchange question.

[png(f) for f in frame(anim)]

writes the frames

000001.png
000002.png
000003.png

to the current working directory.

The animation frames are saved in a temp folder, and its location depends on OS.
To let LaTeX access your frames, you can create a folder, animframes, and save yourself each frame in this folder (as in animation.jl):

using Plots
import Printf.@sprintf

x = 0:0.1:2π
y = sin.(x)
dir="animframes"

for k = 1:2:length(x)
    plt= plot(x[1:k], y[1:k], legend=false, size=(400, 300))
    filename = @sprintf("frame%02d.png", k) #or  @sprintf("frame%03.png", k) if you have more than 100 frames
                                            #and less than 1000
    #print(joinpath(dir, filename), "\n")
    png(plt, joinpath(dir, filename)) #saves the frame as a png file
end