# imwrite

Writes an image, whose pixel data comes from a matrix, m, with functions defined in the omlimgtoolbox.

## Syntax

imwrite(m, file)

imwrite(m, file, ext)

imwrite(m, ..., 'Alpha', alpha, 'DelayTime', delay, 'LoopCount', loops, 'Quality', quality, 'WriteMode', mode, ...)

## Inputs

m
2D or ND matrices with the pixel data for the image to write.
Type: matrix
file
Name of the image file to write.
Type: string
ext (optional)
Specifies the extension to use for the image file to be written. If this option is used, file should be the base name with no extension specified.
Type: string
alpha (optional)
Name-value pair to specify the alpha channel of the image. If specified, the dimensions of alpha should be the same as m. If not specified, the image is considered to be opaque.
Type: matrix
delay (optional)
Specifies the duration of each frame in a GIF file. A scalar value sets the same duration to all frames. A vector with length equal to the number of frames may be used to set the duration of each frame. The default value is 0.1sec.
Type: scalar | vector
loops (optional)
Specifies the number of times the frame sequence will be repeated in a GIF file. A value of 0 or 1 will show the sequence only once. The default value is Inf, it will be repeated infinite times.
Type: integer
quality (optional)
Name-value pair to specify the quality of the image written. Valid values are integers between 0 to 100, with 100 being of the highest quality. If not specified, the default value is 99.
Type: integer
mode (optional)
Name-value pair which specifies whether data needs to be appended or overwritten on an existing image file, file. Valid values are 'overwrite' and 'append', the default option being 'overwrite'.
Type: string

## Examples

Write an image with the default options:
m = imread('img_1640.jpg');
imwrite(m, 'oml_img_1640.jpg');
Overwrite an existing file with extension, alpha and quality options:
[m, dummy, alpha] = imread('img_1640.png');
imwrite(m, 'oml_img_1640', 'png', 'Alpha', alpha, 'Quality', 85, 'WriteMode', 'overwrite');
Create a GIF file from figure frames:
clear all; close all; clc

% init plot
t = 0:0.1:4;
l = plot(t(1), sin(t(1)), 'bo', t(1), cos(t(1)), 'ro');
num_frames = length(t);
axis([0 max(t) -1 1]);

% Create the frames matrix
sz = size(getframe(gcf).cdata);
f = zeros(sz(1), sz(2), sz(3), num_frames);
% Plot animation
for k = 1:num_frames
set(l(1),{'xdata', 'ydata'},{t(k), sin(t(k))});
set(l(2),{'xdata', 'ydata'},{t(k), cos(t(k))});

% get a snapshot of the plot in each iteration
f(:,:,:,k) = getframe(gcf).cdata;
end
close(gcf)

% Export GIF with default options
imwrite(f, 'gifDefault.gif');

% Set the LoopCount to 2 - the sequence will be show once and repeated twice
imwrite(f, 'gifLoop2.gif', 'LoopCount', 2);

% Set the DelayTime to 0.5 for all frames
imwrite(f, 'gifDelay.gif', 'DelayTime', 0.5);

% Set a different DelayTime for each frame
delay = linspace(0.01,1,num_frames);
imwrite(f, 'gifDelayVec.gif', 'DelayTime', delay);

% Save frames separately to demonstrate the 'append' functionality
imwrite(f(:,:,:,1:10), 'gifAppend.gif');
% Append frames to existing GIF
imwrite(f(:,:,:,15:20), 'gifAppend.gif', 'WriteMode', 'append');