Create gif from saved images

Hi! I hope you solved your problem with @empet’s excellent advice.

If you find you’re doing this type of thing often, you might find it useful to have FFMPEG.jl in your toolbox. FFMPEG is the “Swiss Army knife” of image and video processing, and it’s conveniently wrapped in a Julia package.

If all your PNG files are named in the same way, you can write code such as this:

using FFMPEG
imagesdirectory = "/tmp/temp/"
framerate = 30
gifname = "/tmp/output.gif"
FFMPEG.ffmpeg_exe(`-framerate $(framerate) -f image2 -i $(imagesdirectory)/%10d.png -y $(gifname)`)

In this command, the %10d.png matches all files named with just 10 digits followed by “.png”. If your first file was 00001-timeseries.png you would use %05d-timeseries.png. The -y overwrites the GIF output file if it already exists.

You can use similar commands to create ‘real’ videos - this function:

FFMPEG.ffmpeg_exe(`-framerate $(framerate) -f image2 -i $(imagesdirectory)/%10d.png -vf "scale=1920:1080" -c:v libx264 -pix_fmt yuv420p -y "/tmp/output-video.mov"`)

rescales every image then outputs a video in a MPEG 4 container file.

(In a test I just did with this bigger output, the GIF file size for 150 frames was 198Mb, the equivalent movie was 4Mb. Presumably a fair amount of lossy compression going on.)

4 Likes