Read image from graphics file


A = imread(filename, fmt)
[X, map] = imread(...)
[...] = imread(filename)
[...] = imread(URL,...)
[...] = imread(...,Param1,Val1,Param2,Val2...)


A = imread(filename, fmt) reads a grayscale or color image from the file specified by the string filename. If the file is not in the current folder, or in a folder on the MATLAB® path, specify the full pathname.

The text string fmt specifies the format of the file by its standard file extension. For example, specify 'gif' for Graphics Interchange Format files. To see a list of supported formats, with their file extensions, use the imformats function. If imread cannot find a file named filename, it looks for a file named filename.fmt.

The return value A is an array containing the image data. If the file contains a grayscale image, A is an M-by-N array. If the file contains a truecolor image, A is an M-by-N-by-3 array. For TIFF files containing color images that use the CMYK color space, A is an M-by-N-by-4 array. See TIFF in the Format-Specific Information section for more information.

The class of A depends on the bits-per-sample of the image data, rounded to the next byte boundary. For example, imread returns 24-bit color data as an array of uint8 data because the sample size for each color component is 8 bits. See Tips for a discussion of bitdepths, and see Format-Specific Information for more detail about supported bit depths and sample sizes for a particular format.

[X, map] = imread(...) reads the indexed image in filename into X and its associated colormap into map. Colormap values in the image file are automatically rescaled into the range [0,1].

[...] = imread(filename) attempts to infer the format of the file from its content.

