PySide6.QtGui.QImage

class QImage

QImage 类提供了一个与硬件无关的图像表示,允许直接访问像素数据,并且可以用作绘图设备。更多

PySide6.QtGui.QImage 的继承图

概要

方法

静态函数

注意

本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。

详细描述

警告

本节包含从C++自动翻译到Python的代码片段,可能包含错误。

Qt 提供了四个类来处理图像数据:QImageQPixmapQBitmapQPictureQImage 是为 I/O 和直接像素访问及操作而设计和优化的,而 QPixmap 则是为在屏幕上显示图像而设计和优化的。QBitmap 只是一个继承自 QPixmap 的便利类,确保深度为 1。最后,QPicture 类是一个记录和重放 QPainter 命令的绘图设备。

因为 QImageQPaintDevice 的子类,所以可以使用 QPainter 直接在图像上绘制。当在 QImage 上使用 QPainter 时,绘制操作可以在与当前GUI线程不同的线程中执行。

QImage 类支持由 Format 枚举描述的几种图像格式。这些包括单色、8位、32位和带有alpha混合的图像,这些格式在Qt 4.x的所有版本中都可用。

QImage 提供了一系列函数,可用于获取有关图像的各种信息。还有一些函数可以用于图像的转换。

QImage 对象可以通过值传递,因为 QImage 类使用了隐式数据共享。QImage 对象也可以被流化和比较。

注意

如果你想在Qt的静态构建中加载QImage对象,请参考插件指南。

警告

不支持在格式为Format_Indexed8Format_CMYK8888QImage上进行绘画。

读取和写入图像文件

QImage 提供了几种加载图像文件的方式:可以在构造 QImage 对象时加载文件,或者稍后使用 load() 或 loadFromData() 函数加载。QImage 还提供了静态的 fromData() 函数,从给定的数据构造一个 QImage。加载图像时,文件名可以引用磁盘上的实际文件,也可以引用应用程序的嵌入式资源之一。有关如何在应用程序的可执行文件中嵌入图像和其他资源文件的详细信息,请参阅 Qt 资源系统概述。

只需调用save()函数即可保存一个QImage对象。

支持的文件格式的完整列表可以通过supportedImageFormats()supportedImageFormats()函数获取。新的文件格式可以作为插件添加。默认情况下,Qt支持以下格式:

格式

描述

Qt的支持

BMP

Windows 位图

读取/写入

GIF

图形交换格式(可选)

读取

JPG

联合图像专家组

读取/写入

JPEG

联合图像专家组

读/写

PNG

便携式网络图形

读取/写入

PBM

便携式位图

读取

PGM

便携式灰度图

读取

PPM

便携式像素图

读取/写入

XBM

X11位图

读取/写入

XPM

X11 位图

读取/写入

图像信息

QImage 提供了一系列函数,可用于获取有关图像的各种信息:

可用函数

几何

size()width()height()、dotsPerMeterX() 和 dotsPerMeterY() 函数提供有关图像大小和宽高比的信息。

rect() 函数返回图像的包围矩形。valid() 函数告诉给定的坐标对是否在此矩形内。offset() 函数返回图像相对于其他图像定位时预期的偏移像素数,这也可以使用 setOffset() 函数进行操作。

颜色

通过将像素的坐标传递给pixel()函数,可以检索像素的颜色。pixel()函数返回的颜色是一个QRgb值,与图像的格式无关。

对于单色和8位图像,colorCount()和colorTable()函数提供了用于存储图像数据的颜色组件的信息:colorTable()函数返回图像的整个颜色表。要获取单个条目,请使用pixelIndex()函数检索给定坐标对的像素索引,然后使用color()函数检索颜色。请注意,如果您手动创建8位图像,您还必须在图像上设置有效的颜色表。

hasAlphaChannel()函数告诉图像的格式是否尊重alpha通道。allGray()和isGrayscale()函数告诉图像的颜色是否都是灰色调。

另请参阅Pixel ManipulationImage Transformations部分。

文本

text() 函数返回与给定文本键关联的图像文本。可以使用 textKeys() 函数检索图像的文本键。使用 setText() 函数来更改图像的文本。

低级信息

depth() 函数返回图像的深度。支持的深度有1(单色)、8、16、24和32位。bitPlaneCount() 函数告诉您使用了多少位。更多信息请参见 Image Formats 部分。

format()、bytesPerLine() 和 sizeInBytes() 函数提供有关图像中存储数据的低级信息。

