FUNCTION REFERENCE

imwrite

Write a PNG, JPEG, BMP, TIFF or GIF image.

Write a PNG, JPEG, BMP, TIFF or GIF image.

Syntax

imwrite(A, filename)

Syntax

imwrite(X, map, filename, fmt, 'BitDepth', d, 'Alpha', alpha)

Syntax

imwrite(A, filename, 'Quality', q)

Syntax

imwrite(X, map, filename, 'gif', 'LoopCount', Inf, 'DelayTime', s, 'WriteMode', 'append')

Writes grayscale M-by-N or truecolor M-by-N-by-3 images (double, single, logical, uint8, uint16) and indexed images with a colormap. Doubles map 0..1 to the full range (NaN and values above 1 give the maximum, as MATLAB does). PNG: BitDepth 1, 2, 4, 8 or 16 for gray, 8 or 16 for RGB; 'Alpha' adds an alpha channel; other text parameters become PNG text chunks. JPEG: 8-bit baseline, 'Quality' 0..100 (default 75); uint16 data is scaled to 8 bits. BMP: 8-bit gray (with a gray palette), 24-bit RGB, or 8-bit indexed with the colormap. TIFF: uncompressed 8-bit, or 16-bit for uint16 data. GIF: GIF89a with LZW compression; X is uint8 or logical (zero-based) or double (one-based) with a colormap of 1 to 256 colours (padded to a power of 2). 'LoopCount' (Inf or 0..65535; Inf loops forever) adds a NETSCAPE2.0 block to the first frame; 'DelayTime' is in seconds (0..655.35, stored in hundredths, rounded); 'DisposalMethod' is 'doNotSpecify' (default), 'leaveInPlace', 'restoreBG' or 'restorePrevious'; 'WriteMode' 'append' adds a frame with its own colour table to an existing GIF of matching logical screen size (a missing file is created; reading is bounded by 256 MiB). The parent folder must be writable; a Windows viewer holding the file can make append fail, and a killed process may leave a temp file. The format comes from the extension or fmt.

Example

f=[tempname(pwd) '.png']; imwrite([0 0.5 1], f); A=imread(f); delete(f); A   % [0 128 255] uint8

Example

f=[tempname(pwd) '.jpg']; imwrite(uint8(repmat(0:10:250, 8, 1)), f, 'Quality', 90); A=imread(f); delete(f); size(A)   % [8 26]

Example

f=[tempname(pwd) '.gif']; imwrite(uint8([0 1; 2 3]), [0 0 0; 1 0 0; 0 1 0; 0 0 1], f, 'LoopCount', Inf, 'DelayTime', 0.1); i=imfinfo(f); delete(f); i.DelayTime   % 10

Differences from MATLAB: truecolor GIF and GIF without a colormap (use rgb2ind first), GIF frames whose size differs from the logical screen, the GIF 'TransparentColor', 'BackgroundColor', 'Comment', 'Location' and 'ScreenSize' parameters and the other formats (PBM, PCX, ...) are not supported yet, nor the PNG Transparency, Background and Gamma parameters, lossless or 12-bit JPEG, TIFF compression, the other JPEG, BMP and TIFF parameters, or writing several TIFF pages. 'WriteMode' 'append' creates a missing file. Concurrent writers to the same GIF file are unsupported (no CAS). Logical data is written to BMP and TIFF as 8-bit 0/255 (MATLAB writes 1-bit images). JPEG, BMP and TIFF need the desktop build.

These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.