| Image Processing Toolbox | Search  Help Desk |
| imwrite | Examples See Also |
Write an image to a graphics file
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. A can be either a grayscale image (M-by-N) or a truecolor image (M-by-N-by-3). If A is of class uint8 or uint16, imwrite writes the actual values in the array to the file. If A is of class double, imwrite rescales the values in the array before writing, using uint8(round(255*A)). This operation converts the floating-point numbers in the range [0,1] to 8-bit integers in the range [0,255].
imwrite(X,map,filename,fmt) writes the indexed image in X and its associated colormap map to filename. 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). (See note below for an exception.) map must be a valid MATLAB colormap of class double; imwrite rescales the values in map using uint8(round(255*map)). Note that most image file formats do not support colormaps with more than 256 entries.
Note:
double, and you specify PNG as the output format and a bit depth of 16 bpp, the values in the array will be offset using uint16(X-1).
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. Parameter settings can currently be made for HDF, JPEG, and TIFF files. For example, if you are writing a JPEG file, you can set the "quality" of the JPEG compression. For the full list of parameters available per format, see the tables of parameters.
filename is a string that specifies the name of the output file, and fmt is a string that specifies the format of the file.
This table lists the possible values for fmt.| Parameter |
Values |
Default |
'Quality' |
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 |
imwrite can write.Class Support
Most of the supported image file formats storeuint8 data. PNG and TIFF 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 out as uint8. If the data array is uint8 or uint16 (PNG and TIFF only), then it is written out without scaling as uint8 or uint16, respectively.
Note:
double or uint8 is written to a PNG or TIFF file, it is assumed to be a binary image and will be written with a bitdepth 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 out as uint8. If the index array is uint8 or uint16 (PNG and TIFF only), then it is written out without modification as uint8 or uint16, respectively. When writing PNG files, you can override this behavior with the 'BitDepth' parameter; see the PNG table in this imwrite reference for details.
Remarks
imwrite is a function in MATLAB.
Example
This example appends an indexed imageXand its colormapmapto an existing uncompressed multipage HDF file namedflowers.hdf. imwrite(X,map,'flowers.hdf','Compression','none',... 'WriteMode','append')
See Also
imfinfo, imread
fwrite in the online MATLAB Function Reference