QImage

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

Inheritance diagram of PySide2.QtGui.QImage

概要

函数

静态函数

详细描述

Qt 为处理图像数据提供了 4 个类: QImage , QPixmap , QBitmap and QPicture . QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap 为在屏幕上展示图像而设计 优化的。 QBitmap 只是方便类,继承 QPixmap ,确保 1 深度。最后, QPicture 类是描绘设备,它记录并重演 QPainter 命令。

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

QImage class supports several image formats described by the 格式 枚举。这些包括单色、8 位、32 位且 Alpha 融合图像 (可用于所有 Qt 4.x 版本)。

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.

QImage objects can be passed around by value since the QImage class uses 隐式数据共享 . QImage objects can also be streamed and compared.

注意

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

警告

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

读写图像文件

QImage provides several ways of loading an image file: The file can be loaded when constructing the QImage object, or by using the load() or loadFromData() functions later on. QImage also provides the static fromData() function, constructing a 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 资源系统 overview for details on how to embed images and other resource files in the application’s executable.

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

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

格式

描述

Qt’s support

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 (便携式像素图)

读/写

XBM

X11 Bitmap (X11 位图)

读/写

XPM

X11 Pixmap (X11 像素图)

读/写

图像信息

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

可用函数

几何体

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

rect() function returns the image’s enclosing rectangle. The valid() function tells if a given pair of coordinates is within this rectangle. The 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 setOffset() 函数。

Colors

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

In case of monochrome and 8-bit images, the colorCount() and colorTable() functions provide information about the color components used to store the image data: The colorTable() function returns the image’s entire color table. To obtain a single entry, use the pixelIndex() function to retrieve the pixel index for a given pair of coordinates, then use the 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.

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

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

文本

text() function returns the image text associated with the given text key. An image’s text keys can be retrieved using the textKeys() function. Use the setText() function to alter an image’s text.

低级信息

depth() function returns the depth of the image. The supported depths are 1 (monochrome), 8, 16, 24 and 32 bits. The bitPlaneCount() function tells how many of those bits that are used. For more information see the 图像 格式 章节。

format() , bytesPerLine() ,和 sizeInBytes() functions provide low-level information about the data stored in the image.

cacheKey() function returns a number that uniquely identifies the contents of this 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 位图像, 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 位

qimage-32bit_scaled1

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 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 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 setColor() function. For example:

8 位

qimage-8bit_scaled2

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)
																

For images with more than 8-bit per color-channel. The methods setPixelColor() and pixelColor() can be used to set and get with QColor 值。

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

图像格式

Each pixel stored in a QImage is represented by an integer. The size of the integer varies depending on the format. QImage supports several image formats described by the 格式 枚举。

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 format() function. Use the convertToFormat() functions to convert an image into another format. The allGray() and isGrayscale() functions tell whether a color image can safely be converted to a grayscale image.

图像变换

QImage supports a number of functions for creating a new image that is a transformed version of the original: The createAlphaMask() function builds and returns a 1-bpp mask from the alpha buffer in this image, and the 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.

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

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

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. transformed() returns the smallest image containing all transformed points of the original image. The static trueMatrix() function returns the actual matrix used for transforming the image.

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

函数

描述

setDotsPerMeterX()

Defines the aspect ratio by setting the number of pixels that fit horizontally in a physical meter.

setDotsPerMeterY()

Defines the aspect ratio by setting the number of pixels that fit vertically in a physical meter.

fill()

Fills the entire image with the given pixel value.

invertPixels()

Inverts all pixel values in the image using the given InvertMode 值。

setColorTable()

Sets the color table used to translate color indexes. Only monochrome and 8-bit formats.

setColorCount()

Resizes the color table. Only monochrome and 8-bit formats.

class QImage

QImage(arg__1, arg__2, arg__3, arg__4)

QImage(arg__1, arg__2, arg__3, arg__4, arg__5)

QImage(arg__1)

QImage(size, format)

QImage(fileName[, format=None])

QImage(xpm)

QImage(width, height, format)

QImage(data, width, height, format[, cleanupFunction=None[, cleanupInfo=None]])

QImage(data, width, height, bytesPerLine, format[, cleanupFunction=None[, cleanupInfo=None]])

param cleanupInfo

void

param xpm

char[]

param cleanupFunction

QImageCleanupFunction

param format

格式

param bytesPerLine

int

param size

QSize

param arg__1

unicode

param arg__2

int

param arg__3

int

param fileName

unicode

param arg__4

格式

param arg__5

格式

param data

uchar

param width

int

param height

int

构造空图像。

另请参阅

isNull()

构造图像采用给定 width , height and format .

A null image will be returned if memory cannot be allocated.

警告

