QImage

概要

函数

静态函数

详细描述

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 为处理图像数据提供了 4 个类: 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 为在屏幕上展示图像而设计 优化的。 PySide.QtGui.QBitmap 只是方便类,继承 PySide.QtGui.QPixmap ,确保 1 深度。最后, PySide.QtGui.QPicture 类是描绘设备,它记录并重演 PySide.QtGui.QPainter 命令。

因为 PySide.QtGui.QImage PySide.QtGui.QPaintDevice 子类, PySide.QtGui.QPainter 可用于在图像上直接绘制。当使用 PySide.QtGui.QPainter PySide.QtGui.QImage , the painting can be performed in another thread than the current GUI thread.

PySide.QtGui.QImage class supports several image formats described by the QImage.Format 枚举。这些包括单色、8 位、32 位且 Alpha 融合图像 (可用于所有 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 隐式数据共享 . PySide.QtGui.QImage objects can also be streamed and compared.

注意

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

警告

Painting on a PySide.QtGui.QImage with the format QImage.Format_Indexed8 不被支持。

读写图像文件

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 Qt 资源系统 概述,了解如何在应用程序可执行文件中嵌入图像及其它资源文件的细节。

只需调用 PySide.QtGui.QImage.save() function to save a PySide.QtGui.QImage 对象。

可获得支持的文件格式的完整列表,透过 QImageReader.supportedImageFormats() and QImageWriter.supportedImageFormats() functions. New file formats can be added as plugins. By default, Qt supports the following formats:

格式 描述 Qt 支持
BMP Windows 位图 读/写
GIF Graphic Interchange Format (图形互换格式) 可选 Read
JPG Joint Photographic Experts Group (联合摄影专家组) 读/写
JPEG Joint Photographic Experts Group (联合摄影专家组) 读/写
PNG Portable Network Graphics (便携式网络图形) 读/写
PBM Portable Bitmap (便携式位图) Read
PGM Portable Graymap (便携式灰度图) Read
PPM Portable Pixmap (便携式像素图) 读/写
TIFF 标签化图像文件格式 读/写
XBM X11 Bitmap (X11 位图) 读/写
XPM X11 Pixmap (X11 像素图) 读/写

图像信息

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

  可用函数
几何体

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

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() 函数。
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.

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.

另请参阅 Pixel 操纵 and 图像 变换 sections.

文本 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.
低级信息

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 图像 格式 章节。

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

PySide.QtGui.QImage.cacheKey() function returns a number that uniquely identifies the contents of this PySide.QtGui.QImage 对象。

像素操纵

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 图像 格式 章节。

若是 32 位图像, 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 位
 
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() 函数。

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

8 位
 
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) ).

图像格式

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 枚举。

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.

图像变换

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.

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.

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

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:

函数 描述
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 值。
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.