cacheKey() 函数返回一个唯一标识此 QImage 对象内容的数字。

像素操作

用于操作图像像素的函数取决于图像格式。原因是单色和8位图像是基于索引的,并使用颜色查找表,而32位图像直接存储ARGB值。有关图像格式的更多信息,请参见Image Formats部分。

在32位图像的情况下,可以使用setPixel()函数将给定坐标处的像素颜色更改为指定为ARGB四元组的任何其他颜色。要生成合适的QRgb值,请使用qRgb()(将默认的alpha分量添加到给定的RGB值中,即创建不透明颜色)或qRgba()函数。例如:

32位

qimage-32bit_scaled1

image = QImage(3, 3, QImage.Format_RGB32)
value = QRgb()
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)

对于8位单色图像,像素值只是图像颜色表中的一个索引。因此,setPixel()函数只能用于将给定坐标处的像素颜色更改为图像颜色表中的预定义颜色,即它只能更改像素的索引值。要更改或向图像的颜色表添加颜色,请使用setColor()函数。

颜色表中的条目是一个编码为QRgb值的ARGB四元组。使用qRgb()qRgba()函数生成适合与setColor()函数一起使用的QRgb值。例如:

8位

qimage-8bit_scaled2

image = QImage(3, 3, QImage.Format_Indexed8)
value = QRgb()
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)

对于每个颜色通道超过8位的图像。可以使用setPixelColor()pixelColor()方法来设置和获取QColor值。

QImage 还提供了 scanLine() 函数,该函数返回指向给定索引处扫描线的像素数据的指针,以及 bits() 函数,该函数返回指向第一个像素数据的指针(这相当于 scanLine(0))。

图像格式

存储在QImage中的每个像素都由一个整数表示。整数的大小根据格式而变化。QImage支持由Format枚举描述的几种图像格式。

单色图像使用1位索引存储在最多包含两种颜色的颜色表中。有两种不同类型的单色图像:大端序(MSB优先)或小端序(LSB优先)位顺序。

8位图像使用8位索引存储到颜色表中,即每个像素有一个字节。颜色表是一个QList< QRgb >,而QRgb类型定义等同于一个包含ARGB四元组的无符号整数,格式为0xAARRGGBB。

32位图像没有颜色表;相反,每个像素包含一个QRgb值。有三种不同类型的32位图像,分别存储RGB(即0xffRRGGBB)、ARGB和预乘ARGB值。在预乘格式中,红色、绿色和蓝色通道乘以alpha分量除以255。

可以使用format()函数检索图像的格式。使用convertToFormat()函数将图像转换为另一种格式。allGray()和isGrayscale()函数可以判断彩色图像是否可以安全地转换为灰度图像。

图像变换

QImage 支持多种函数来创建原始图像的变换版本:createAlphaMask() 函数从图像的 alpha 缓冲区构建并返回一个 1-bpp 的掩码,而 createHeuristicMask() 函数则为该图像创建并返回一个 1-bpp 的启发式掩码。后一个函数的工作原理是从一个角落选择一种颜色,然后从所有边缘开始去除该颜色的像素。

mirrored() 函数返回图像在所需方向上的镜像,scaled() 返回按所需尺寸缩放的图像副本,而 rgbSwapped() 函数从 RGB 图像构建 BGR 图像。

scaledToWidth()scaledToHeight() 函数返回图像的缩放副本。

transformed() 函数返回一个经过给定变换矩阵和变换模式变换后的图像副本:在内部,变换矩阵会被调整以补偿不需要的平移,即 transformed() 返回包含原始图像所有变换点的最小图像。静态函数 trueMatrix() 返回用于变换图像的实际矩阵。

还有一些用于就地更改图像属性的函数:

函数

描述

setDotsPerMeterX()

通过设置水平方向上每米物理长度内的像素数来定义宽高比。

setDotsPerMeterY()

通过设置垂直方向上每米物理长度内的像素数来定义宽高比。

fill()

用给定的像素值填充整个图像。

invertPixels()

使用给定的InvertMode值反转图像中的所有像素值。

setColorTable()

设置用于转换颜色索引的颜色表。仅适用于单色和8位格式。

setColorCount()

调整颜色表的大小。仅适用于单色和8位格式。

另请参阅

QImageReader QImageWriter QPixmap QSvgRendererImage Composition ExampleScribble Example

class InvertMode

此枚举类型用于描述在invertPixels()函数中应如何反转像素值。

常量

描述

QImage.InvertRgb

仅反转RGB值并保持alpha通道不变。

QImage.InvertRgba

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

另请参阅

invertPixels()

class Format

Qt中可用的图像格式如下。请参阅表格后的注释。

常量

描述

QImage.Format_Invalid

图像无效。

QImage.Format_Mono

图像使用每像素1位存储。字节以最高有效位(MSB)优先打包。

QImage.Format_MonoLSB

图像使用每像素1位存储。字节以最低有效位(LSB)优先打包。

QImage.Format_Indexed8

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

QImage.Format_RGB32

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

QImage.Format_ARGB32

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

QImage.Format_ARGB32_Premultiplied

图像使用预乘的32位ARGB格式(0xAARRGGBB)存储,即红色、绿色和蓝色通道乘以alpha分量除以255。(如果RR、GG或BB的值高于alpha通道,则结果未定义。)某些操作(例如使用alpha混合的图像合成)在使用预乘的ARGB32时比使用普通的ARGB32更快。

QImage.Format_RGB16

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

QImage.Format_ARGB8565_Premultiplied

图像使用预乘的24位ARGB格式(8-5-6-5)存储。

QImage.Format_RGB666

图像使用24位RGB格式(6-6-6)存储。未使用的最高有效位始终为零。

QImage.Format_ARGB6666_Premultiplied

图像使用预乘的24位ARGB格式(6-6-6-6)存储。

QImage.Format_RGB555

图像使用16位RGB格式(5-5-5)存储。未使用的最高有效位始终为零。

QImage.Format_ARGB8555_Premultiplied

图像使用预乘的24位ARGB格式(8-5-5-5)存储。

QImage.Format_RGB888

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

QImage.Format_RGB444

图像使用16位RGB格式(4-4-4)存储。未使用的位始终为零。

QImage.Format_ARGB4444_Premultiplied

图像使用预乘的16位ARGB格式(4-4-4-4)存储。

QImage.Format_RGBX8888

图像使用32位字节顺序的RGB(x)格式(8-8-8-8)存储。这与Format_RGBA8888相同,只是alpha必须始终为255。

QImage.Format_RGBA8888

图像使用32位字节顺序的RGBA格式(8-8-8-8)存储。与ARGB32不同,这是一种字节顺序格式,这意味着32位编码在大端和小端架构之间有所不同,分别为(0xRRGGBBAA)和(0xAABBGGRR)。如果读取为字节0xRR,0xGG,0xBB,0xAA,则颜色的顺序在任何架构上都是相同的。

QImage.Format_RGBA8888_Premultiplied

图像使用预乘的32位字节顺序RGBA格式(8-8-8-8)存储。

QImage.Format_BGR30

图像使用32位BGR格式(x-10-10-10)存储。

QImage.Format_A2BGR30_Premultiplied

图像使用32位预乘ABGR格式(2-10-10-10)存储。

QImage.Format_RGB30

图像使用32位RGB格式(x-10-10-10)存储。

QImage.Format_A2RGB30_Premultiplied

图像使用32位预乘ARGB格式(2-10-10-10)存储。

QImage.Format_Alpha8

图像仅使用8位alpha格式存储。

QImage.Format_Grayscale8

图像使用8位灰度格式存储。

QImage.Format_Grayscale16

图像使用16位灰度格式存储。

QImage.Format_RGBX64

图像使用64位半字顺序的RGB(x)格式(16-16-16-16)存储。这与Format_RGBA64相同,只是alpha必须始终为65535。

QImage.Format_RGBA64

图像使用64位半字顺序的RGBA格式(16-16-16-16)存储。

QImage.Format_RGBA64_Premultiplied

图像使用预乘的64位半字顺序RGBA格式(16-16-16-16)存储。

QImage.Format_BGR888

图像使用24位BGR格式存储。

QImage.Format_RGBX16FPx4

图像使用四个16位半字浮点RGBx格式(16FP-16FP-16FP-16FP)存储。这与Format_RGBA16FPx4相同,只是alpha必须始终为1.0。

QImage.Format_RGBA16FPx4

图像使用四个16位半字浮点RGBA格式(16FP-16FP-16FP-16FP)存储。

QImage.Format_RGBA16FPx4_Premultiplied

图像使用预乘的四16位半字浮点RGBA格式(16FP-16FP-16FP-16FP)存储。