This will create a QImage with uninitialized data. Call fill() to fill the image with an appropriate pixel value before drawing onto it with QPainter .

构造图像采用给定 width , height and format , that uses an existing memory buffer, data width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.

The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.

format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount() or setColorTable() before the image is used.

构造图像采用给定 width , height and format , that uses an existing memory buffer, data width and height must be specified in pixels. bytesPerLine specifies the number of bytes per line (stride).

The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.

format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount() or setColorTable() before the image is used.

PySide2.QtGui.QImage. InvertMode

This enum type is used to describe how pixel values should be inverted in the invertPixels() 函数。

常量

描述

QImage.InvertRgb

Invert only the RGB values and leave the alpha channel unchanged.

QImage.InvertRgba

反转所有通道,包括 Alpha 通道。

另请参阅

invertPixels()

PySide2.QtGui.QImage. 格式

以下图像格式可用于 Qt。见表格后注意事项。

常量

描述

QImage.Format_Invalid

图像无效。

QImage.Format_Mono

The image is stored using 1-bit per pixel. Bytes are packed with the most significant bit (MSB) first.

QImage.Format_MonoLSB

The image is stored using 1-bit per pixel. Bytes are packed with the less significant bit (LSB) first.

QImage.Format_Indexed8

图像使用 8 位索引存储到颜色图。

QImage.Format_RGB32

图像使用 32 位 RGB 格式 (0xffRRGGBB) 存储。

QImage.Format_ARGB32

图像使用 32 位 ARGB 格式 (0xAARRGGBB) 存储。

QImage.Format_ARGB32_Premultiplied

The image is stored using a premultiplied 32-bit ARGB format (0xAARRGGBB), i.e. the red, green, and blue channels are multiplied by the alpha component divided by 255. (If RR, GG, or BB has a higher value than the alpha channel, the results are undefined.) Certain operations (such as image composition using alpha blending) are faster using premultiplied ARGB32 than with plain ARGB32.

QImage.Format_RGB16

图像使用 16 位 RGB 格式 (5-6-5) 存储。

QImage.Format_ARGB8565_Premultiplied

The image is stored using a premultiplied 24-bit ARGB format (8-5-6-5).

QImage.Format_RGB666

The image is stored using a 24-bit RGB format (6-6-6). The unused most significant bits is always zero.

QImage.Format_ARGB6666_Premultiplied

The image is stored using a premultiplied 24-bit ARGB format (6-6-6-6).

QImage.Format_RGB555

The image is stored using a 16-bit RGB format (5-5-5). The unused most significant bit is always zero.

QImage.Format_ARGB8555_Premultiplied

The image is stored using a premultiplied 24-bit ARGB format (8-5-5-5).

QImage.Format_RGB888

图像使用 24 位 RGB 格式 (8-8-8) 存储。

QImage.Format_RGB444

The image is stored using a 16-bit RGB format (4-4-4). The unused bits are always zero.

QImage.Format_ARGB4444_Premultiplied

The image is stored using a premultiplied 16-bit ARGB format (4-4-4-4).

QImage.Format_RGBX8888

The image is stored using a 32-bit byte-ordered RGB(x) format (8-8-8-8). This is the same as the except alpha must always be 255. (added in Qt 5.2)

QImage.Format_RGBA8888

The image is stored using a 32-bit byte-ordered RGBA format (8-8-8-8). Unlike ARGB32 this is a byte-ordered format, which means the 32bit encoding differs between big endian and little endian architectures, being respectively (0xRRGGBBAA) and (0xAABBGGRR). The order of the colors is the same on any architecture if read as bytes 0xRR,0xGG,0xBB,0xAA. (added in Qt 5.2)

QImage.Format_RGBA8888_Premultiplied

The image is stored using a premultiplied 32-bit byte-ordered RGBA format (8-8-8-8). (added in Qt 5.2)

QImage.Format_BGR30

The image is stored using a 32-bit BGR format (x-10-10-10). (added in Qt 5.4)

QImage.Format_A2BGR30_Premultiplied

The image is stored using a 32-bit premultiplied ABGR format (2-10-10-10). (added in Qt 5.4)

QImage.Format_RGB30

图像使用 32 位 RGB 格式 (x-10-10-10) 存储。(在 Qt 5.4 添加)

QImage.Format_A2RGB30_Premultiplied

The image is stored using a 32-bit premultiplied ARGB format (2-10-10-10). (added in Qt 5.4)

QImage.Format_Alpha8

图像使用 8 位仅 alpha 格式存储。(在 Qt 5.5 添加)

QImage.Format_Grayscale8

图像使用 8 位灰度格式存储。(在 Qt 5.5 添加)

QImage.Format_Grayscale16

图像使用 16 位灰度格式存储。(在 Qt 5.13 添加)

QImage.Format_RGBX64

The image is stored using a 64-bit halfword-ordered RGB(x) format (16-16-16-16). This is the same as the except alpha must always be 65535. (added in Qt 5.12)

QImage.Format_RGBA64

图像使用 64 位半字有序 RGBA 格式 (16-16-16-16) 存储。(在 Qt 5.12 添加)

QImage.Format_RGBA64_Premultiplied

The image is stored using a premultiplied 64-bit halfword-ordered RGBA format (16-16-16-16). (added in Qt 5.12)

QImage.Format_BGR888

图像使用 24 位 BGR 格式存储。(在 Qt 5.14 添加)

注意

绘制进 QImage with is not supported.

注意

避免大多数直接渲染到大多数这些格式使用 QPainter 。最好优化渲染针对 Format_RGB32 and Format_ARGB32_Premultiplied 格式,和其次是渲染到 Format_RGB16 , Format_RGBX8888 , Format_RGBA8888_Premultiplied , Format_RGBX64 and Format_RGBA64_Premultiplied formats

PySide2.QtGui.QImage. allGray ( )
返回类型

bool

返回 true if all the colors in the image are shades of gray (i.e. their red, green and blue components are equal); otherwise false.

Note that this function is slow for images without color table.

另请参阅

isGrayscale()

PySide2.QtGui.QImage. alphaChannel ( )
返回类型

QImage

注意

此函数被弃用。

Returns the alpha channel of the image as a new grayscale QImage in which each pixel’s red, green, and blue values are given the alpha value of the original image. The color depth of the returned image is 8-bit.

You can see an example of use of this function in QPixmap ‘s alphaChannel() , which works in the same way as this function on QPixmaps.

Most usecases for this function can be replaced with QPainter and using composition modes.

Note this returns a color-indexed image if you want the alpha channel in the alpha8 format instead use convertToFormat ( Format_Alpha8 ) on the source image.

警告

This is an expensive function.

PySide2.QtGui.QImage. bitPlaneCount ( )
返回类型

int

返回图像位平面数。

The number of bit planes is the number of bits of color and transparency information for each pixel. This is different from (i.e. smaller than) the depth when the image format contains unused bits.

另请参阅

depth() format() 图像 格式

PySide2.QtGui.QImage. byteCount ( )
返回类型

int

注意

此函数被弃用。

Returns the number of bytes occupied by the image data.

Note this method should never be called on an image larger than 2 gigabytes. Instead use sizeInBytes() .

另请参阅

sizeInBytes() bytesPerLine() bits() 图像 Information

PySide2.QtGui.QImage. bytesPerLine ( )
返回类型

int

返回每图像扫描线的字节数。

这相当于 sizeInBytes() / height() if height() is non-zero.

另请参阅

scanLine()

PySide2.QtGui.QImage. cacheKey ( )
返回类型

qint64

Returns a number that identifies the contents of this QImage object. Distinct QImage objects can only have the same key if they refer to the same contents.

键将改变当变更图像时。

PySide2.QtGui.QImage. color ( i )
参数

i int

返回类型

long

Returns the color in the color table at index i . The first color is at index 0.

The colors in an image’s color table are specified as ARGB quadruplets ( QRgb ). Use the qAlpha() , qRed() , qGreen() ,和 qBlue() functions to get the color value components.

另请参阅

setColor() pixelIndex() Pixel 操纵

PySide2.QtGui.QImage. colorSpace ( )
返回类型

QColorSpace

返回图像的色彩空间,若有定义色彩空间。

另请参阅

setColorSpace()

PySide2.QtGui.QImage. colorTable ( )
返回类型

Returns a list of the colors contained in the image’s color table, or an empty list if the image does not have a color table

另请参阅

setColorTable() colorCount() color()

PySide2.QtGui.QImage. constBits ( )
返回类型

uchar

返回第一像素数据指针。

注意: QImage 使用 隐式数据共享 , but this function does not perform a deep copy of the shared pixel data, because the returned data is const.

另请参阅

bits() constScanLine()

PySide2.QtGui.QImage. constScanLine ( arg__1 )
参数

arg__1 int

返回类型

PyObject

Returns a pointer to the pixel data at the scanline with index i . The first scanline is at index 0.

The scanline data is as minimum 32-bit aligned. For 64-bit formats it follows the native alignment of 64-bit integers (64-bit for most platforms, but notably 32-bit on i386).

注意: QImage 使用 隐式数据共享 , but this function does not perform a deep copy of the shared pixel data, because the returned data is const.

另请参阅

scanLine() constBits()

