This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Write Image Data to File in Graphics Format

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 imwrite function.

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

load clown
  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 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
Was this topic helpful?