QImage.Format_RGBX32FPx4

图像使用四个32位浮点RGBx格式(32FP-32FP-32FP-32FP)存储。这与Format_RGBA32FPx4相同,只是alpha必须始终为1.0。

QImage.Format_RGBA32FPx4

图像使用四个32位浮点RGBA格式(32FP-32FP-32FP-32FP)存储。

QImage.Format_RGBA32FPx4_Premultiplied

图像使用预乘的四32位浮点RGBA格式(32FP-32FP-32FP-32FP)存储。

QImage.Format_CMYK8888

图像使用32位字节顺序的CMYK格式存储。

注意

不支持使用格式 QImage::Format_Indexed8 或 QImage::Format_CMYK8888 绘制到 QImage 中。

注意

避免直接使用QPainter渲染到大多数这些格式。渲染最好优化为Format_RGB32Format_ARGB32_Premultiplied格式,其次是为Format_RGB16Format_RGBX8888Format_RGBA8888_PremultipliedFormat_RGBX64Format_RGBA64_Premultiplied格式进行渲染。

另请参阅

convertToFormat()

__init__()
__init__(image)
Parameters:

图像QImage

构造给定image的浅拷贝。

有关浅拷贝的更多信息,请参阅隐式数据共享文档。

另请参阅

copy()

__init__(xpm)
Parameters:

xpmchar[]

__init__(size, format)
Parameters:
__init__(fileName[, format=None])
Parameters:
  • fileName – str

  • format – str

__init__(width, height, format)
Parameters:
  • width – int

  • height – int

  • formatFormat

__init__(arg__1, width, height, format)
Parameters:
  • arg__1 – str

  • width – int

  • height – int

  • formatFormat

__init__(arg__1, width, height, bytes_per_line, format)
Parameters:
  • arg__1 – str

  • width – int

  • height – int

  • bytes_per_line – int

  • formatFormat

__init__(data, width, height, format[, cleanupFunction=None[, cleanupInfo=None]])
Parameters:
  • data – 字符串

  • width – int

  • height – int

  • formatFormat

  • cleanupFunctionQImageCleanupFunction

  • cleanupInfovoid

__init__(data, width, height, bytesPerLine, format[, cleanupFunction=None[, cleanupInfo=None]])
Parameters:
  • data – 字符串

  • width – int

  • height – int

  • bytesPerLine – int

  • formatFormat

  • cleanupFunctionQImageCleanupFunction

  • cleanupInfovoid

allGray()
Return type:

布尔

applyColorTransform(transform)
Parameters:

transformQColorTransform

applyColorTransform(transform, format[, flags=Qt.AutoColor])
Parameters:
bitPlaneCount()
Return type:

整数

bytesPerLine()
Return type:

整数

cacheKey()
Return type:

整数

color(i)
Parameters:

i – 整数

Return type:

整数

colorSpace()
Return type:

QColorSpace

colorTable()
Return type:

无符号整数列表

colorTransformed(transform)
Parameters:

transformQColorTransform

Return type:

QImage

colorTransformed(transform, format[, flags=Qt.AutoColor])
Parameters:
Return type:

QImage

constBits()
Return type:

字符串

constScanLine(arg__1)
Parameters:

arg__1 – 整数

Return type:

PyObject

convertTo(f[, flags=Qt.AutoColor])
Parameters:
convertToColorSpace(colorSpace)
Parameters:

colorSpaceQColorSpace

convertToColorSpace(colorSpace, format[, flags=Qt.AutoColor])
Parameters:
convertToFormat(f[, flags=Qt.AutoColor])
Parameters:
Return type:

QImage

convertToFormat(f, colorTable[, flags=Qt.AutoColor])
Parameters:
Return type:

QImage

convertToFormat_helper(format, flags)
Parameters:
Return type:

QImage

convertToFormat_inplace(format, flags)
Parameters:
Return type:

布尔

convertedTo(f[, flags=Qt.AutoColor])
Parameters:
Return type:

QImage

convertedToColorSpace(colorSpace)
Parameters:

colorSpaceQColorSpace

Return type:

QImage

convertedToColorSpace(colorSpace, format[, flags=Qt.AutoColor])
Parameters:
Return type:

QImage

copy([rect=QRect()])
Parameters:

rectQRect

Return type:

QImage

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

返回的图像是从此图像中的位置(rectangle.x(), rectangle.y())复制的,并且将始终具有给定rectangle的大小。