PySide2.QtGui.QImage. convertTo ( f [ , flags=Qt.AutoColor ] )
参数
  • f 格式

  • flags ImageConversionFlags

分离图像并将其转换成给定 format 原位。

指定图像转换 flags 控制转换过程中如何处理图像数据。

另请参阅

convertToFormat()

PySide2.QtGui.QImage. convertToColorSpace ( arg__1 )
参数

arg__1 QColorSpace

将图像转换成 colorSpace .

若图像没有有效色彩空间,方法什么都不做。

PySide2.QtGui.QImage. convertToFormat ( f [ , flags=Qt.AutoColor ] )
参数
  • f 格式

  • flags ImageConversionFlags

返回类型

QImage

PySide2.QtGui.QImage. convertToFormat ( f , colorTable [ , flags=Qt.AutoColor ] )
参数
  • f 格式

  • colorTable

  • flags ImageConversionFlags

返回类型

QImage

这是重载函数。

Returns a copy of the image converted to the given format , using the specified colorTable .

Conversion from RGB formats to indexed formats is a slow operation and will use a straightforward nearest color approach, with no dithering.

PySide2.QtGui.QImage. convertToFormat_helper ( format , flags )
参数
  • format 格式

  • flags ImageConversionFlags

返回类型

QImage

PySide2.QtGui.QImage. convertToFormat_inplace ( format , flags )
参数
  • format 格式

  • flags ImageConversionFlags

返回类型

bool

PySide2.QtGui.QImage. convertedToColorSpace ( arg__1 )
参数

arg__1 QColorSpace

返回类型

QImage

返回图像转换到 colorSpace .

若图像没有有效色彩空间,null QImage 被返回。

PySide2.QtGui.QImage. copy ( [ rect=QRect() ] )
参数

rect QRect

返回类型

QImage

将图像的子区域作为新图像返回。

The returned image is copied from the position ( rectangle .x(), rectangle .y()) in this image, and will always have the size of the given rectangle .

In areas beyond this image, pixels are set to 0. For 32-bit RGB images, this means black; for 32-bit ARGB images, this means transparent black; for 8-bit images, this means the color with index 0 in the color table which can be anything; for 1-bit images, this means color0 .

若给定 rectangle is a null rectangle the entire image is copied.

另请参阅

QImage()

PySide2.QtGui.QImage. copy ( x , y , w , h )
参数
  • x int

  • y int

  • w int

  • h int

返回类型

QImage

这是重载函数。

The returned image is copied from the position ( x , y ) in this image, and will always have the given width and height . In areas beyond this image, pixels are set to 0.

PySide2.QtGui.QImage. createAlphaMask ( [ flags=Qt.AutoColor ] )
参数

flags ImageConversionFlags

返回类型

QImage

Builds and returns a 1-bpp mask from the alpha buffer in this image. Returns a null image if the image’s format is Format_RGB32 .

flags 自变量是按位 OR 的 ImageConversionFlags , and controls the conversion process. Passing 0 for flags sets all the default options.

The returned image has little-endian bit order (i.e. the image’s format is Format_MonoLSB ), which you can convert to big-endian ( Format_Mono ) using the convertToFormat() 函数。

另请参阅

createHeuristicMask() 图像 变换

PySide2.QtGui.QImage. createHeuristicMask ( [ clipTight=true ] )
参数

clipTight bool

返回类型

QImage

Creates and returns a 1-bpp heuristic mask for this image.

The function works by selecting a color from one of the corners, then chipping away pixels of that color starting at all the edges. The four corners vote for which color is to be masked away. In case of a draw (this generally means that this function is not applicable to the image), the result is arbitrary.

The returned image has little-endian bit order (i.e. the image’s format is Format_MonoLSB ), which you can convert to big-endian ( Format_Mono ) using the convertToFormat() 函数。

clipTight is true (the default) the mask is just large enough to cover the pixels; otherwise, the mask is larger than the data pixels.

Note that this function disregards the alpha buffer.

另请参阅

createAlphaMask() 图像 变换

PySide2.QtGui.QImage. createMaskFromColor ( color [ , mode=Qt.MaskInColor ] )
参数
  • color – long

  • mode MaskMode

返回类型

QImage

Creates and returns a mask for this image based on the given color value. If the mode is MaskInColor (the default value), all pixels matching color will be opaque pixels in the mask. If mode is MaskOutColor, all pixels matching the given color will be transparent.

PySide2.QtGui.QImage. dotsPerMeterX ( )
返回类型

int

Returns the number of pixels that fit horizontally in a physical meter. Together with dotsPerMeterY() , this number defines the intended scale and aspect ratio of the image.

另请参阅

setDotsPerMeterX() 图像 Information

