| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
imwrite(A,filename,fmt)
imwrite(X,map,filename,fmt)
imwrite(...,filename)
imwrite(...,Param1,Val1,Param2,Val2...)
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 (grayscale image) or M-by-N-by-3 (truecolor image) array, but it cannot be an empty array. For TIFF files, A can be an M-by-N-by-4 array containing color data that uses the CMYK color space. For GIF files, A can be an M-by-N-by-1-by-P array containing grayscale or indexed images — RGB images are not supported. 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 Image Types. 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, imwrite offsets the values in the array before writing, using uint8(X–1). map must be a valid MATLAB colormap. Note that most image file formats do not support colormaps with more than 256 entries.
When writing multiframe GIF images, X should be an 4-dimensional M-by-N-by-1-by-P array, where P is the number of frames to write.
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 Image Types.
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.
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. Input values must be full (non-sparse).
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 bit depth 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.
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.
Parameter | Values |
|---|---|
A scalar integer that specifies which index in the colormap should be treated as the transparent color for the image and is used for certain disposal methods in animated GIFs. If X is uint8 or logical, indexing starts at 0. If X is double, indexing starts at 1. | |
A string or cell array of strings containing a comment to be added to the image. For a cell array of strings, a carriage return is added after each row. | |
A scalar value between 0 and 655 inclusive, that specifies the delay in seconds before displaying the next image. | |
One of the following strings, which sets the disposal method of an animated GIF: 'leaveInPlace', 'restoreBG', 'restorePrevious', or 'doNotSpecify'. | |
'Location' | A two-element vector specifying the offset of the top left corner of the screen relative to the top left corner of the image. The first element is the offset from the top, and the second element is the offset from the left. |
A finite integer between 0 and 65535 or the value Inf (the default) which specifies the number of times to repeat the animation. By default, the animation loops continuously. If you specify 0, the animation will be played once. If you specify the value 1, the animation will be played twice, etc. To enable animation within Microsoft® PowerPoint®, specify a value for the 'LoopCount' parameter within the range [1 65535]. Some Microsoft applications interpret the value 0 to mean do not loop at all. | |
'ScreenSize' | A two-element vector specifying the height and width of the frame. When used with 'Location', ScreenSize provides a way to write frames to the image that are smaller than the whole frame. 'DisposalMethod' determines the fill value used for pixels outside the frame. |
A scalar integer. This value specifies which index in the colormap should be treated as the transparent color for the image. If X (the image) is uint8 or logical, indexing starts at 0. If X is double, indexing starts at 1. | |
'overwrite' (the default) or 'append'. In append mode, imwrite adds a single frame to the existing file. |
Parameter | Values |
|---|---|
'none' (the default) 'jpeg'(valid only for grayscale and RGB images) 'rle' (valid only for grayscale and indexed images) | |
A number between 0 and 100; this parameter applies only if 'Compression' is 'jpeg'. Higher numbers mean higher quality (less image degradation due to compression), but the resulting file size is larger. The default value is 75. | |
'overwrite' (the default) 'append' |
Parameter | Values | Default |
|---|---|---|
A scalar value indicating desired bitdepth; for grayscale images this can be 8, 12, or 16; for color images this can be 8 or 12. | 8 (grayscale) and 8 bit per plane for color images | |
A column vector cell array of strings or a character matrix. imwrite writes each row of input as a comment in the JPEG file. | Empty | |
Specifies the type of compression used:'lossy' or 'lossless' | 'lossy' | |
A number between 0 and 100; higher numbers mean higher quality (less image degradation due to compression), but the resulting file size is larger. | 75 |
This table describes the available parameters for PBM, PGM, and PPM files.
Parameter | Values | Default |
|---|---|---|
One of these strings: 'ASCII' for plain encoding 'rawbits' for binary encoding | 'rawbits' | |
A scalar indicating the maximum gray or color value. Available only for PGM and PPM files. For PBM files, this value is always 1. | Default is 65535 if image array is 'uint16'; 255 otherwise. |
The following table lists the available parameters for PNG files, in alphabetical order. 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.
Parameter | Values |
|---|---|
A matrix specifying the transparency of each pixel individually. The row and column dimensions must be the same as the data array; they can be uint8, uint16, or double, in which case the values should be in the range [0,1]. | |
A string | |
The value specifies background color to be used when compositing transparent pixels. For indexed images: an integer in the range [1,P], where P is the colormap length. For grayscale images: a scalar in the range [0,1]. For truecolor images: a three-element vector in the range [0,1]. | |
A scalar value indicating desired bit depth. For grayscale images this can be 1, 2, 4, 8, or 16. For grayscale images with an alpha channel this can be 8 or 16. For indexed images this can be 1, 2, 4, or 8. For truecolor images with or without an alpha channel this can be 8 or 16. By default, imwrite uses 8 bits per pixel, if image is double or uint8; 16 bits per pixel if image is uint16; 1 bit per pixel if image is logical. | |
An eight-element vector [wx wy rx ry gx gy bx by] that specifies the reference white point and the primary chromaticities | |
A string | |
A string | |
A string | |
A string | |
A string | |
A nonnegative scalar indicating the file gamma | |
| A MATLAB serial date number (see the datenum function) or a string convertible to a date vector via the datevec function. Values should be in Coordinated Universal Time (UTC). | |
Either 'none' (the default) or 'adam7' | |
Either 'unknown' or 'meter' | |
A scalar or vector indicating how many bits in the data array should be regarded as significant; values must be in the range [1,BitDepth]. For indexed images: a three-element vector. For grayscale images: a scalar. For grayscale images with an alpha channel: a two-element vector. For truecolor images: a three-element vector. For truecolor images with an alpha channel: a four-element vector. | |
A string | |
A string | |
This value is used to indicate transparency information only when no alpha channel is used. Set to the value that indicates which pixels should be considered transparent. (If the image uses a colormap, this value represents an index number to the colormap.) For indexed images: a Q-element vector in the range [0,1], where Q is no larger than the colormap length and each value indicates the transparency associated with the corresponding colormap entry. In most cases, Q = 1. For grayscale images: a scalar in the range [0,1]. The value indicates the grayscale color to be considered transparent. For truecolor images: a three-element vector in the range [0,1]. The value indicates the truecolor color to be considered transparent. | |
A string | |
A scalar indicating the number of pixels/unit in the horizontal direction | |
A scalar indicating the number of pixels/unit in the vertical direction |
This table describes the available parameters for RAS files.
Parameter | Values | Default |
|---|---|---|
A matrix specifying the transparency of each pixel individually; the row and column dimensions must be the same as the data array; can be uint8, uint16, or double. Can only be used with truecolor images. | Empty matrix ([]) | |
One of these strings: 'standard' (uncompressed, b-g-r color order with truecolor images) 'rgb' (like 'standard', but uses r-g-b color order for truecolor images) 'rle' (run-length encoding of 1-bit and 8-bit images) | 'standard' |
This table describes the available parameters for TIFF files.
Parameter | Values |
|---|---|
Specifies the color space used to represent the color data. 'rgb' 'cielab' 'icclab' 1 (default is 'rgb'). Note: To use the CMYK color space in a TIFF file, do not use the 'ColorSpace' parameter. It is sufficient to specify an M-by-N-by-4 input array. | |
'none' Note: 'jpeg' is a lossy compression scheme; other compression modes are lossless. Also, if you specify 'jpeg' compression, you must specify the 'RowsPerStrip' parameter and the value must be a multiple of 8. | |
Any string; fills in the ImageDescription field returned by imfinfo. By default, the field is empty. | |
A two-element vector containing the XResolution and YResolution, or a scalar indicating both resolutions. The default value is 72. | |
'RowsPerStrip' | A scalar value specifying the number of rows to include in each strip. The default will be such that each strip is about 8K bytes. Note: You must specify the 'RowsPerStrip' parameter if you specify 'jpeg' compression. The value must be a multiple of 8. |
'overwrite' (default) 'append' |
1imwrite can write color image data that uses the L*a*b* color space to TIFF files. 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 8-bit and 16-bit encodings defined by the TIFF specification, called the CIELAB encodings, or using 8-bit and 16-bit encodings defined by the International Color Consortium, called ICCLAB encodings.
The output class and encoding used by imwrite depends on the class of the input array and the value of 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.)
Input Class and Encoding | ColorSpace Parameter Value | Output Class and Encoding |
|---|---|---|
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. | 8-bit ICCLAB | |
8-bit CIELAB | ||
16-bit ICCLAB Represents the values as integers in the range [0, 65280]. L* values are multiplied by 65280/100. 32768 is added to both the a* and b* values, which are represented as integers in the range [0,65535]. | 16-bit ICCLAB | |
16-bit CIELAB | ||
Double-precision 1976 CIE L*a*b* values L* is in the dynamic range [0, 100]. a* and b* can take any value. Setting a* and b* to 0 (zero) produces a neutral color (gray). | 8-bit ICCLAB | |
8-bit CIELAB |
This example appends an indexed image X and its colormap map to an existing uncompressed multipage HDF4 file.
imwrite(X,map,'your_hdf_file.hdf','Compression','none',... 'WriteMode','append')
fwrite, getframe, imfinfo, imformats, imread
Bit-Mapped Images for related functions
![]() | imread | incenters (TriRep) | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |