|  | Home · All Classes · Modules

# QImageReader Class Reference
[QtGui module]

The QImageReader class provides a format independent interface for reading images from files or other devices. More...

### Types

- enum ImageReaderError { UnknownError, FileNotFoundError, DeviceError, UnsupportedFormatError, InvalidDataError }

### Methods

- __init__ (self)
- __init__ (self, QIODevice device, QByteArray format = QByteArray())
- __init__ (self, QString fileName, QByteArray format = QByteArray())
- QColor backgroundColor (self)
- bool canRead (self)
- QRect clipRect (self)
- int currentImageNumber (self)
- QRect currentImageRect (self)
- QIODevice device (self)
- ImageReaderError error (self)
- QString errorString (self)
- QString fileName (self)
- QByteArray format (self)
- int imageCount (self)
- bool jumpToImage (self, int imageNumber)
- bool jumpToNextImage (self)
- int loopCount (self)
- int nextImageDelay (self)
- int quality (self)
- QImage read (self)
- bool read (self, QImage image)
- QRect scaledClipRect (self)
- QSize scaledSize (self)
- setBackgroundColor (self, QColor color)
- setClipRect (self, QRect rect)
- setDevice (self, QIODevice device)
- setFileName (self, QString fileName)
- setFormat (self, QByteArray format)
- setQuality (self, int quality)
- setScaledClipRect (self, QRect rect)
- setScaledSize (self, QSize size)
- QSize size (self)
- bool supportsAnimation (self)
- bool supportsOption (self, QImageIOHandler.ImageOption option)
- QString text (self, QString key)
- QStringList textKeys (self)

### Static Methods

- QByteArray imageFormat (QString fileName)
- QByteArray imageFormat (QIODevice device)
- QByteArray-list supportedImageFormats ()

## Detailed Description

The QImageReader class provides a format independent interface for reading images from files or other devices.

The most common way to read images is through QImage and QPixmap's constructors, or by calling QImage.load() and QPixmap.load(). QImageReader is a specialized class which gives you more control when reading images. For example, you can read an image into a specific size by calling setScaledSize(), and you can select a clip rect, effectively loading only parts of an image, by calling setClipRect(). Depending on the underlying support in the image format, this can save memory and speed up loading of images.

To read an image, you start by constructing a QImageReader object. Pass either a file name or a device pointer, and the image format to QImageReader's constructor. You can then set several options, such as the clip rect (by calling setClipRect()) and scaled size (by calling setScaledSize()). canRead() returns the image if the QImageReader can read the image (i.e., the image format is supported and the device is open for reading). Call read() to read the image.

If any error occurs when reading the image, read() will return a null QImage. You can then call error() to find the type of error that occurred, or errorString() to get a human readable description of what went wrong.

Call supportedImageFormats() for a list of formats that QImageReader can read. QImageReader supports all built-in image formats, in addition to any image format plugins that support reading.

Qt supports GIF reading if it is configured that way during installation. If it is, we are required to state that "The Graphics Interchange Format(c) is the Copyright property of CompuServe Incorporated. GIF(sm) is a Service Mark property of CompuServe Incorporated."

Warning: If you are in a country that recognizes software patents and in which Unisys holds a patent on LZW compression and/or decompression and you want to use GIF, Unisys may require you to license that technology. Such countries include Canada, Japan, the US, France, Germany, Italy and the UK.

GIF support may be removed completely in a future version of Qt. We recommend using the MNG or PNG format.

See also QImageWriter, QImageIOHandler, and QImageIOPlugin.

## Type Documentation

### QImageReader.ImageReaderError

This enum describes the different types of errors that can occur when reading images with QImageReader.

Constant | Value | Description
--- | --- | ---
`QImageReader.FileNotFoundError` | `1` | QImageReader was used with a file name, but not file was found with that name. This can also happen if the file name contained no extension, and the file with the correct extension is not supported by Qt.
`QImageReader.DeviceError` | `2` | QImageReader encountered a device error when reading the image. You can consult your particular device for more details on what went wrong.
`QImageReader.UnsupportedFormatError` | `3` | Qt does not support the requested image format.
`QImageReader.InvalidDataError` | `4` | The image data was invalid, and QImageReader was unable to read an image from it. The can happen if the image file is damaged.
`QImageReader.UnknownError` | `0` | An unknown error occurred. If you get this value after calling read(), it is most likely caused by a bug in QImageReader.

## Method Documentation

### QImageReader.__init__ (self)

Constructs an empty QImageReader object. Before reading an image, call setDevice() or setFileName().

### QImageReader.__init__ (self, QIODevice device, QByteArray format = QByteArray())

Constructs a QImageReader object with the device device and the image format format.

### QImageReader.__init__ (self, QString fileName, QByteArray format = QByteArray())

Constructs a QImageReader object with the file name fileName and the image format format.

See also setFileName().

### QColor QImageReader.backgroundColor (self)

Returns the background color that's used when reading an image. If the image format does not support setting the background color an invalid color is returned.

This function was introduced in Qt 4.1.

See also setBackgroundColor() and read().

### bool QImageReader.canRead (self)