[...] = imread(URL,...) reads the image from an Internet URL. The URL must include the protocol type (e.g., http://).

[...] = imread(...,Param1,Val1,Param2,Val2...) specifies parameters that control various characteristics of the operations for specific formats. For more information, see Format-Specific Information.

Format-Specific Information

The following sections provide information about the support for specific formats, listed in alphabetical order by format name. These sections include information about format-specific syntaxes, if they exist.

BMP — Windows BitmapJPEG — Joint Photographic Experts GroupPNG — Portable Network Graphics
CUR — Cursor FileJPEG 2000 — Joint Photographic Experts Group 2000PPM — Portable Pixmap
GIF — Graphics Interchange FormatPBM — Portable BitmapRAS — Sun Raster
HDF4 — Hierarchical Data FormatPCX — Windows PaintbrushTIFF — Tagged Image File Format
ICO — Icon FilePGM — Portable GraymapXWD — X Window Dump

BMP — Windows Bitmap

Supported BitdepthsNo CompressionRLE CompressionOutput ClassNotes
16-bityuint81 sample/pixel
24-bityuint83 samples/pixel
32-bityuint83 samples/pixel
(1 byte padding)

CUR — Cursor File

Supported BitdepthsNo CompressionCompressionOutput Class

Format-specific syntaxes:

[...] = imread(..., idx) reads in one image from a multi-image icon or cursor file. idx is an integer value that specifies the order that the image appears in the file. For example, if idx is 3, imread reads the third image in the file. If you omit this argument, imread reads the first image in the file.

[A, map, alpha] = imread(...) returns the AND mask for the resource, which can be used to determine the transparency information. For cursor files, this mask may contain the only useful data.

    Note   By default, Microsoft® Windows® cursors are 32-by-32 pixels. MATLAB pointers must be 16-by-16. You will probably need to scale your image. If you have Image Processing Toolbox™, you can use its imresize function.

GIF — Graphics Interchange Format

Supported BitdepthsOutput Class
2-bit to 8-bituint8

Format-specific syntaxes:

[...] = imread(..., idx) reads in one or more frames from a multiframe (i.e., animated) GIF file. idx must be an integer scalar or vector of integer values. For example, if idx is 3, imread reads the third image in the file. If idx is 1:5, imread returns only the first five frames.

[...] = imread(..., 'frames', idx) is the same as the syntax above except that idx can be 'all'. In this case, all the frames are read and returned in the order that they appear in the file.

    Note   Because of the way that GIF files are structured, all the frames must be read when a particular frame is requested. Consequently, it is much faster to specify a vector of frames or 'all' for idx than to call imread in a loop when reading multiple frames from the same GIF file.

HDF4 — Hierarchical Data Format

Supported BitdepthsRaster Image with colormapRaster image without colormapOutput ClassNotes
24-bityuint83 samples/pixel

Format-specific syntaxes:

[...] = imread(..., ref) reads in one image from a multi-image HDF4 file. ref is an integer value that specifies the reference number used to identify the image. For example, if ref is 12, imread reads the image whose reference number is 12. (Note that in an HDF4 file the reference numbers do not necessarily correspond to the order of the images in the file. You can use imfinfo to match image order with reference number.) If you omit this argument, imread reads the first image in the file.

ICO — Icon File

See CUR — Cursor File

JPEG — Joint Photographic Experts Group

imread can read any baseline JPEG image as well as JPEG images with some commonly used extensions. For information about support for JPEG 2000 files, see JPEG 2000.

Supported Bits-per-sampleLossy CompressionLossless CompressionOutput ClassNotes
8-bityyuint8Grayscale or RGB
12-bityyuint16Grayscale or RGB

JPEG 2000 — Joint Photographic Experts Group 2000

For information about JPEG files, see JPEG.

    Note:   Indexed JPEG 2000 images are not supported. Only JP2 compatible color spaces are supported for JP2/JPX files. By default, all image channels are returned in the order they are stored in the file.

Supported Bits-per-sample

Lossy CompressionLossless CompressionOutput ClassNotes
1-bityylogicalGrayscale only
2- to 8-bityyuint8 or int8Grayscale
or RGB
9- to 16-bityyuint16 or int16Grayscale
or RGB

Format-specific syntaxes:

[...] = imread(..., 'Param1', value1, 'Param2', value2, ...) uses parameter-value pairs to control the read operation, described in the following table.

'ReductionLevel'A non-negative integer specifying the reduction in the resolution of the image. For a reduction level L, the image resolution is reduced by a factor of 2^L. Its default value is 0 implying no reduction. The reduction level is limited by the total number of decomposition levels as specified by the'WaveletDecompositionLevels' field in the structure returned by the imfinfo function.
'PixelRegion'{ROWS, COLS} — The imread function returns the sub-image specified by the boundaries in ROWS and COLS. ROWS and COLS must both be two-element vectors that denote the 1-based indices [START STOP]. If 'ReductionLevel' is greater than 0, then ROWS and COLS are coordinates in the reduced-sized image.
'V79Compatible'A logical value. If true, the image returned is transformed to grayscale or RGB, consistent with previous versions of imread (MATLAB 7.9 [R2009b] and earlier). Use this option to transform YCC images into RGB. The default is false.

PBM — Portable Bitmap

Supported BitdepthsRaw BinaryASCII (Plain) EncodedOutput Class

PCX — Windows Paintbrush

Supported BitdepthsOutput ClassNotes
1-bitlogicalGrayscale only
8-bituint8Grayscale or indexed
Three 8-bit samples/pixel

PGM — Portable Graymap

Supported BitdepthsRaw BinaryASCII (Plain) EncodedOutput ClassNotes
Arbitraryy1-bit to 8-bit: uint8
9-bit to 16-bit: uint16
Values are scaled

PNG — Portable Network Graphics

Supported BitdepthsOutput ClassNotes
8-bituint8Grayscale or Indexed
16-bituint16Grayscale or Indexed
Three 8-bit samples/pixel.
Three 16-bit samples/pixel.

Format-specific syntaxes:

[...] = imread(...,'BackgroundColor',BG) composites any transparent pixels in the input image against the color specified in BG. If BG is 'none', then no compositing is performed. If the input image is indexed, BG must be an integer in the range [1,P] where P is the colormap length. If the input image is grayscale, BG should be a value in the range [0,1]. If the input image is RGB, BG should be a three-element vector whose values are in the range [0,1]. The string 'BackgroundColor' can be abbreviated.

[A, map, alpha] = imread(...) returns the alpha channel if one is present. If no alpha channel is present, or if you specify 'BackgroundColor', then alpha is []. The map output can be empty if the file contains a grayscale or truecolor image.

If you specify the alpha output argument, BG defaults to 'none', if not specified. Otherwise, if the PNG file contains a background color chunk, that color is used as the default value for BG. If alpha is not used and the file does not contain a background color chunk, then the default value for BG is 1 for indexed images; 0 for grayscale images; and [0 0 0] for truecolor (RGB) images.

PPM — Portable Pixmap

Supported BitdepthsRaw BinaryASCII (Plain) EncodedOutput Class
Up to 16-bityuint8

RAS — Sun Raster

The following table lists the supported bitdepths, compression, and output classes for RAS files.

Supported BitdepthsOutput ClassNotes
Three 8-bit samples/pixel
32-bituint8RGB with Alpha
Four 8-bit samples/pixel

TIFF — Tagged Image File Format

Most images supported by the TIFF specification or LibTIFF can be read by imread.

imread supports the following TIFF capabilities:

  • Any number of samples-per-pixel

  • CCITT group 3 and 4 FAX, Packbits, JPEG, LZW, Deflate, ThunderScan compression, and uncompressed images

  • Logical, grayscale, indexed color, truecolor and hyperspectral images

  • RGB, CMYK, CIELAB, ICCLAB color spaces. If the color image uses the CMYK color space, A is an M-by-N-by-4 array. To determine which color space is used, use imfinfo to get information about the graphics file and look at the value of the PhotometricInterpretation field. If a file contains CIELAB color data, imread converts it to ICCLAB before bringing it into the MATLAB workspace because 8- or 16-bit TIFF CIELAB-encoded values use a mixture of signed and unsigned data types that cannot be represented as a single MATLAB array.

  • Data organized into tiles or scanlines


    • YCbCr images are converted into the RGB colorspace.

    • All grayscale images are read as if black=0, white=largest value.

    • 1-bit images are returned as class logical.

    • CIELab images are converted into ICCLab colorspace.

The following are format-specific syntaxes for TIFF files.

A = imread(...) returns color data that uses the RGB, CIELAB, ICCLAB, or CMYK color spaces. If the color image uses the CMYK color space, A is an M-by-N-by-4 array.

[...] = imread(..., 'Param1', value1, 'Param2', value2, ...) uses parameter/value pairs to control the read operation. The following table lists the parameters you can use.

'Index'Positive integer specifying which image to read. For example, if you specify the value 3, imread reads the third image in the file. If you omit this argument, imread reads the first image in the file.

Structure array returned by imfinfo.

Note: When reading images from a multi-image TIFF file, passing the output of imfinfo as the value of the 'Info' argument helps imread locate the images in the file more quickly.

'PixelRegion'Cell array, {Rows, Cols}, specifying the boundaries of the region. Rows and Cols must be either two- or three-element vectors. If you specify two elements, the values denote the 1-based indices [start stop]. If you specify three elements, the values denote the 1-based indices [start increment stop], to allow image downsampling.

For copyright information, see the libtiffcopyright.txt file.

XWD — X Window Dump

The following table lists the supported bitdepths, compression, and output classes for XWD files.

Supported BitdepthsZPixmapsXYBitmapsXYPixmapsOutput Class

Class Support

For most image file formats, imread uses 8 or fewer bits per color plane to store image pixels. The following table lists the class of the returned array for the data types used by the file formats.

Data Type Used in File

Class of Array Returned by imread

1-bit per pixel


2- to 8-bits per color plane


9- to 16-bit per pixel

uint16 (BMP, JPEG, PNG, and TIFF)

For the 16-bit BMP packed format (5-6-5), MATLAB returns uint8

    Note   For indexed images, imread always reads the colormap into an array of class double, even though the image array itself may be of class uint8 or uint16.


expand all

Read and Display Image

Read a sample image.

A = imread('ngc6543a.jpg');

imread returns a 650-by-600-by-3 array, A.

Display the image.


Convert Indexed Image to RGB

Read an image and convert it to an RGB image.

Read the first image in the sample indexed image file, corn.tif.

[X,map] = imread('corn.tif');

X is a 415-by-312 array of type uint8.

Verify that the colormap, map, is not empty, and convert the data in X to RGB.

if ~isempty(map)
    Im = ind2rgb(X,map);

View the size and class of X.

whos Im
  Name        Size                 Bytes  Class     Attributes

  Im        415x312x3            3107520  double              

X is now a 415-by-312-by-3 array of type double.

Read Specific Image in Multi-Page TIFF File

Read the third image in the sample file, corn.tif.

[X,map] = imread('corn.tif',3);

Return Alpha Channel of PNG Image

Return the alpha channel of the sample image, peppers.png.

[X,map,alpha] = imread('peppers.png');
alpha =


No alpha channel is present, so alpha is empty.

Read Specified Region of TIFF Image

Read a specific region of pixels of the sample image, corn.tif.

Specify the 'PixelRegion' parameter with a cell array of vectors indicating the boundaries of the region to read. The first vector specifies the range of rows to read, and the second vector specifies the range of columns to read.

A = imread('corn.tif','PixelRegion',{[1,2],[2,5]});

imread reads the image data in rows 1-2 and columns 2-5 from corn.tif and returns the 2-by-4 array, A.

More About

expand all


  • Bit depth is the number of bits used to represent each image pixel. Bit depth is calculated by multiplying the bits-per-sample with the samples-per-pixel. Thus, a format that uses 8-bits for each color component (or sample) and three samples per pixel has a bit depth of 24. Sometimes the sample size associated with a bit depth can be ambiguous: does a 48-bit bit depth represent six 8-bit samples, four 12-bit samples, or three 16-bit samples? See Format-Specific Information for sample size information to avoid this ambiguity.

Was this topic helpful?