Python PyQt5 教程:QImage 类的使用方法和代码示例

QImage

QImage 与 Widget 不同,它不是用于界面显示的控件,而是为了图像 I/O 或像素级访问而设计的。如果需要访问或修改图像的像素,就需要使用 QImage。实际上,使用 QImage 时通常需要与 QLabel 和 QPixmap 配合使用。

以下是一个基本的使用示例,展示如何将 QImage 与 QLabel 结合使用。首先创建一个 QLabel 控件,并使用 QImage 读取一张图片。由于 QLabel 只支持显示 QPixmap,需要将 QImage 转换为 QPixmap。转换使用 QPixmap.fromImage() 函数,然后通过 QLabel.setPixmap() 方法将转换后的 QPixmap 设置给 QLabel。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
from PyQt5.QtGui import QImage, QPixmap

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('我的窗口')
        self.setGeometry(50, 50, 200, 150)
        layout = QVBoxLayout()
        self.setLayout(layout)
        self.mylabel = QLabel('这是一张图片', self)
        layout.addWidget(self.mylabel)
        self.myqimage = QImage('lena.jpg')
        self.mylabel.setPixmap(QPixmap.fromImage(self.myqimage))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec_())

使用 QImage 来填充特定颜色。

QImage 提供了 fill() 方法来实现这一点。以下示例演示了如何用红色填充一个 320×240 大小的 QImage。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
from PyQt5.QtGui import QImage, QPixmap, qRgb

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # ... 省略其他代码 ...
        self.myqimage = QImage(320, 240, QImage.Format_RGB888)
        self.myqimage.fill(qRgb(255, 0, 0))
        self.mylabel.setPixmap(QPixmap.fromImage(self.myqimage))

# ... 省略其他代码 ...

修改 QImage 的像素

可以使用 setPixel() 方法。以下示例演示了如何遍历每个像素,并将它们设置为蓝色。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
from PyQt5.QtGui import QImage, QPixmap, QColor

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # ... 省略其他代码 ...
        self.myqimage = QImage(320, 240, QImage.Format_RGB888)
        for x in range(self.myqimage.width()):
            for y in range(self.myqimage.height()):
                self.myqimage.setPixel(x, y, QColor(0, 0, 255).rgb())
        # ... 省略其他代码 ...

此外,还可以从 numpy.ndarray 初始化 QImage。以下是一个示例,展示如何使用 numpy 数组来创建 QImage。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
from PyQt5.QtGui import QImage, QPixmap
import numpy as np

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # ... 省略其他代码 ...
        img_np = np.zeros((240, 320, 3), dtype=np.uint8)
        # ... 省略其他代码 ...
        self.myqimage = QImage(img_np.data, img_np.shape[1], img_np.shape[0], 3 * img_np.shape[1], QImage.Format_RGB888)
        # ... 省略其他代码 ...

# ... 省略其他代码 ...

OpenCV 读取图像并转换为 QImage

,OpenCV 的像素格式是 BGR,而 QImage 使用的是 RGB 格式,因此在转换时需要使用 rgbSwapped() 方法。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
from PyQt5.QtGui import QImage, QPixmap
import cv2

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # ... 省略其他代码 ...
        img = cv2.imread('lena.jpg')
        # ... 省略其他代码 ...
        self.myqimage = QImage(img.data, img.shape[1], img.shape[0], 3 * img.shape[1], QImage.Format_RGB888)
        self.mylabel.setPixmap(QPixmap.fromImage(self.myqimage.rgbSwapped()))

# ... 省略其他代码 ...

作者:知来者逆

物联沃分享整理
物联沃-IOTWORD物联网 » Python PyQt5 教程:QImage 类的使用方法和代码示例

发表回复