1%IM2GIF Convert a multiframe image to an animated GIF file
13% This function converts a multiframe image to an animated GIF.
15% To create an animation from a series of figures, export to a multiframe
16% TIFF file
using export_fig, then convert to a GIF, as follows:
20% export_fig test.tif -nocrop -append
22%
im2gif(
'test.tif',
'-delay', 0.5);
25% infile -
string containing the
name of the input image.
26% outfile -
string containing the
name of the output image (must have the
27% .gif extension). Default: infile, with .gif extension.
28% A - HxWxCxN array of input images, stacked along fourth dimension, to
30% -nocrop - option indicating that the borders of the output are not to
32% -nodither - option indicating that dithering is not to be used when
33% converting the image.
34% -ncolors - option pair, the value of
which indicates the maximum number
35% of colors the GIF can have. This can also be a quantization
36% tolerance, between 0 and 1. Default/maximum: 256.
37% -loops - option pair, the value of
which gives the number of times the
38% animation is to be looped. Default: 65535.
39% -delay - option pair, the value of
which gives the time, in seconds,
40% between frames. Default: 1/15.
42% Copyright (C) Oliver Woodford 2011
45% 14/02/18: Merged issue #235: reduced memory usage, improved performance (thanks to @numb7rs)
46% 30/11/19: Merged issue #288: Fix
im2gif.m
for greyscale TIFF images (thanks @Blackbelt1221)
51 % Parse the input arguments
59 % Convert to indexed image
60 [h, w, c, n] = size(A);
62 % Issue #235: Using unique(A,
'rows') on the whole image stack at once causes
63 % massive memory usage when dealing with large images (at least on Matlab 2017b).
64 % Running unique(...) on individual frames, then again on the results drastically
65 % reduces the memory usage & slightly improves the execution time (@numb7rs).
66 uns = cell(1,size(A,4));
68 uns{nn}=unique(reshape(A(:,:,:,nn), h*w, c),
'rows');
70 map=unique(cell2mat(uns
'),'rows
');
72 A = reshape(permute(A, [1 2 4 3]), h, w*n, c);
75 dither_str = {'dither
', 'nodither
'};
76 dither_str = dither_str{1+(options.dither==0)};
77 if options.ncolors <= 1
78 [B, map] = rgb2ind(A, options.ncolors, dither_str);
80 [B, map] = rgb2ind(A, 256, dither_str);
83 [B, map] = rgb2ind(A, min(round(options.ncolors), 256), dither_str);
87 map = double(map) / 255;
90 B = rgb2ind(im2double(A), map);
92 B = reshape(B, h, w, 1, n);
95 map(B(1)+1,:) = im2double(A(1,1,:));
98 imwrite(B, map, options.outfile, 'LoopCount
', round(options.loops(1)), 'DelayTime
', options.delay);
101%% Parse the input arguments
102function [A, options] = parse_args(A, varargin)
104 options = struct('outfile
', '', ...
111 % Go through the arguments
116 if ischar(varargin{a}) && ~isempty(varargin{a})
117 if varargin{a}(1) == '-
'
118 opt = lower(varargin{a}(2:end));
121 options.crop = false;
123 options.dither = false;
125 if ~isfield(options, opt)
126 error('Option %s not recognized
', varargin{a});
129 if ischar(varargin{a}) && ~ischar(options.(opt))
130 options.(opt) = str2double(varargin{a});
132 options.(opt) = varargin{a};
136 options.outfile = varargin{a};
141 if isempty(options.outfile)
143 error('No output filename given.
');
145 % Generate the output filename from the input filename
146 [path, outfile] = fileparts(A);
147 options.outfile = fullfile(path, [outfile '.gif
']);
156%% Read image to uint8 rgb array
157function [A, alpha] = imread_rgb(name)
159 info = imfinfo(name);
160 % Special case formats
161 switch lower(info(1).Format)
163 [A, map] = imread(name, 'frames
', 'all
');
165 map = uint8(map * 256 - 0.5); % Convert to uint8 for storage
166 A = reshape(map(uint32(A)+1,:), [size(A) size(map, 2)]); % Assume indexed from 0
167 A = permute(A, [1 2 5 4 3]);
170 A = cell(numel(info), 1);
172 [A{a}, map] = imread(name, 'Index
', a, 'Info
', info);
174 map = uint8(map * 256 - 0.5); % Convert to uint8 for storage
175 A{a} = reshape(map(uint32(A{a})+1,:), [size(A) size(map, 2)]); % Assume indexed from 0
177 if size(A{a}, 3) == 4
178 % TIFF in CMYK colourspace - convert to RGB
185 A{a}(:,:,4) = A{a}(:,:,4) / 255;
186 A{a} = uint8(A(:,:,1:3) .* A{a}(:,:,[4 4 4]));
187 elseif size(A{a}, 3) < 3 %Check whether TIFF has been read in as greyscale
188 %Convert from greyscale to RGB colorspace (issue #288)
189 A{a} = cat(3, A{a}, A{a}, A{a});
194 [A, map, alpha] = imread(name);
195 A = A(:,:,:,1); % Keep only first frame of multi-frame files
197 map = uint8(map * 256 - 0.5); % Convert to uint8 for storage
198 A = reshape(map(uint32(A)+1,:), [size(A) size(map, 2)]); % Assume indexed from 0
199 elseif size(A, 3) == 4
200 % Assume 4th channel is an alpha matte
function name(in vendor)
Return the MPI library name as used in naming the ParaMonte MATLAB shared library directories.
function crop_borders(in A, in bcol, in padding, in crop_amounts)
function parse_args(in A, in varargin)
function imread_rgb(in name)
function im2gif(in A, in varargin)
function map()
Return a scalar MATLAB logical that is true if and only if the current installation of MATLAB contain...
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...