imwrite(A, filename)
imwrite(A, map, filename)
imwrite(..., fmt)
imwrite(..., propertyName, propertyValue)
| Parameter | Description |
|---|---|
| A | matrix: 3D for color and 2D for gray or indexed image. |
| map | Colormap of indexed image:m-by-3 array. |
| fmt | Format of output file: 'bmp', 'png', 'jpg', 'gif', ... |
| filename | a row vector characters or scalar string: name of graphics file. |
| propertyName | a scalar string or row vector character. |
| propertyValue | a value. |
imwrite(A, filename) writes image dataA to the file specified by filename
Writable formats are PNG, JPEG/JFIF, GIF, WebP, TIFF, BMP/DIB, TGA, PBM, PGM, PPM/PNM and PCX. The format is selected from fmt, or from the filename extension.
A may be logical, single, double, uint8 or uint16. A two-dimensional array is grayscale unless a K-by-3 colormap in [0, 1] is supplied; a direct-color image is M-by-N-by-3. The alpha matrix must be exactly M-by-N. Floating-point and logical data are normalized to 8 bits; uint8 data is used directly. An indexed image remains indexed when each palette entry has a consistent alpha value. uint16 input is accepted only for grayscale PNG, TIFF and PGM. Other formats report an explicit error.
Files are replaced atomically after successful encoding, so an encoding error does not leave a partially written destination.
Property name:
Quality: JPEG or WebP quality in [0, 100] (75 by default).
Alpha: M-by-N per-pixel matrix; floating-point values use [0, 1], uint8 values use [0, 255].
Comment: text stored when the selected codec supports this metadata.
Author: author text stored when the selected codec supports this metadata.
PNG, JPEG and TIFF store Comment and Author; GIF stores Comment. Other codecs ignore these properties without failing the image write.
Properties for gif format:
WriteMode: overwrite (default) or append.
LoopCount: animation loop count; Inf repeats indefinitely.
DelayTime: frame delay in seconds, in [0, 655].
f = figure();
A = rand(69, 69);
A(:,:,2) = rand(69,69);
A(:,:,3) = rand(69,69);
imshow(A);
imwrite(A, [tempdir, '69x69-RGB.png']);
close(f);
rgb = rand(16, 16, 3);
alpha = rand(16, 16);
gray16 = uint16(reshape(0:255, 16, 16) * 257);
imwrite(rgb, [tempdir, 'image.webp'], 'Quality', 90, 'Alpha', alpha);
imwrite(gray16, [tempdir, 'image16.png']);
firstFrame = uint8(zeros(32, 32, 3));
firstFrame(:, :, 1) = 255;
secondFrame = uint8(zeros(32, 32, 3));
secondFrame(:, :, 3) = 255;
filename_gif = [tempname(), '.gif'];
imwrite(firstFrame, filename_gif, 'gif', 'WriteMode', 'overwrite', ...
'LoopCount', Inf, 'DelayTime', 0.25);
imwrite(secondFrame, filename_gif, 'gif', 'WriteMode', 'append', ...
'DelayTime', 0.50);
| Version | Description |
|---|---|
| 1.0.0 | initial version |
| 1.13.0 | gif animation, pcx format added |
| 2.0.0 | WebP, portable codecs and 16-bit grayscale writing added |