PySide2.QtGui.QImage. dotsPerMeterY ( )
返回类型

int

Returns the number of pixels that fit vertically in a physical meter. Together with dotsPerMeterX() , this number defines the intended scale and aspect ratio of the image.

另请参阅

setDotsPerMeterY() 图像 Information

PySide2.QtGui.QImage. fill ( color )
参数

color GlobalColor

这是重载函数。

填充图像采用给定 color ,描述为标准全局颜色。

PySide2.QtGui.QImage. fill ( color )
参数

color QColor

PySide2.QtGui.QImage. fill ( pixel )
参数

pixel uint

填充整个图像采用给定 pixelValue .

If the depth of this image is 1, only the lowest bit is used. If you say fill(0), fill(2), etc., the image is filled with 0s. If you say fill(1), fill(3), etc., the image is filled with 1s. If the depth is 8, the lowest 8 bits are used and if the depth is 16 the lowest 16 bits are used.

注意: pixel() returns the color of the pixel at the given coordinates while QColor::pixel() returns the pixel value of the underlying window system (essentially an index value), so normally you will want to use pixel() to use a color from an existing image or rgb() to use a specific color.

另请参阅

depth() 图像 变换

PySide2.QtGui.QImage. format ( )
返回类型

格式

返回图像格式。

另请参阅

图像 格式

static PySide2.QtGui.QImage. fromData ( data [ , format=None ] )
参数
  • data QByteArray

  • format – str

返回类型

QImage

这是重载函数。

加载图像从给定 QByteArray data .

PySide2.QtGui.QImage. hasAlphaChannel ( )
返回类型

bool

返回 true 若图像拥有的格式遵守 Alpha 通道,否则返回 false .

另请参阅

图像 Information

PySide2.QtGui.QImage. invertPixels ( [ mode=InvertRgb ] )
参数

mode InvertMode

反转图像所有像素值。

The given invert mode only have a meaning when the image’s depth is 32. The default mode is InvertRgb , which leaves the alpha channel unchanged. If the mode is InvertRgba , the alpha bits are also inverted.

Inverting an 8-bit image means to replace all pixels using color index i with a pixel using color index 255 minus i . The same is the case for a 1-bit image. Note that the color table is not 改变。

If the image has a premultiplied alpha channel, the image is first converted to an unpremultiplied image format to be inverted and then converted back.

另请参阅

图像 变换

PySide2.QtGui.QImage. isGrayscale ( )
返回类型

bool

对于 32 位图像,此函数相当于 allGray() .

对于颜色索引图像,此函数返回 true 若 color(i) 为 QRgb (i, i, i) 对于颜色表所有索引而言;否则返回 false .

另请参阅

allGray() 图像 格式

PySide2.QtGui.QImage. isNull ( )
返回类型

bool

返回 true 若它是 null 图像,否则返回 false .

null 图像的所有参数都设置为零,并且没有分配的数据。

PySide2.QtGui.QImage. load ( device , format )
参数
  • device QIODevice

  • format – str

返回类型

bool

这是重载函数。

此函数读取 QImage from the given device . This can, for example, be used to load an image directly into a QByteArray .

PySide2.QtGui.QImage. load ( fileName [ , format=None ] )
参数
  • fileName – unicode

  • format – str

返回类型

bool

PySide2.QtGui.QImage. loadFromData ( data [ , aformat=None ] )
参数
  • data QByteArray

  • aformat – str

返回类型

bool

这是重载函数。

加载图像从给定 QByteArray data .

PySide2.QtGui.QImage. mirrored ( [ horizontally=false [ , vertically=true ] ] )
参数
  • horizontally bool

  • vertically bool

返回类型

QImage

PySide2.QtGui.QImage. mirrored_helper ( horizontal , vertical )
参数
  • horizontal bool

  • vertical bool

返回类型

QImage

PySide2.QtGui.QImage. mirrored_inplace ( horizontal , vertical )
参数
  • horizontal bool

  • vertical bool

PySide2.QtGui.QImage. offset ( )
返回类型

QPoint

Returns the number of pixels by which the image is intended to be offset by when positioning relative to other images.

另请参阅

setOffset() 图像 Information

PySide2.QtGui.QImage. __ne__ ( arg__1 )
参数

arg__1 QImage

返回类型

bool

返回 true 若此图像和给定 image 拥有不同内容;否则返回 false .

比较可能很慢,除非有一些明显差异 (譬如:不同宽度),在这种情况下函数会快速返回。

另请参阅

operator=()

PySide2.QtGui.QImage. __eq__ ( arg__1 )
参数

arg__1 QImage

返回类型

bool

返回 true 若此图像和给定 image 拥有相同内容;否则返回 false .

