This example shows how to write image data from the MATLAB workspace to a file in one of the supported graphics file formats using the
Load image data into the workspace. This example loads the indexed image
X from a MAT-file,
clown.mat, along with the associated colormap
load clown whos
Name Size Bytes Class Attributes X 200x320 512000 double caption 2x1 4 char map 81x3 1944 double
Export the image data as a bitmap file using
imwrite, specifying the name of the variable and the name of the output file you want to create. If you include an extension in the filename,
imwrite attempts to infer the desired file format from it. For example, the file extension
.bmp specifies the Microsoft Windows Bitmap format. You can also specify the format explicitly as an argument to
Use format-specific parameters with
imwrite to control aspects of the export process. For example, with PNG files, you can specify the bit depth. To illustrate, read an image into the workspace in TIFF format and note its bit depth.
I = imread('cameraman.tif'); s = imfinfo('cameraman.tif'); s.BitDepth
ans = 8
Write the image to a graphics file in PNG format, specifying a bit depth of 4.
Check the bit depth of the newly created file.
newfile = imfinfo('cameraman.png'); newfile.BitDepth
ans = 4