Returns true if an image can be read for the device (i.e., the image format is supported, and the device seems to contain valid data); otherwise returns false.

canRead() is a lightweight function that only does a quick test to see if the image data is valid. read() may still return false after canRead() returns true, if the image data is corrupt.

For images that support animation, canRead() returns false when all frames have been read.

See also read() and supportedImageFormats().

### QRect QImageReader.clipRect (self)

Returns the clip rect (also known as the ROI, or Region Of Interest) of the image. If no clip rect has been set, an invalid QRect is returned.

See also setClipRect().

### int QImageReader.currentImageNumber (self)

For image formats that support animation, this function returns the sequence number of the current frame. Otherwise, -1 is returned.

See also supportsAnimation() and QImageIOHandler.currentImageNumber().

### QRect QImageReader.currentImageRect (self)

For image formats that support animation, this function returns the rect for the current frame. Otherwise, a null rect is returned.

See also supportsAnimation() and QImageIOHandler.currentImageRect().

### QIODevice QImageReader.device (self)

Returns the device currently assigned to QImageReader, or 0 if no device has been assigned.

See also setDevice().

### ImageReaderError QImageReader.error (self)

Returns the type of error that occurred last.

See also ImageReaderError and errorString().

### QString QImageReader.errorString (self)

Returns a human readable description of the last error that occurred.

See also error().

### QString QImageReader.fileName (self)

If the currently assigned device is a QFile, or if setFileName() has been called, this function returns the name of the file QImageReader reads from. Otherwise (i.e., if no device has been assigned or the device is not a QFile), an empty QString is returned.

See also setFileName() and setDevice().

### QByteArray QImageReader.format (self)

Returns the format QImageReader uses for reading images.

You can call this function after assigning a device to the reader to determine the format of the device. For example:

     QImageReader reader("image.png");
     // reader.format() == "png"

If the reader cannot read any image from the device (e.g., there is no image there, or the image has already been read), or if the format is unsupported, this function returns an empty QByteArray().

See also setFormat() and supportedImageFormats().

### int QImageReader.imageCount (self)

For image formats that support animation, this function returns the total number of images in the animation.

Certain animation formats do not support this feature, in which case 0 is returned.

See also supportsAnimation() and QImageIOHandler.imageCount().

### QByteArray QImageReader.imageFormat (QString fileName)

If supported, this function returns the image format of the file fileName. Otherwise, an empty string is returned.

### QByteArray QImageReader.imageFormat (QIODevice device)

This is an overloaded member function, provided for convenience.

If supported, this function returns the image format of the device device. Otherwise, an empty string is returned.

### bool QImageReader.jumpToImage (self, int imageNumber)

For image formats that support animation, this function skips to the image whose sequence number is imageNumber, returning true if successful or false if the corresponding image cannot be found.

The next call to read() will attempt to read this image.

See also jumpToNextImage() and QImageIOHandler.jumpToImage().

### bool QImageReader.jumpToNextImage (self)

For image formats that support animation, this function steps over the current image, returning true if successful or false if there is no following image in the animation.

The default implementation calls read(), then discards the resulting image, but the image handler may have a more efficient way of implementing this operation.

See also jumpToImage() and QImageIOHandler.jumpToNextImage().

### int QImageReader.loopCount (self)

For image formats that support animation, this function returns the number of times the animation should loop. Otherwise, it returns -1.

See also supportsAnimation() and QImageIOHandler.loopCount().

### int QImageReader.nextImageDelay (self)

For image formats that support animation, this function returns the number of milliseconds to wait until displaying the next frame in the animation. Otherwise, 0 is returned.

See also supportsAnimation() and QImageIOHandler.nextImageDelay().

### int QImageReader.quality (self)

Returns the quality level of the image.

This function was introduced in Qt 4.2.

See also setQuality().

### QImage QImageReader.read (self)

Reads an image from the device. On success, the image that was read is returned; otherwise, a null QImage is returned. You can then call error() to find the type of error that occurred, or errorString() to get a human readable description of the error.

For image formats that support animation, calling read() repeatedly will return the next frame. When all frames have been read, a null image will be returned.

See also canRead(), supportedImageFormats(), supportsAnimation(), and QMovie.

### bool QImageReader.read (self, QImage image)

This is an overloaded member function, provided for convenience.

Reads an image from the device into image, which must point to a QImage. Returns true on success; otherwise, returns false.

If image has same format and size as the image data that is about to be read, this function may not need to allocate a new image before reading. Because of this, it can be faster than the other read() overload, which always constructs a new image; especially when reading several images with the same format and size.

     QImage icon(64, 64, QImage.Format_RGB32);
     QImageReader reader("icon_64x64.bmp");
     if (reader.read(&icon)) {
         // Display icon
     }

For image formats that support animation, calling read() repeatedly will return the next frame. When all frames have been read, a null image will be returned.

See also canRead(), supportedImageFormats(), supportsAnimation(), and QMovie.

### QRect QImageReader.scaledClipRect (self)

Returns the scaled clip rect of the image.

See also setScaledClipRect().

### QSize QImageReader.scaledSize (self)

Returns the scaled size of the image.