在此图像之外的区域,像素被设置为0。对于32位RGB图像,这意味着黑色;对于32位ARGB图像,这意味着透明黑色;对于8位图像,这意味着颜色表中索引为0的颜色,可以是任何颜色;对于1位图像,这意味着Qt::color0。

如果给定的rectangle是一个空矩形,则复制整个图像。

另请参阅

QImage()

copy(x, y, w, h)
Parameters:
  • x – 整数

  • y – 整数

  • w – 整数

  • h – 整数

Return type:

QImage

这是一个重载函数。

返回的图像是从此图像中的位置 (x, y) 复制的,并且将始终具有给定的 widthheight。在此图像之外的区域,像素被设置为0。

createAlphaMask([flags=Qt.AutoColor])
Parameters:

flagsImageConversionFlag 的组合

Return type:

QImage

createHeuristicMask([clipTight=true])
Parameters:

clipTight – 布尔值

Return type:

QImage

createMaskFromColor(color[, mode=Qt.MaskInColor])
Parameters:
Return type:

QImage

detachMetadata([invalidateCache=false])
Parameters:

invalidateCache – 布尔值

deviceIndependentSize()
Return type:

QSizeF

dotsPerMeterX()
Return type:

整数

dotsPerMeterY()
Return type:

整数

fill(color)
Parameters:

颜色GlobalColor

这是一个重载函数。

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

fill(color)
Parameters:

颜色QColor

这是一个重载函数。

用给定的color填充整个图像。

如果图像的深度为1,当color等于Qt::color1时,图像将被填充为1;否则将被填充为0。

如果图像的深度为8,图像将使用颜色表中与color对应的索引填充(如果存在);否则将填充为0。

fill(pixel)
Parameters:

像素 – 整数

用给定的pixelValue填充整个图像。

如果此图像的深度为1,则仅使用最低位。如果你说fill(0),fill(2)等,图像将填充0。如果你说fill(1),fill(3)等,图像将填充1。如果深度为8,则使用最低的8位,如果深度为16,则使用最低的16位。

如果图像深度高于32位,则结果未定义。

注意

虽然没有相应的值获取器,但pixelIndex()将为索引格式返回相同的值,而pixel()将为RGB32、ARGB32和ARGB32PM格式返回相同的值。

另请参阅

depth() Image 图像变换

format()
Return type:

格式

static fromData(data[, format=None])
Parameters:
  • dataQByteArrayView

  • format – str

Return type:

QImage

这是一个重载函数。

从给定的QByteArray data构造一个QImage

static fromData(data[, format=None])
Parameters:
Return type:

QImage

这是一个重载函数。

从给定的QByteArray data构造一个QImage

hasAlphaChannel()
Return type:

布尔

invertPixels([mode=QImage.InvertMode.InvertRgb])
Parameters:

modeInvertMode

isGrayscale()
Return type:

布尔

isNull()
Return type:

布尔

如果它是空图像,则返回true,否则返回false

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

load(device, format)
Parameters:
Return type:

布尔

load(fileName[, format=None])
Parameters:
  • fileName – str

  • format – str

Return type:

布尔

loadFromData(data[, format=None])
Parameters:
  • dataQByteArrayView

  • format – str

Return type:

布尔

这是一个重载函数。

从给定的QByteArray data加载图像。

loadFromData(data[, format=None])
Parameters:
Return type:

布尔

这是一个重载函数。

从给定的QByteArray data加载图像。

mirror([horizontally=false[, vertically=true]])
Parameters:
  • 水平 – bool

  • 垂直 – bool

根据horizontalvertical设置为true或false,图像在水平和/或垂直方向上进行镜像。

另请参阅

mirrored() 图像 变换

mirrored([horizontally=false[, vertically=true]])
Parameters:
  • 水平方向 – 布尔值

  • 垂直 – bool

Return type:

QImage

mirrored_helper(horizontal, vertical)
Parameters:
  • horizontal – 布尔值

  • vertical – bool

Return type:

QImage

mirrored_inplace(horizontal, vertical)
Parameters:
  • horizontal – 布尔值

  • vertical – bool

offset()
Return type:

QPoint

返回图像在相对于其他图像定位时预期偏移的像素数。

另请参阅

setOffset() 图像 信息

__ne__(image)
Parameters:

图像QImage

Return type:

布尔

如果此图像和给定的image内容不同,则返回true;否则返回false

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

另请参阅

operator=()

__eq__(image)
Parameters:

