QImage

Synopsis

Functions

Static functions

Detailed Description

The PySide.QtGui.QImage class provides a hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device.

Qt provides four classes for handling image data: PySide.QtGui.QImage , PySide.QtGui.QPixmap , PySide.QtGui.QBitmap and PySide.QtGui.QPicture . PySide.QtGui.QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while PySide.QtGui.QPixmap is designed and optimized for showing images on screen. PySide.QtGui.QBitmap is only a convenience class that inherits PySide.QtGui.QPixmap , ensuring a depth of 1. Finally, the PySide.QtGui.QPicture class is a paint device that records and replays PySide.QtGui.QPainter commands.

Because PySide.QtGui.QImage is a PySide.QtGui.QPaintDevice subclass, PySide.QtGui.QPainter can be used to draw directly onto images. When using PySide.QtGui.QPainter on a PySide.QtGui.QImage , the painting can be performed in another thread than the current GUI thread.

The PySide.QtGui.QImage class supports several image formats described by the QImage.Format enum. These include monochrome, 8-bit, 32-bit and alpha-blended images which are available in all versions of Qt 4.x.

PySide.QtGui.QImage provides a collection of functions that can be used to obtain a variety of information about the image. There are also several functions that enables transformation of the image.

PySide.QtGui.QImage objects can be passed around by value since the PySide.QtGui.QImage class uses implicit data sharing . PySide.QtGui.QImage objects can also be streamed and compared.

Note

If you would like to load PySide.QtGui.QImage objects in a static build of Qt, refer to the Plugin HowTo .

Warning

Painting on a PySide.QtGui.QImage with the format QImage.Format_Indexed8 is not supported.

Reading and Writing Image Files

PySide.QtGui.QImage provides several ways of loading an image file: The file can be loaded when constructing the PySide.QtGui.QImage object, or by using the PySide.QtGui.QImage.load() or PySide.QtGui.QImage.loadFromData() functions later on. PySide.QtGui.QImage also provides the static PySide.QtGui.QImage.fromData() function, constructing a PySide.QtGui.QImage from the given data. When loading an image, the file name can either refer to an actual file on disk or to one of the application’s embedded resources. See The Qt Resource System overview for details on how to embed images and other resource files in the application’s executable.

Simply call the PySide.QtGui.QImage.save() function to save a PySide.QtGui.QImage object.

The complete list of supported file formats are available through the QImageReader.supportedImageFormats() and QImageWriter.supportedImageFormats() functions. New file formats can be added as plugins. By default, Qt supports the following formats:

Format Description Qt’s support
BMP Windows Bitmap Read/write
GIF Graphic Interchange Format (optional) Read
JPG Joint Photographic Experts Group Read/write
JPEG Joint Photographic Experts Group Read/write
PNG Portable Network Graphics Read/write
PBM Portable Bitmap Read
PGM Portable Graymap Read
PPM Portable Pixmap Read/write
TIFF Tagged Image File Format Read/write
XBM X11 Bitmap Read/write
XPM X11 Pixmap Read/write

Image Information

PySide.QtGui.QImage provides a collection of functions that can be used to obtain a variety of information about the image:

  Available Functions
Geometry

The PySide.QtGui.QImage.size() , PySide.QtGui.QImage.width() , PySide.QtGui.QImage.height() , PySide.QtGui.QImage.dotsPerMeterX() , and PySide.QtGui.QImage.dotsPerMeterY() functions provide information about the image size and aspect ratio.

The PySide.QtGui.QImage.rect() function returns the image’s enclosing rectangle. The PySide.QtGui.QImage.valid() function tells if a given pair of coordinates is within this rectangle. The PySide.QtGui.QImage.offset() function returns the number of pixels by which the image is intended to be offset by when positioned relative to other images, which also can be manipulated using the PySide.QtGui.QImage.setOffset() function.
Colors

The color of a pixel can be retrieved by passing its coordinates to the PySide.QtGui.QImage.pixel() function. The PySide.QtGui.QImage.pixel() function returns the color as a QRgb value indepedent of the image’s format.

In case of monochrome and 8-bit images, the PySide.QtGui.QImage.colorCount() and PySide.QtGui.QImage.colorTable() functions provide information about the color components used to store the image data: The PySide.QtGui.QImage.colorTable() function returns the image’s entire color table. To obtain a single entry, use the PySide.QtGui.QImage.pixelIndex() function to retrieve the pixel index for a given pair of coordinates, then use the PySide.QtGui.QImage.color() function to retrieve the color. Note that if you create an 8-bit image manually, you have to set a valid color table on the image as well.

The PySide.QtGui.QImage.hasAlphaChannel() function tells if the image’s format respects the alpha channel, or not. The PySide.QtGui.QImage.allGray() and PySide.QtGui.QImage.isGrayscale() functions tell whether an image’s colors are all shades of gray.

See also the Pixel Manipulation and Image Transformations sections.

Text The PySide.QtGui.QImage.text() function returns the image text associated with the given text key. An image’s text keys can be retrieved using the PySide.QtGui.QImage.textKeys() function. Use the PySide.QtGui.QImage.setText() function to alter an image’s text.
Low-level information

The PySide.QtGui.QImage.depth() function returns the depth of the image. The supported depths are 1 (monochrome), 8, 16, 24 and 32 bits. The PySide.QtGui.QImage.bitPlaneCount() function tells how many of those bits that are used. For more information see the Image Formats section.

The PySide.QtGui.QImage.format() , PySide.QtGui.QImage.bytesPerLine() , and PySide.QtGui.QImage.byteCount() functions provide low-level information about the data stored in the image.