比较可能很慢,除非有一些明显差异 (如:不同大小或格式),在这种情况下函数会快速返回。

另请参阅

operator=()

PySide2.QtGui.QImage. pixel ( pt )
参数

pt QPoint

返回类型

long

返回像素的颜色在给定 position .

position 无效,结果未定义。

警告

This function is expensive when used for massive pixel manipulations. Use constBits() or constScanLine() when many pixels needs to be read.

PySide2.QtGui.QImage. pixel ( x , y )
参数
  • x int

  • y int

返回类型

long

这是重载函数。

返回像素的颜色在坐标 ( x , y ).

PySide2.QtGui.QImage. pixelColor ( pt )
参数

pt QPoint

返回类型

QColor

返回像素的颜色在给定 position 作为 QColor .

position is not valid, an invalid QColor 被返回。

警告

This function is expensive when used for massive pixel manipulations. Use constBits() or constScanLine() when many pixels needs to be read.

PySide2.QtGui.QImage. pixelColor ( x , y )
参数
  • x int

  • y int

返回类型

QColor

这是重载函数。

返回像素的颜色在坐标 ( x , y ) 作为 QColor .

PySide2.QtGui.QImage. pixelFormat ( )
返回类型

QPixelFormat

返回 格式 作为 QPixelFormat

PySide2.QtGui.QImage. pixelIndex ( pt )
参数

pt QPoint

返回类型

int

返回像素索引在给定 position .

position 无效,或图像不是调色板图像 ( depth() > 8), the results are undefined.

另请参阅

valid() depth() Pixel 操纵

PySide2.QtGui.QImage. pixelIndex ( x , y )
参数
  • x int

  • y int

返回类型

int

这是重载函数。

返回像素索引在 ( x , y ).

PySide2.QtGui.QImage. rect ( )
返回类型

QRect

返回封闭矩形 (0, 0, width() , height() ) of the image.

另请参阅

图像 Information

PySide2.QtGui.QImage. reinterpretAsFormat ( f )
参数

f 格式

返回类型

bool

将图像格式改为 format 不改变数据。仅工作于相同深度的格式之间。

返回 true 若成功。

This function can be used to change images with alpha-channels to their corresponding opaque formats if the data is known to be opaque-only, or to change the format of a given image buffer before overwriting it with new data.

警告

The function does not check if the image data is valid in the new format and will still return true if the depths are compatible. Operations on an image with invalid data are undefined.

警告

若图像未分离,这将导致数据被拷贝。

PySide2.QtGui.QImage. rgbSwapped ( )
返回类型

QImage

PySide2.QtGui.QImage. rgbSwapped_helper ( )
返回类型

QImage

PySide2.QtGui.QImage. rgbSwapped_inplace ( )
PySide2.QtGui.QImage. save ( device [ , format=None [ , quality=-1 ] ] )
参数
  • device QIODevice

  • format – str

  • quality int

返回类型

bool

这是重载函数。

此函数写入 QImage 到给定 device .

例如,这可以用于将图像直接保存到 QByteArray :

image = QImage()
ba = QByteArray()
buffer(ba)
buffer.open(QIODevice.WriteOnly)
image.save(buffer, "PNG") # writes image into ba in PNG format
												
PySide2.QtGui.QImage. save ( fileName [ , format=None [ , quality=-1 ] ] )
参数
  • fileName – unicode

  • format – str

  • quality int

返回类型

bool

PySide2.QtGui.QImage. scaled ( s [ , aspectMode=Qt.IgnoreAspectRatio [ , mode=Qt.FastTransformation ] ] )
参数
  • s QSize

  • aspectMode AspectRatioMode

  • mode TransformationMode

返回类型

QImage

Returns a copy of the image scaled to a rectangle defined by the given size according to the given aspectRatioMode and transformMode .

../../_images/qimage-scaling1.png
  • aspectRatioMode is IgnoreAspectRatio , the image is scaled to size .

  • aspectRatioMode is KeepAspectRatio , the image is scaled to a rectangle as large as possible inside size , 维持宽高比。

  • aspectRatioMode is KeepAspectRatioByExpanding , the image is scaled to a rectangle as small as possible outside size , 维持宽高比。

若给定 size 为空,此函数返回 null 图像。

另请参阅

isNull() 图像 变换

PySide2.QtGui.QImage. scaled ( w , h [ , aspectMode=Qt.IgnoreAspectRatio [ , mode=Qt.FastTransformation ] ] )
参数
  • w int

  • h int

  • aspectMode AspectRatioMode

  • mode TransformationMode

返回类型

QImage

这是重载函数。

Returns a copy of the image scaled to a rectangle with the given width and height according to the given aspectRatioMode and transformMode .

