QPixmap
是 PyQt5 中用于处理图像数据的核心类(属于 PyQt5.QtGui
模块),专门用于在屏幕上高效显示图像。它支持从文件、内存或资源系统加载图像,并提供了丰富的图像操作功能。以下是 QPixmap
的详细介绍:
1. 核心功能
- 加载图像:支持常见格式(如 PNG、JPG、BMP 等)。
- 显示图像:用于在控件(如
QLabel
、QPushButton
)中显示图像。 - 图像处理:缩放、裁剪、旋转、透明度调整等。
- 内存优化:利用底层硬件加速,适合频繁绘图的场景。
2. 基本用法
导入模块
python
from PyQt5.QtGui import QPixmap
创建 QPixmap 对象
- 从文件加载:
python
pixmap = QPixmap("image.png") # 直接传入文件路径
- 从资源系统加载(需提前编译
.qrc
文件):python
pixmap = QPixmap(":/images/logo.png") # 资源路径格式为 :/path
- 创建空白图像:
python
pixmap = QPixmap(200, 100) # 宽200像素,高100像素的空白图像 pixmap.fill(Qt.white) # 填充白色(需导入 Qt 模块)
3. 常见属性和方法
图像信息
- 尺寸:
python
width = pixmap.width() # 图像宽度 height = pixmap.height() # 图像高度 size = pixmap.size() # 返回 QSize 对象
- 有效性检查:
python
if not pixmap.isNull(): print("图像加载成功")
图像处理
- 缩放:
python
scaled_pixmap = pixmap.scaled(100, 50) # 缩放到 100x50 # 保持宽高比,使用平滑缩放 scaled_pixmap = pixmap.scaled(100, 100, Qt.KeepAspectRatio, Qt.SmoothTransformation)
- 裁剪:
python
cropped_pixmap = pixmap.copy(10, 10, 50, 50) # 从 (10,10) 裁剪 50x50 区域
- 旋转和镜像:
python
rotated_pixmap = pixmap.transformed(QTransform().rotate(90)) # 旋转90度 mirrored_pixmap = pixmap.transformed(QTransform().scale(-1, 1)) # 水平镜像
- 透明度:
python
pixmap.setMask(pixmap.createMaskFromColor(Qt.white)) # 将白色设为透明
保存图像
python
pixmap.save("output.jpg", quality=90) # 保存为 JPG,质量90%
4. 在控件中显示图像
QLabel 显示图像
python
from PyQt5.QtWidgets import QLabel
label = QLabel()
label.setPixmap(pixmap) # 设置图像
label.show()
QPushButton 设置图标
python
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize
button = QPushButton()
button.setIcon(QIcon(pixmap)) # 设置图标
button.setIconSize(QSize(100, 100)) # 调整图标大小
button.setText("Click Me")
5. 资源系统(嵌入图像到代码)
PyQt5 允许将图像编译到代码中,避免依赖外部文件:
- 创建
.qrc
文件:xml
<RCC> <qresource prefix="/images"> <file>logo.png</file> </qresource> </RCC>
- 编译为 Python 代码:
bash
pyrcc5 resources.qrc -o resources.py
- 在代码中引用:
python
pixmap = QPixmap(":/images/logo.png")
6. 性能优化
- 避免频繁缩放大图:尽量预缩放并缓存结果。
- 使用硬件加速:
QPixmap
默认使用 GPU 加速,适合频繁绘图的场景。 - 内存释放:不再使用的
QPixmap
对象及时置空或删除:python
pixmap = None # 解除引用,触发垃圾回收
7. 与 QImage 的区别
QPixmap | QImage |
---|---|
针对屏幕显示优化(GPU 友好) | 针对像素级操作优化(CPU 友好) |
不支持直接修改像素 | 支持直接读写像素 |
适合在控件中显示 | 适合图像处理算法 |
相互转换
python
# QPixmap → QImage
image = pixmap.toImage()
# QImage → QPixmap
pixmap = QPixmap.fromImage(image)
8. 示例代码
加载并显示图像
python
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtGui import QPixmap
app = QApplication([])
pixmap = QPixmap("photo.jpg")
if not pixmap.isNull():
label = QLabel()
label.setPixmap(pixmap.scaled(400, 300, Qt.KeepAspectRatio))
label.show()
app.exec_()
图像处理(裁剪 + 旋转)
python
from PyQt5.QtGui import QTransform
# 加载图像
pixmap = QPixmap("input.png")
# 裁剪中心区域
cropped = pixmap.copy(pixmap.width()//2 - 50, pixmap.height()//2 - 50, 100, 100)
# 旋转45度
rotated = cropped.transformed(QTransform().rotate(45), Qt.SmoothTransformation)
# 保存结果
rotated.save("output.png")
9. 注意事项
- 路径问题:文件路径错误时,
QPixmap
会返回空对象(isNull() == True
)。 - 跨平台兼容性:不同操作系统对图像格式的支持可能不同,优先使用 PNG。
- 内存占用:大尺寸图像会占用较多内存,需合理管理。
掌握 QPixmap
可以轻松实现复杂的图像显示和操作需求。