MATLAB Function Reference | ![]() ![]() |
Syntax
imwrite(A,filename,fmt
) imwrite(X,map,filename,fmt
) imwrite(...,filename) imwrite(...,Param1,Val1,Param2,Val2...)
Description
imwrite(A,filename,
fmt
)
writes the image in A
to filename
in the format specified by fmt
. A
can be either a grayscale image (M-by-N) or a truecolor image (M-by-N-by-3). filename
is a string that specifies the name of the output file. Empty image data is not allowed. The possible values for fmt
are determined by the MATLAB file format registry. See imformats
for more information about this registry. To view a summary of these formats, see Supported Formats.
imwrite(X,map,filename,
fmt
)
writes the indexed image in X
and its associated colormap map
to filename
in the format specified by fmt
. If X
is of class uint8 or uint16
, imwrite
writes the actual values in the array to the file. If X
is of class double
, the imwrite
function offsets the values in the array before writing using uint8(X-1)
. The map
parameter must be a valid MATLAB colormap. Note that most image file formats do not support colormaps with more than 256 entries.
imwrite(...,filename)
writes the image to filename
, inferring the format to use from the filename's extension. The extension must be one of the legal values for fmt
.
imwrite(...,Param1,Val1,Param2,Val2...)
specifies parameters that control various characteristics of the output file. For example, if you are writing a JPEG file, you can set the quality of the JPEG compression. Parameter settings can currently be made for HDF, JPEG, PBM, PGM, PNG, PPM, and TIFF files. For the lists of parameters available for each format, see Format-Specific Parameters.
Supported Formats
This table summarizes the types of images that imwrite
can write. The MATLAB file format registry determines which file formats are supported. See imformats
for more information about this registry. Note that, for certain formats, imwrite
may take additional parameters, described in Format-Specific Parameters.
Format |
Full Name |
Variants |
'bmp' |
Windows Bitmap |
1-bit, 8-bit, and 24-bit uncompressed images |
'hdf' |
Hierarchical Data Format |
8-bit raster image datasets, with or without associated colormap, 24-bit raster image datasets; uncompressed or with RLE or JPEG compression |
'jpg' or 'jpeg' |
Joint Photographic Experts Group |
Baseline JPEG images (8- or 24-bit) Note: Indexed images are converted to RGB before writing out JPEG files, because the JPEG format does not support indexed images. |
'pbm' |
Portable Bitmap |
Any 1-bit PBM image, ASCII (plain) or raw (binary) encoding |
'pcx' |
Windows Paintbrush |
8-bit images |
'pgm' |
Portable Graymap |
Any standard PGM image; ASCII (plain) encoded with arbitrary color depth; raw (binary) encoded with up to 16 bits per gray value |
'png' |
Portable Network Graphics |
1-bit, 2-bit, 4-bit, 8-bit, and 16-bit grayscale images; 8-bit and 16-bit grayscale images with alpha channels; 1-bit, 2-bit, 4-bit, and 8-bit indexed images; 24-bit and 48-bit truecolor images with or without alpha channels |
'pnm' |
Portable Anymap |
Any of the PPM/PGM/PBM formats, chosen automatically |
'ppm' |
Portable Pixmap |
Any standard PPM image. ASCII (plain) encoded with arbitrary color depth; raw (binary) encoded with up to 16 bits per color component |
'ras' |
Sun Raster |
Any RAS image, including 1-bit bitmap, 8-bit indexed, 24-bit truecolor and 32-bit truecolor with alpha |
'tif' or 'tiff' |
Tagged Image File Format |
Baseline TIFF images, including 1-bit, 8-bit, 16-bit, and 24-bit uncompressed images; 1-bit, 8-bit, 16-bit, and 24-bit images with packbits compression; 1-bit images with CCITT 1D, Group 3, and Group 4 compression |
'xwd' |
X Windows Dump |
8-bit ZPixmaps |
Format-Specific Parameters
The following tables list parameters that can be used with specific file formats.
HDF-Specific Parameters
This table describes the available parameters for HDF files.
JPEG-Specific Parameters
This table describes the available parameters for JPEG files.
RAS-Specific Parameters
This table describes the available parameters for RAS files.
TIFF-Specific Parameters
This table describes the available parameters for TIFF files.
PNG-Specific Parameters
This table describes the available parameters for PNG files.
In addition to these PNG parameters, you can use any parameter name that satisfies the PNG specification for keywords, including only printable characters, 80 characters or fewer, and no leading or trailing spaces. The value corresponding to these user-specified parameters must be a string that contains no control characters other than linefeed.
PBM-, PGM-, and PPM-Specific Parameters
This table describes the available parameters for PBM, PGM, and PPM files.
Class Support
Most of the supported image file formats store uint8
data. PNG and TIFF formats additionally support uint16
data. For grayscale and RGB images, if the data array is double
, the assumed dynamic range is [0,1]. The data array is automatically scaled by 255 before being written as uint8
. If the data array is uint8
or uint16
, it is written without scaling as uint8
or uint16
, respectively.
Note
When the imwrite function writes logical data to a BMP, PNG or TIFF file, it assumes the data is a binary image and writes it to the file with a bit-depth of 1.
|
For indexed images, if the index array is double
, then the indices are first converted to zero-based indices by subtracting 1 from each element, and then they are written as uint8
. If the index array is uint8
or uint16
, then it is written without modification as uint8
or uint16
, respectively. When writing PNG files, you can override this behavior with the 'BitDepth'
parameter; see PNG-Specific Syntax for details.
Example
This example appends an indexed image X
and its colormap map
to an existing uncompressed multipage HDF file.
See Also
fwrite
, imfinfo
, imformats
, imread
Bit-Mapped Images for related functions
![]() | imread | ind2rgb | ![]() |