Search code examples
imagematlabjpeggif

How to make gif images from a set of images in matlab?


How to make '.gif' image from a set of '.jpg' images (say: I1.jpg, I2.jpg,..., I10.jpg) in matlab?


Solution

  • Ok here is a simple example. I got an image with a unicorn on it and remove 2 part to create 3 different images, just for the sake of creating an animated gif. Here is what it looks like:

    clear
    clc
    
    %// Image source: http:\\giantbomb.com
    A = rgb2gray(imread('Unicorn1.jpg'));
    B = rgb2gray(imread('Unicorn2.jpg'));
    C = rgb2gray(imread('Unicorn3.jpg'));
    
    ImageCell = {A;B;C};
    
    figure;
    subplot(131)
    imshow(A)
    
    subplot(132)
    imshow(B)
    
    subplot(133)
    imshow(C)
    
    %// Just to show what the images look like (I removed spots to make sure there was an animation created):
    

    enter image description here

    %// Create file name.
    FileName = 'UnicornAnimation.gif';
    
    for k = 1:numel(ImageCell)
    
        if k ==1
    
    %// For 1st image, start the 'LoopCount'.
            imwrite(ImageCell{k},FileName,'gif','LoopCount',Inf,'DelayTime',1);
        else
            imwrite(ImageCell{k},FileName,'gif','WriteMode','append','DelayTime',1);
        end
    
    end
    

    As you see, its not that different from the example on the Mathworks website. Here my images are in a cell array but yours might be in a regular array or something else.That should work fine; when I open 'UnicornAnimation.gif' it is indeed a nice animation!

    Hope that helps!