| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Image Processing Toolbox |
| Contents | Index |
| Learn more about Image Processing Toolbox |
| On this page… |
|---|
Specifying Format-Specific Parameters |
To export image data from the MATLAB workspace to a graphics file in one of the supported graphics file formats, use the imwrite function. When using imwrite, you specify the MATLAB variable name and the name of the file. If you include an extension in the filename, imwrite attempts to infer the desired file format from it. For example, the file extension .jpg infers the Joint Photographic Experts Group (JPEG) format. You can also specify the format explicitly as an argument to imwrite.
This example loads the indexed image X from a MAT-file, clown.mat, along with the associated colormap map, and then exports the image as a bitmap (BMP) file.
load clown whos Name Size Bytes Class X 200x320 512000 double array caption 2x1 4 char array map 81x3 1944 double array Grand total is 64245 elements using 513948 bytes imwrite(X,map,'clown.bmp')
When using imwrite with some graphics formats, you can specify additional format-specific parameters. For example, with PNG files, you can specify the bit depth. This example writes a grayscale image I to a 4-bit PNG file.
imwrite(I,'clown.png','BitDepth',4);
This example writes an image A to a JPEG file, using an additional parameter to specify the compression quality parameter.
imwrite(A, 'myfile.jpg', 'Quality', 100);
For more information about these additional format-specific syntaxes, see the imwrite reference page.
In certain file formats, such as TIFF, a binary image can be stored in a 1-bit format. When you read in a binary image in 1-bit format, imread stores the data in the workspace as a logical array. If the file format supports it, imwrite writes binary images as 1-bit images by default. This example reads in a binary image and writes it as a TIFF file.
BW = imread('text.png');
imwrite(BW,'test.tif'); To verify the bit depth of test.tif, call imfinfo and check the BitDepth field.
info = imfinfo('test.tif');
info.BitDepth
ans =
1imwrite uses the following rules to determine the storage class used in the output image.
![]() | Reading Image Data | Converting Between Graphics File Formats | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |