一 解决的问题
在PyQt或者Pyside中使用QLabel显示OpenCV图像或视频流时,常会出现图像尺寸不能缩放或出现图像自动占满整个QLabel,而图像的长宽比例发生变化。
二 设置图像自动占满整个QLabel
使用如下代码可实现QLabel中的图像的自动调整
from PyQt5.QtWidgets import QLabel, QApplication, QWidget, QVBoxLayout
from PyQt5.QtGui import QPixmap
import sys
class ImageLabel(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 创建一个 QLabel
self.label = QLabel(self)
# 加载图像
pixmap = QPixmap("path/to/your/image.png") # 用你的图片路径替换这里的"path/to/your/image.png"
# 设置图像到 QLabel
self.label.setPixmap(pixmap)
# 启用自动缩放
self.label.setScaledContents(True)
# 设置布局
layout = QVBoxLayout()
layout.addWidget(self.label)
self.setLayout(layout)
# 设置窗口大小和标题
self.setGeometry(100, 100

最低0.47元/天 解锁文章
9275

被折叠的 条评论
为什么被折叠?



