Image Processing Toolbox | ![]() ![]() |
Writing a Graphics Image
The function imwrite
writes an image to a graphics file in one of the supported formats. If the image is of class uint8
and the format you choose supports 8-bit images, by default the image is written as 8-bit. If the image is of class double
, MATLAB's default behavior is to scale the data to class uint8
before writing it to file, since most graphics file format images do not support double-precision data. When the image is of class uint16
, there are two possible default outcomes: if you write an image of class uint16
to a format that supports 16-bit images (PNG or TIFF), it is written as a 16-bit file; if you write to a format that does not support 16-bit files, MATLAB first scales the data to class uint8
, as it does for images of class double
.
The most basic syntax for imwrite
takes the image variable name and a filename. If you include an extension in the filename, MATLAB infers the desired file format from it. This example writes an RGB image RGB
to a BMP file.
imwrite(RGB,'myfile.bmp');
For some graphics formats, you can specify additional parameters. One of the additional parameters for PNG files sets the bit depth. This example writes an intensity image I
to a 4-bit PNG file.
imwrite(I,'clown.png','BitDepth',4);
(The bit depths and image types supported for each format are shown in the reference pages for imwrite
.)
This example writes an image A
to a JPEG file with a compression quality setting of 100 (the default is 75).
imwrite(A, 'myfile.jpg', 'Quality', 100);
See the reference entry for imwrite
for more information.
![]() | Reading a Graphics Image | Querying a Graphics File | ![]() |