See also setScaledSize().

### QImageReader.setBackgroundColor (self, QColor color)

Sets the background color to color. Image formats that support this operation are expected to initialize the background to color before reading an image.

This function was introduced in Qt 4.1.

See also backgroundColor() and read().

### QImageReader.setClipRect (self, QRect rect)

Sets the image clip rect (also known as the ROI, or Region Of Interest) to rect. The coordinates of rect are relative to the untransformed image size, as returned by size().

See also clipRect(), setScaledSize(), and setScaledClipRect().

### QImageReader.setDevice (self, QIODevice device)

Sets QImageReader's device to device. If a device has already been set, the old device is removed from QImageReader and is otherwise left unchanged.

If the device is not already open, QImageReader will attempt to open the device in QIODevice.ReadOnly mode by calling open(). Note that this does not work for certain devices, such as QProcess, QTcpSocket and QUdpSocket, where more logic is required to open the device.

See also device() and setFileName().

### QImageReader.setFileName (self, QString fileName)

Sets the file name of QImageReader to fileName. Internally, QImageReader will create a QFile object and open it in QIODevice.ReadOnly mode, and use this when reading images.

If fileName does not include a file extension (e.g., .png or .bmp), QImageReader will cycle through all supported extensions until it finds a matching file.

See also fileName(), setDevice(), and supportedImageFormats().

### QImageReader.setFormat (self, QByteArray format)

Sets the format QImageReader will use when reading images, to format. format is a case insensitive text string. Example:

     QImageReader reader;
     reader.setFormat("png"); // same as reader.setFormat("PNG");

You can call supportedImageFormats() for the full list of formats QImageReader supports.

See also format().

### QImageReader.setQuality (self, int quality)

This is an image format specific function that sets the quality level of the image to quality. For image formats that do not support setting the quality, this value is ignored.

The value range of quality depends on the image format. For example, the "jpeg" format supports a quality range from 0 (low quality, high compression) to 100 (high quality, low compression).

This function was introduced in Qt 4.2.

See also quality().

### QImageReader.setScaledClipRect (self, QRect rect)

Sets the scaled clip rect to rect. The scaled clip rect is the clip rect (also known as ROI, or Region Of Interest) that is applied after the image has been scaled.

See also scaledClipRect() and setScaledSize().

### QImageReader.setScaledSize (self, QSize size)

Sets the scaled size of the image to size. The scaling is performed after the initial clip rect, but before the scaled clip rect is applied. The algorithm used for scaling depends on the image format. By default (i.e., if the image format does not support scaling), QImageReader will use QImage.scale() with Qt.SmoothScaling.

See also scaledSize(), setClipRect(), and setScaledClipRect().

### QSize QImageReader.size (self)

Returns the size of the image, without actually reading the image contents.

If the image format does not support this feature, this function returns an invalid size. Qt's built-in image handlers all support this feature, but custom image format plugins are not required to do so.

See also QImageIOHandler.ImageOption, QImageIOHandler.option(), and QImageIOHandler.supportsOption().

### QByteArray-list QImageReader.supportedImageFormats ()

Returns the list of image formats supported by QImageReader.

By default, Qt can read the following formats:

Format | Description
--- | ---
BMP | Windows Bitmap
GIF | Graphic Interchange Format (optional)
JPG | Joint Photographic Experts Group
JPEG | Joint Photographic Experts Group
MNG | Multiple-image Network Graphics
PNG | Portable Network Graphics
PBM | Portable Bitmap
PGM | Portable Graymap
PPM | Portable Pixmap
TIFF | Tagged Image File Format
XBM | X11 Bitmap
XPM | X11 Pixmap

To configure Qt with GIF support, pass `-qt-gif` to the `configure` script or check the appropriate option in the graphical installer.

See also setFormat(), QImageWriter.supportedImageFormats(), and QImageIOPlugin.

### bool QImageReader.supportsAnimation (self)

Returns true if the image format supports animation; otherwise, false is returned.

This function was introduced in Qt 4.1.

See also QMovie.supportedFormats().

### bool QImageReader.supportsOption (self, QImageIOHandler.ImageOption option)

Returns true if the reader supports option; otherwise returns false.

Different image formats support different options. Call this function to determine whether a certain option is supported by the current format. For example, the PNG format allows you to embed text into the image's metadata (see text()), and the BMP format allows you to determine the image's size without loading the whole image into memory (see size()).

     QImageReader reader(":/image.png");
     if (reader.supportsOption(QImageIOHandler.Size))
         qDebug() << "Size:" << reader.size();

This function was introduced in Qt 4.2.

See also QImageWriter.supportsOption().

### QString QImageReader.text (self, QString key)

Returns the image text associated with key.

Support for this option is implemented through QImageIOHandler.Description.

This function was introduced in Qt 4.1.

See also textKeys() and QImageWriter.setText().

### QStringList QImageReader.textKeys (self)

Returns the text keys for this image. You can use these keys with text() to list the image text for a certain key.

Support for this option is implemented through QImageIOHandler.Description.

This function was introduced in Qt 4.1.

See also text(), QImageWriter.setText(), and QImage.textKeys().