imwrite
Writes an array to an image file.
Category: Data Import and Analysis > Data Import and Export
Syntax
imwrite(A, filename)
Writes A; the format comes from the file extension.
imwrite(A, map, filename)
Writes an indexed image with its colormap.
imwrite(..., Name, Value)
Sets format options such as 'Quality' for JPEG.
Description
Arrays of class double or single are scaled to 8 bits, so 0.5 is written as 128. Logical arrays are written as black and white, and uint16 data is stored with 16 bits in PNG files. PNG is lossless, while JPEG is lossy and will not return the exact pixels you wrote.
Input arguments
A- Grayscale (
m-by-n) or truecolor (m-by-n-by-3) array. Doubles are taken to span 0 to 1;uint8spans 0 to 255. filename- Output file name with extension such as
.png,.jpg,.bmpor.tif. 'Quality'- JPEG quality from 0 to 100.
Example
Scaling of doubles
imwrite([0 0.5; 1 0.25], 'd.png');
imread('d.png')
imwrite(uint16([0 65535; 1000 2000]), 'w.png');
class(imread('w.png'))
Output
ans = 2×2 uint8 matrix
0 128
255 64
ans = 'uint16'