Plotting a sequence of images as a gif

Hi,

I have an input signal parameterized by time, which consists of a time dependent noise mask added on top of a base image defined as:

I(t) = X + noise(t)

I now want to create a gif animation using Plots to see what the signal looks like. The library example tells me to do something like this:

plt = plot(1)

@gif for i=1:500
        frame = I(i)
        push!(plt, frame)
    end every 10

However, frame here is an image, not several variables. I was experimenting with imshow but I get an since this does not return a plot object. What is the right way of doing this?

Your best bet is probably Makie.jl, which can do this sort of thing before breakfast (and not just for cat images…) :joy_cat: :smiley_cat:

Deep in the wilder fringes of the Julia ecosystem you’ll find Luxor.jl, which has the words “PNG”, “image”, “noise”, “GIF”, and “animation” in the documentation as this image confirms…

viking-funeral-for-attobot

… I abandoned this attempt at a pictorial Viking send-off for Attobot a while ago but it’s now found a home. However, I feel that Luxor is designed more for recreational use, and doesn’t provide such obviously essential plotting features such as axes, tick marks, and legends.

(Issues, pull requests, and stars are all welcome!)

8 Likes

Did you try to just use plot(I(i)) inside the loop?

Another option: just directly save a 3d array.

julia> using FileIO, ImageMagick, Colors, FixedPointNumbers

julia> save("/tmp/test.gif", rand(RGB{N0f8}, 32, 32, 5))
7 Likes

Small remark, You should drop GIF and use APNG.
It is now supported on all major web browsers and it is much better than GIF.

3 Likes

Oooo! Cool, thanks for the tip!

Animated PNGs look like they’d be a better solution than animated GIFs, but I don’t think there’s enough support as regards utilities and editing tools at present. For example, how do you get them to loop?

this is an animated PNG

↑ This pet cthulhu is an animated PNG, but I’ve only ever seen it move once. If it were a serious scientific animation, I’d be disappointed in the AnimatedPNG format.

1 Like

All my APNG loop forever.
I for one usually just wrap my scripts around APNG Assembler.
I write to HD the PNG files and run a command line to wrap them all into APNG.

As you can see you can set the number of loops (Or infinite) etc…

2 Likes

Ah, that’s cool, thanks - I couldn’t find out how to edit an existing animated PNG, but it’s easy enough to post-process the original still frames again.

It’s clever that you can use the same suffix .PNG for both still and animated images, but it would be good if file managers could show previews (the Finder on macOS doesn’t, but perhaps its different on other platforms…?).

This doesn’t work because plot treats the input as an y-axis values with the index in the array being the corresponding x-axis if it is not provided. Reshaping only means that now I have 32 sequences of y-values for 32 x-values.

This seems like the simplest way, yes. I can just concatenate the frames into one array. Does the time dimension need to be the last one?

Thanks, this indeed looks useful, though probably an overkill for what I want to do right now which is just look at what I’m doing. Still, probably better for flashier presentations.

Yes.

just look at what I’m doing

FYI, the various plotting packages (e.g., Makie, Plots, etc) and ImageView all allow you to visualize time-varying images from within Julia. For time-varying images, ImageView will give you a player-widget that you can use to go back and forth in time:

1 Like

Oh cool. I did not know that. Thanks.

I did not realize that you want to plot a 2d matrix. I think heatmap should work in that case, if I remember correctly.

Hi,
is it possible to tune the framerate with animated gifs?

save("/tmp/test.gif", rand(RGB{N0f8}, 32, 32, 5); fps=5)
3 Likes

Thank you so very much !

You better use Animated PNG (APNG). It is much better than GIF while the file size is similar.

It is time to let GIF vanish :-).

Thanks for the tip !
The thing is if I do

save("/tmp/test.png", rand(RGB{N0f8}, 32, 32, 5); fps=5)ype or paste code here

instead of gif, then a sequence of pngs is created but the animation is not.
Creating the animation directly in Julia is handy :wink:

Thank you again.
The created animated gif is perfect but I wonder If I could display it directly inside vscode (not via ImageView or clicking on the gif file) ?