图像QImage

Return type:

布尔

如果此图像和给定的image具有相同的内容,则返回true;否则返回false

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

另请参阅

operator=()

pixel(pt)
Parameters:

ptQPoint

Return type:

整数

返回给定position处像素的颜色。

如果position无效,则结果未定义。

警告

当用于大规模像素操作时,此函数开销较大。当需要读取许多像素时,请使用 constBits() 或 constScanLine()。

另请参阅

setPixel() valid() Pixel 操作

pixel(x, y)
Parameters:
  • x – 整数

  • y – 整数

Return type:

整数

pixelColor(pt)
Parameters:

ptQPoint

Return type:

QColor

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

如果position无效,则返回一个无效的QColor

警告

当用于大规模像素操作时,此函数成本较高。当需要读取许多像素时,请使用 constBits() 或 constScanLine()。

另请参阅

setPixelColor() setPixel() valid() Pixel 操作

pixelColor(x, y)
Parameters:
  • x – 整数

  • y – 整数

Return type:

QColor

pixelFormat()
Return type:

QPixelFormat

返回Format作为QPixelFormat

pixelIndex(pt)
Parameters:

ptQPoint

Return type:

整数

返回给定position处的像素索引。

如果position无效,或者图像不是调色板图像(depth() > 8),则结果未定义。

另请参阅

valid() depth() Pixel 操作

pixelIndex(x, y)
Parameters:
  • x – 整数

  • y – 整数

Return type:

整数

rect()
Return type:

QRect

返回图像的包围矩形 (0, 0, width() , height() )。

另请参阅

图像 信息

reinterpretAsFormat(f)
Parameters:

f格式

Return type:

布尔

rgbSwap()

交换所有像素的红色和蓝色分量的值,有效地将RGB图像转换为BGR图像。

另请参阅

rgbSwapped() 图像 变换

rgbSwapped()
Return type:

QImage

rgbSwapped_helper()
Return type:

QImage

rgbSwapped_inplace()
save(device[, format=None[, quality=-1]])
Parameters:
  • 设备QIODevice

  • format – str

  • quality – int

Return type:

布尔

save(fileName[, format=None[, quality=-1]])
Parameters:
  • fileName – str

  • format – str

  • quality – int

Return type:

布尔

scaled(s[, aspectMode=Qt.IgnoreAspectRatio[, mode=Qt.FastTransformation]])
Parameters:
Return type:

QImage

返回图像的副本,根据给定的aspectRatioModetransformMode,缩放到由给定size定义的矩形。

../../_images/qimage-scaling1.png
  • 如果 aspectRatioMode 是 Qt::IgnoreAspectRatio,图像将被缩放到 size

  • 如果 aspectRatioMode 是 Qt::KeepAspectRatio,图像将缩放到尽可能大的矩形内,保持宽高比。

  • 如果 aspectRatioMode 是 Qt::KeepAspectRatioByExpanding,图像将被缩放到尽可能小的矩形,该矩形位于 size 之外,同时保持宽高比。

如果给定的size为空,此函数将返回一个空图像。

另请参阅

isNull() 图像 变换

scaled(w, h[, aspectMode=Qt.IgnoreAspectRatio[, mode=Qt.FastTransformation]])
Parameters:
Return type:

QImage

这是一个重载函数。

返回图像的副本,根据给定的aspectRatioModetransformMode,缩放到具有给定widthheight的矩形。

如果widthheight为零或负数,此函数将返回一个空图像。

scaledToHeight(h[, mode=Qt.FastTransformation])
Parameters:
Return type:

QImage

返回图像的缩放副本。返回的图像使用指定的变换mode缩放到给定的height

此函数自动计算图像的宽度,以保持图像的宽高比。

如果给定的height为0或负数,则返回空图像。

另请参阅

Image Transformations

scaledToWidth(w[, mode=Qt.FastTransformation])
Parameters:
Return type:

QImage

返回图像的缩放副本。返回的图像使用指定的变换mode缩放到给定的width

此函数自动计算图像的高度,以保持其宽高比。

如果给定的 width 为 0 或负数,则返回空图像。

另请参阅

图像 变换

setAlphaChannel(alphaChannel)
Parameters:

alphaChannelQImage

setColor(i, c)
Parameters:
  • i – 整数

  • c – 整数

在颜色表中给定的index处设置颜色为给定的colorValue。颜色值是一个ARGB四元组。

