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 A
to the file specified by filename
in the format specified by fmt
.
A
can be an M-by-N (greyscale image) or M-by-N-by-3 (color image) array. A
cannot be an empty array. If the format specified is TIFF, imwrite
can also accept an M-by-N-by-4 arrray containing color data that uses the CMYK color space. For information about the class of the input array and the output image, see Class Support.
filename
is a string that specifies the name of the output file.
fmt
can be any of the text strings listed in the table in Supported Formats. This list of supported formats is determined by the MATLAB image file format registry. See imformats
for more information about this registry.
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 values for fmt
, listed in Supported Formats.
imwrite(...,Param1,Val1,Param2,Val2...)
specifies parameters that control various characteristics of the output file for HDF, JPEG, PBM, PGM, PNG, PPM, and TIFF files. For example, if you are writing a JPEG file, you can specify the quality of the output image. 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 (BMP |
1-bit, 8-bit, and 24-bit uncompressed images |
'gif' |
Graphics Interchange Format (GIF) |
8-bit images |
'hdf' |
Hierarchical Data Format (HDF) |
8-bit raster image datasets, with or without associated colormap, 24-bit raster image data sets; uncompressed or with RLE or JPEG compression |
'jpg' or 'jpeg' |
Joint Photographic Experts Group (JPEG) |
8-bit, 12-bit, and 16-bit Baseline JPEG images Note: Indexed images are converted to RGB before writing out JPEG files, because the JPEG format does not support indexed images. |
'pbm' |
Portable Bitmap (PBM) |
Any 1-bit PBM image, ASCII (plain) or raw (binary) encoding |
'pcx' |
Windows Paintbrush (PCX) |
8-bit images |
'pgm' |
Portable Graymap (PGM) |
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 (PNG) |
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; 24-bit and 48-bit truecolor images with alpha channels |
'pnm' |
Portable Anymap (PNM) |
Any of the PPM/PGM/PBM formats, chosen automatically |
'ppm' |
Portable Pixmap (PPM) |
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 (RAS) |
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 (TIFF) |
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; CIELAB, ICCLAB, and CMYK images |
'xwd' |
X Windows Dump (XWD) |
8-bit ZPixmaps |
Format-Specific Parameters
The following tables list parameters that can be used with specific file formats.
GIF-Specific Parameters
This table describes the available parameters for GIF files.
HDF-Specific Parameters
This table describes the available parameters for HDF files.
JPEG-Specific Parameters
This table describes the available parameters for JPEG files.
PBM-, PGM-, and PPM-Specific Parameters
This table describes the available parameters for PBM, PGM, and PPM files.
PNG-Specific Parameters
The following 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; that is, uses only printable characters, contains 80 or fewer characters, and no contains 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.
RAS-Specific Parameters
This table describes the available parameters for RAS files.
TIFF-Specific Parameters
This table describes the available parameters for TIFF files.
Parameter |
Values |
Default |
'ColorSpace' |
Specifies one of the following color spaces used to represent the color data. 'rgb' See L*a*b* Color Data for more information about this parameter. |
'rgb' |
'Compression' |
One of these strings: 'none' , 'packbits' , 'ccitt' , 'fax3' , or 'fax4' The 'ccitt' , 'fax3' , and 'fax4' compression schemes are valid for binary images only. |
|
'Description' |
Any string; fills in the ImageDescription field returned by imfinfo |
Empty |
'Resolution' |
A two-element vector containing the XResolution and YResolution, or a scalar indicating both resolutions |
72 |
'WriteMode' |
One of these strings: 'overwrite' |
'overwrite' |
Class Support
The input array A
can be of class logical
, uint8
, uint16
, or double
. Indexed images (X
) can be of class uint8
, uint16
, or double
; the associated colormap, map
, must be of class double
.
The class of the image written to the file depends on the format specified. For most formats, if the input array is of class uint8
, imwrite
outputs the data as 8-bit values. If the input array is of class uint16
and the format supports 16-bit data (JPEG, PNG, and TIFF), imwrite
outputs the data as 16-bit values. If the format does not support 16-bit values, imwrite
issues an error. Several formats, such as JPEG and PNG, support a parameter that lets you specify the bitdepth of the output data.
If the input array is of class double
, and the image is a grayscale or RGB color image, imwrite
assumes the dynamic range is [0,1] and automatically scales the data by 255 before writing it to the file as 8-bit values.
If the input array is of class double
, and the image is an indexed image, imwrite
converts the indices to zero-based indices by subtracting 1 from each element, and then writes the data as uint8
.
If the input array is of class logical
, imwrite
assumes the data is a binary image and writes it to the file with a bit depth of 1, if the format allows it. BMP, PNG, or TIFF formats accept binary images as input arrays.
L*a*b* Color Data
For TIFF files only, imwrite
can write a color image that uses the L*a*b* color space. The 1976 CIE L*a*b* specification defines numeric values that represent luminance (L*) and chrominance (a* and b*) information.
To store L*a*b* color data in a TIFF file, the values must be encoded to fit into either 8-bit or 16-bit storage. imwrite
can store L*a*b* color data in a TIFF file using these encodings:
The output class and encoding used by imwrite
to store color data depends on the class of the input array and the value you specify for the TIFF-specific ColorSpace
parameter. The following table explains these options. (The 8-bit and 16-bit CIELAB encodings cannot be input arrays because they use a mixture of signed and unsigned values and cannot be represented as a single MATLAB array.)
1 8-bit ICCLAB represents values as integers in the range [0 255]. L*
values are multiplied by 255/100
; 128
is added to both the a* and b* values.
2 16-bit ICCLAB multiplies L* values by 65280/100
and represents the values as integers in the range [0, 65280]. 32768
is added to both the a* and b* values, which are represented as integers in the range [0,65535].
3 L* is in the dynamic range [0, 100]. a* and b* can take any value. Setting a* and b* to 0
produces a neutral color (gray).
Example
This example appends an indexed image X
and its colormap map
to an existing uncompressed multipage HDF file.
See Also
fwrite
, getframe
, imfinfo
, imformats
, imread
Bit-Mapped Images for related functions
imread | ind2rgb |
© 1994-2005 The MathWorks, Inc.