width height 为 0 或负数,此函数返回 null 图像。

PySide2.QtGui.QImage. scaledToHeight ( h [ , mode=Qt.FastTransformation ] )
参数
  • h int

  • mode TransformationMode

返回类型

QImage

Returns a scaled copy of the image. The returned image is scaled to the given height using the specified transformation mode .

This function automatically calculates the width of the image so that the ratio of the image is preserved.

若给定 height 为 0 或负数,返回 null 图像。

另请参阅

图像 变换

PySide2.QtGui.QImage. scaledToWidth ( w [ , mode=Qt.FastTransformation ] )
参数
  • w int

  • mode TransformationMode

返回类型

QImage

Returns a scaled copy of the image. The returned image is scaled to the given width using the specified transformation mode .

This function automatically calculates the height of the image so that its aspect ratio is preserved.

若给定 width 为 0 或负数,返回 null 图像。

另请参阅

图像 变换

PySide2.QtGui.QImage. setAlphaChannel ( alphaChannel )
参数

alphaChannel QImage

将此图像的 Alpha 通道设为给定 alphaChannel .

alphaChannel is an 8 bit alpha image, the alpha values are used directly. Otherwise, alphaChannel is converted to 8 bit grayscale and the intensity of the pixel values is used.

If the image already has an alpha channel, the existing alpha channel is multiplied with the new one. If the image doesn’t have an alpha channel it will be converted to a format that does.

操作类似于描绘 alphaChannel as an alpha image over this image using QPainter::CompositionMode_DestinationIn .

另请参阅

hasAlphaChannel() alphaChannel() 图像 变换 图像 格式

PySide2.QtGui.QImage. setColor ( i , c )
参数
  • i int

  • c – long

设置颜色在给定 index 在颜色表中,到给定 colorValue 。颜色值是 ARGB 四元组。

index 超出颜色表当前大小,展开它采用 setColorCount() .

另请参阅

color() colorCount() setColorTable() Pixel 操纵

PySide2.QtGui.QImage. setColorCount ( arg__1 )
参数

arg__1 int

重置颜色表大小以包含 colorCount 条目。

若颜色表被展开,所有额外颜色将被设为透明 (即 qRgba (0, 0, 0, 0)).

When the image is used, the color table must be large enough to have entries for all the pixel/index values present in the image, otherwise the results are undefined.

另请参阅

colorCount() colorTable() setColor() 图像 变换

PySide2.QtGui.QImage. setColorSpace ( arg__1 )
参数

arg__1 QColorSpace

将图像色彩空间设为 colorSpace 不对图像数据履行任何转换。

另请参阅

colorSpace()

PySide2.QtGui.QImage. setColorTable ( colors )
参数

colors

将用于翻译颜色索引的颜色表设为 QRgb 值,到指定 colors .

When the image is used, the color table must be large enough to have entries for all the pixel/index values present in the image, otherwise the results are undefined.

另请参阅

colorTable() setColor() 图像 变换

PySide2.QtGui.QImage. setDevicePixelRatio ( scaleFactor )
参数

scaleFactor qreal

Sets the device pixel ratio for the image. This is the ratio between image pixels and device-independent pixels.

默认 scaleFactor is 1.0. Setting it to something else has two effects:

QPainters that are opened on the image will be scaled. For example, painting on a 200x200 image if with a ratio of 2.0 will result in effective (device-independent) painting bounds of 100x100.

Code paths in Qt that calculate layout geometry based on the image size will take the ratio into account: QSize layoutSize = image. size() / image. devicePixelRatio() The net effect of this is that the image is displayed as high-DPI image rather than a large image (see 绘制 High Resolution 版本 of Pixmaps and 图像 ).

另请参阅

devicePixelRatio()

PySide2.QtGui.QImage. setDotsPerMeterX ( arg__1 )
参数

arg__1 int

Sets the number of pixels that fit horizontally in a physical meter, to x .

Together with dotsPerMeterY() , this number defines the intended scale and aspect ratio of the image, and determines the scale at which QPainter will draw graphics on the image. It does not change the scale or aspect ratio of the image when it is rendered on other paint devices.

另请参阅

dotsPerMeterX() 图像 Information

PySide2.QtGui.QImage. setDotsPerMeterY ( arg__1 )
参数

arg__1 int

Sets the number of pixels that fit vertically in a physical meter, to y .

Together with dotsPerMeterX() , this number defines the intended scale and aspect ratio of the image, and determines the scale at which QPainter will draw graphics on the image. It does not change the scale or aspect ratio of the image when it is rendered on other paint devices.

另请参阅

dotsPerMeterY() 图像 Information

PySide2.QtGui.QImage. setOffset ( arg__1 )
参数