如果 index 超出了当前颜色表的大小,则使用 setColorCount() 进行扩展。

另请参阅

colorCount() Pixel 操作

setColorCount(arg__1)
Parameters:

arg__1 – 整数

setColorSpace(colorSpace)
Parameters:

colorSpaceQColorSpace

setColorTable(colors)
Parameters:

颜色 – .无符号整数列表

setDevicePixelRatio(scaleFactor)
Parameters:

scaleFactor – 浮点数

setDotsPerMeterX(arg__1)
Parameters:

arg__1 – 整数

setDotsPerMeterY(arg__1)
Parameters:

arg__1 – 整数

setOffset(offset)
Parameters:

偏移量QPoint

设置图像相对于其他图像定位时预期的像素偏移量为offset

另请参阅

offset() 图像 信息

setPixel(pt, index_or_rgb)
Parameters:
  • ptQPoint

  • index_or_rgb – 整数

将给定position处的像素索引或颜色设置为index_or_rgb

如果图像的格式是单色或调色板,给定的index_or_rgb值必须是图像颜色表中的索引,否则参数必须是QRgb值。

如果 position 不是图像中的有效坐标对,或者在单色和调色板图像的情况下 index_or_rgb >= colorCount(),则结果未定义。

警告

由于内部调用了detach()函数,此函数的开销较大;如果性能是一个问题,我们建议使用scanLine()或bits()直接访问像素数据。

另请参阅

pixel() 像素 操作

setPixel(x, y, index_or_rgb)
Parameters:
  • x – 整数

  • y – 整数

  • index_or_rgb – 整数

setPixelColor(pt, c)
Parameters:

将给定position处的颜色设置为color

如果 position 不是图像中的有效坐标对,或者图像的格式是单色或调色板的,则结果是未定义的。

警告

由于内部调用了detach()函数,此函数的开销较大;如果性能是一个问题,我们建议使用scanLine()或bits()直接访问像素数据。

另请参阅

pixelColor() pixel() Pixel 操作

setPixelColor(x, y, c)
Parameters:
  • x – 整数

  • y – 整数

  • cQColor

setText(key, value)
Parameters:
  • key – str

  • value – str

警告

本节包含从C++自动翻译到Python的代码片段,可能包含错误。

将图像文本设置为给定的 text 并将其与给定的 key 关联。

如果你只想存储一个文本块(即“评论”或只是一个描述),你可以传递一个空键,或者使用像“描述”这样的通用键。

当你调用save()或write()时,图像文本被嵌入到图像数据中。

并非所有图像格式都支持嵌入文本。您可以通过使用supportsOption()来查找特定图像或格式是否支持嵌入文本。我们给出一个示例:

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

你可以使用supportedImageFormats()来找出哪些图像格式对你可用。

另请参阅

text()textKeys()

size()
Return type:

QSize

返回图像的大小,即其width()height()

另请参阅

图像 信息 deviceIndependentSize()

sizeInBytes()
Return type:

整数

smoothScaled(w, h)
Parameters:
  • w – 整数

  • h – 整数

Return type:

QImage

swap(other)
Parameters:

其他QImage

将此图像与other图像交换。此操作非常快速且永远不会失败。

text([key=""])
Parameters:

key – str

Return type:

字符串

textKeys()
Return type:

字符串列表

static toImageFormat(format)
Parameters:

formatQPixelFormat

Return type:

格式

format 转换为 Format

static toPixelFormat(format)
Parameters:

formatFormat

Return type:

QPixelFormat

format 转换为 QPixelFormat

transformed(matrix[, mode=Qt.FastTransformation])
Parameters:
Return type:

QImage

static trueMatrix(matrix, w, h)
Parameters:
Return type:

QTransform

返回用于使用给定的widthheightmatrix转换图像的实际矩阵。

当使用transformed()函数转换图像时,内部会调整变换矩阵以补偿不需要的平移,即transformed()返回包含原始图像所有变换点的最小图像。此函数返回修改后的矩阵,该矩阵正确地将原始图像中的点映射到新图像中。

与其他重载不同,此函数创建的变换矩阵可用于对图像执行透视变换。

另请参阅

图像 变换

valid(pt)
Parameters:

ptQPoint

Return type:

布尔

如果 pos 是图像中的有效坐标对,则返回 true;否则返回 false

另请参阅

rect() contains()

valid(x, y)
Parameters:
  • x – 整数

  • y – 整数

Return type:

布尔