image thumbnail

Tools to read and write animated gif files

version (9 KB) by DGM
Functions to read and write animated gifs to and from 4-D RGB image arrays


Updated 13 Aug 2021

View License

These functions can be used to read and write animated gifs. They are intended for use with 4-D image arrays of various type. Supported types include RGB/RGBA arrays, as well as indexed images. Indexed mages with transparent content can be specified by a corresponding vector of indices, or by simply appending an alpha channel to the image array itself. When writing, colormaps, transparency, and disposal method can be specified per-frame or globally.
Optional functionality requires the use of imagemagick and is exclusively intended to work in a *nix environment, but default settings should work on other systems. I have no intention of modifying this to be more robust in that aspect.
reads all frames of an animated gif into a 4-D image array
Write image stack to an animated gif
Since R2018b, there are extant bugs in the support files for imread() and imfinfo() which cripple MATLAB's ability to read most GIF files without destructive alteration or complete loss of image data. I alone am in no position to motivate a bugfix or troubleshoot versions after R2019b. Until I write a complete replacement for the GIF functionality of imread/imfinfo, be very careful that your data isn't being damaged by MATLAB.
The 'imagemagick' option may be a workaround for the first bug in R2018b-R2020b. I have no means of determining whether a workaround exists for versions after R2020b. I have no idea if imwrite still correctly works after R2020b either.
These tools are merely a subset of the Matlab Image Manipulation Toolbox (FEX)

Cite As

DGM (2021). Tools to read and write animated gif files (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!