arg__1 QPoint

Sets the number of pixels by which the image is intended to be offset by when positioning relative to other images, to offset .

另请参阅

offset() 图像 Information

PySide2.QtGui.QImage. setPixel ( pt , index_or_rgb )
参数
  • pt QPoint

  • index_or_rgb uint

设置像素索引或颜色在给定 position to index_or_rgb .

If the image’s format is either monochrome or paletted, the given index_or_rgb value must be an index in the image’s color table, otherwise the parameter must be a QRgb 值。

position is not a valid coordinate pair in the image, or if index_or_rgb >= colorCount() in the case of monochrome and paletted images, the result is undefined.

警告

This function is expensive due to the call of the internal detach() function called within; if performance is a concern, we recommend the use of scanLine() or bits() to access pixel data directly.

另请参阅

pixel() Pixel 操纵

PySide2.QtGui.QImage. setPixel ( x , y , index_or_rgb )
参数
  • x int

  • y int

  • index_or_rgb uint

这是重载函数。

设置像素索引或颜色在 ( x , y ) 到 index_or_rgb .

PySide2.QtGui.QImage. setPixelColor ( pt , c )
参数

设置颜色在给定 position to color .

position is not a valid coordinate pair in the image, or the image’s format is either monochrome or paletted, the result is undefined.

警告

This function is expensive due to the call of the internal detach() function called within; if performance is a concern, we recommend the use of scanLine() or bits() to access pixel data directly.

另请参阅

pixelColor() pixel() bits() scanLine() Pixel 操纵

PySide2.QtGui.QImage. setPixelColor ( x , y , c )
参数

这是重载函数。

设置像素颜色在 ( x , y ) 到 color .

PySide2.QtGui.QImage. setText ( key , value )
参数
  • key – unicode

  • value – unicode

将图像文本设为给定 text 并将其关联到给定 key .

If you just want to store a single text block (i.e., a “comment” or just a description), you can either pass an empty key, or use a generic key like “Description”.

图像文本被嵌入图像数据当调用 save() or write() .

并非所有图像格式都支持嵌入文本。可以找出哪些特定图像或格式支持嵌入文本通过使用 supportsOption() . We give an example:

writer = QImageWriter()
writer.setFormat("png")
if writer.supportsOption(QImageIOHandler.Description):
    print "Png supports embedded text"
												

可以使用 supportedImageFormats() to find out which image formats are available to you.

PySide2.QtGui.QImage. size ( )
返回类型

QSize

返回图片大小,即其 width() and height() .

另请参阅

图像 Information

PySide2.QtGui.QImage. sizeInBytes ( )
返回类型

long long

返回图像数据大小 (以字节为单位)。

另请参阅

byteCount() bytesPerLine() bits() 图像 Information

PySide2.QtGui.QImage. smoothScaled ( w , h )
参数
  • w int

  • h int

返回类型

QImage

返回平滑比例缩放图像副本。返回图像拥有尺寸的宽度为 w 按高度 h 像素。

PySide2.QtGui.QImage. swap ( other )
参数

other QImage

交换图像 other 与此图像。此操作非常快且从不失败。

PySide2.QtGui.QImage. text ( [ key="" ] )
参数

key – unicode

返回类型

unicode

PySide2.QtGui.QImage. textKeys ( )
返回类型

字符串列表

返回此图像的文本键。

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

另请参阅

text()

static PySide2.QtGui.QImage. toImageFormat ( format )
参数

format QPixelFormat

返回类型

格式

转换 format 格式

static PySide2.QtGui.QImage. toPixelFormat ( format )
参数

format 格式

返回类型

QPixelFormat

转换 format QPixelFormat

PySide2.QtGui.QImage. transformed ( matrix [ , mode=Qt.FastTransformation ] )
参数
  • matrix QMatrix

  • mode TransformationMode

返回类型

QImage

注意

此函数被弃用。

PySide2.QtGui.QImage. transformed ( matrix [ , mode=Qt.FastTransformation ] )
参数
返回类型

QImage

static PySide2.QtGui.QImage. trueMatrix ( arg__1 , w , h )
参数
返回类型

QMatrix

注意

此函数被弃用。

static PySide2.QtGui.QImage. trueMatrix ( arg__1 , w , h )
参数
返回类型

QTransform

PySide2.QtGui.QImage. valid ( pt )
参数

pt QPoint

返回类型

bool

返回 true if pos 是图像中的有效坐标对;否则返回 false .

另请参阅

rect() contains()

PySide2.QtGui.QImage. valid ( x , y )
参数
  • x int

  • y int

返回类型

bool

这是重载函数。

返回 true if QPoint ( x , y ) 是图像中的有效坐标对;否则返回 false .