Main Content

Write Image Data to File in Graphics Format

This example shows how to write image data from the workspace to a file in one of the supported graphics file formats using the imwrite function.

Load image data into the workspace. This example loads the indexed image X from a MAT-file, trees.mat, along with the associated colormap map.

load trees
  Name           Size              Bytes  Class     Attributes

  X            258x350            722400  double              
  caption        1x66                132  char                
  map          128x3                3072  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 imwrite.


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');
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');
ans = 4

See Also

Related Topics