The PySide.QtGui.QImage.cacheKey() function returns a number that uniquely identifies the contents of this PySide.QtGui.QImage object.

Pixel Manipulation

The functions used to manipulate an image’s pixels depend on the image format. The reason is that monochrome and 8-bit images are index-based and use a color lookup table, while 32-bit images store ARGB values directly. For more information on image formats, see the Image Formats section.

In case of a 32-bit image, the PySide.QtGui.QImage.setPixel() function can be used to alter the color of the pixel at the given coordinates to any other color specified as an ARGB quadruplet. To make a suitable QRgb value, use the qRgb() (adding a default alpha component to the given RGB values, i.e. creating an opaque color) or qRgba() function. For example:

32-bit
 
image = QImage(3, 3, QImage.Format_RGB32)

value = qRgb(189, 149, 39)  # 0xffbd9527
image.setPixel(1, 1, value)

value = qRgb(122, 163, 39)  # 0xff7aa327
image.setPixel(0, 1, value)
image.setPixel(1, 0, value)

value = qRgb(237, 187, 51)  # 0xffedba31
image.setPixel(2, 1, value)

In case of a 8-bit and monchrome images, the pixel value is only an index from the image’s color table. So the PySide.QtGui.QImage.setPixel() function can only be used to alter the color of the pixel at the given coordinates to a predefined color from the image’s color table, i.e. it can only change the pixel’s index value. To alter or add a color to an image’s color table, use the PySide.QtGui.QImage.setColor() function.

An entry in the color table is an ARGB quadruplet encoded as an QRgb value. Use the qRgb() and qRgba() functions to make a suitable QRgb value for use with the PySide.QtGui.QImage.setColor() function. For example:

8-bit
 
image = QImage(3, 3, QImage.Format_Indexed8)
value = qRgb(122, 163, 39) # 0xff7aa327
image.setColor(0, value)

value = qRgb(237, 187, 51) # 0xffedba31
image.setColor(1, value)

value = qRgb(189, 149, 39) # 0xffbd9527
image.setColor(2, value)

image.setPixel(0, 1, 0)
image.setPixel(1, 0, 0)
image.setPixel(1, 1, 2)
image.setPixel(2, 1, 1)

PySide.QtGui.QImage also provide the PySide.QtGui.QImage.scanLine() function which returns a pointer to the pixel data at the scanline with the given index, and the PySide.QtGui.QImage.bits() function which returns a pointer to the first pixel data (this is equivalent to scanLine(0) ).

Image Formats

Each pixel stored in a PySide.QtGui.QImage is represented by an integer. The size of the integer varies depending on the format. PySide.QtGui.QImage supports several image formats described by the QImage.Format enum.

Monochrome images are stored using 1-bit indexes into a color table with at most two colors. There are two different types of monochrome images: big endian (MSB first) or little endian (LSB first) bit order.

8-bit images are stored using 8-bit indexes into a color table, i.e. they have a single byte per pixel. The color table is a QVector < QRgb >, and the QRgb typedef is equivalent to an unsigned int containing an ARGB quadruplet on the format 0xAARRGGBB.

32-bit images have no color table; instead, each pixel contains an QRgb value. There are three different types of 32-bit images storing RGB (i.e. 0xffRRGGBB), ARGB and premultiplied ARGB values respectively. In the premultiplied format the red, green, and blue channels are multiplied by the alpha component divided by 255.

An image’s format can be retrieved using the PySide.QtGui.QImage.format() function. Use the PySide.QtGui.QImage.convertToFormat() functions to convert an image into another format. The PySide.QtGui.QImage.allGray() and PySide.QtGui.QImage.isGrayscale() functions tell whether a color image can safely be converted to a grayscale image.

Image Transformations

PySide.QtGui.QImage supports a number of functions for creating a new image that is a transformed version of the original: The PySide.QtGui.QImage.createAlphaMask() function builds and returns a 1-bpp mask from the alpha buffer in this image, and the PySide.QtGui.QImage.createHeuristicMask() function creates and returns a 1-bpp heuristic mask for this image. The latter function works by selecting a color from one of the corners, then chipping away pixels of that color starting at all the edges.

The PySide.QtGui.QImage.mirrored() function returns a mirror of the image in the desired direction, the PySide.QtGui.QImage.scaled() returns a copy of the image scaled to a rectangle of the desired measures, and the PySide.QtGui.QImage.rgbSwapped() function constructs a BGR image from a RGB image.

The PySide.QtGui.QImage.scaledToWidth() and PySide.QtGui.QImage.scaledToHeight() functions return scaled copies of the image.

The PySide.QtGui.QImage.transformed() function returns a copy of the image that is transformed with the given transformation matrix and transformation mode: Internally, the transformation matrix is adjusted to compensate for unwanted translation, i.e. PySide.QtGui.QImage.transformed() returns the smallest image containing all transformed points of the original image. The static PySide.QtGui.QImage.trueMatrix() function returns the actual matrix used for transforming the image.

There are also functions for changing attributes of an image in-place:

Function Description
PySide.QtGui.QImage.setDotsPerMeterX() Defines the aspect ratio by setting the number of pixels that fit horizontally in a physical meter.
PySide.QtGui.QImage.setDotsPerMeterY() Defines the aspect ratio by setting the number of pixels that fit vertically in a physical meter.
PySide.QtGui.QImage.fill() Fills the entire image with the given pixel value.
PySide.QtGui.QImage.invertPixels() Inverts all pixel values in the image using the given QImage.InvertMode value.
PySide.QtGui.QImage.setColorTable() Sets the color table used to translate color indexes. Only monochrome and 8-bit formats.
PySide.QtGui.QImage.setColorCount() Resizes the color table. Only monochrome and 8-bit formats.