# Get individual frames of an \`Animation\` object

**URL:** <https://discourse.julialang.org/t/get-individual-frames-of-an-animation-object/64310>\
**Category:** Visualization\
**Tags:** plotting, plots, animations\
**Created:** [July 8, 2021, 11:25pm UTC](https://discourse.julialang.org/t/get-individual-frames-of-an-animation-object/64310 "2021-07-08T23:25:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [July 8, 2021, 11:25pm UTC](https://discourse.julialang.org/t/get-individual-frames-of-an-animation-object/64310/1 "2021-07-08T23:25:34Z")

</div>

Here is an example from the [Plots documentation](https://docs.juliaplots.org/latest/animations/#animations):

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/9/9/99dd18cebf0db850e97570bb72c5587d952c3f1f.png)

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

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

```julia
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:

```julia
for f in frame(anim)
    @show f
end

```

yields

```julia
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](https://tex.stackexchange.com/questions/240243/getting-gif-and-or-moving-images-into-a-latex-presentation).

---

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [July 8, 2021, 11:57pm UTC](https://discourse.julialang.org/t/get-individual-frames-of-an-animation-object/64310/2 "2021-07-08T23:57:58Z")

</div>

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

```

writes the frames

```julia
000001.png
000002.png
000003.png

```

to the current working directory.

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [July 10, 2021, 11:19am UTC](https://discourse.julialang.org/t/get-individual-frames-of-an-animation-object/64310/3 "2021-07-10T11:19:54Z")

</div>

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](https://github.com/JuliaPlots/Plots.jl/blob/master/src/animation.jl#L20)):

```julia
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 

```
