pyqt5设置背景图片

PyQt5设置背景图片

1、打开QTDesigner

创建一个UI,camera.ui

2、创建一个pictures.qrc文件

在ui文件同级目录下先创建一个pictures.txt,填写内容:

复制代码
<RCC>
  <qresource prefix="media">
    <file>1.jpg</file>
  </qresource>
</RCC>

如图:

3、添加背景图片

3.1、选择label控件

并命名为label_video

3.2、选中label,右键->改变样式表->添加资源->background-image

3.3、添加pictures.qrc

3.4、添加前缀

3.5、添加背景图

3.6、保存

4、转py文件

4.1、把UI转成py文件

4.2、把qrc转py文件

4.3、项目目录

5、打开摄像头代码

python 复制代码
# !/usr/bin/python
# -*- coding: utf-8 -*-

"""
@contact: 微信 1257309054
@file: t.py
@time: 2023/9/10 0:16
@author: LDC
"""

import sys

import cv2
from PyQt5 import QtCore
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtGui import QPixmap, QImage

from camera import Ui_MainWindow


class OpenCamera(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(OpenCamera, self).__init__()
        self.setupUi(self)  # 创建窗体对象
        self.init()
        self.label_video.setScaledContents(True)  # 图片自适应

    def init(self):
        # 打开摄像头
        self.btn_open_camera.clicked.connect(self.open_camera)
        # 关闭摄像头
        self.btn_close_camera.clicked.connect(self.close_camera)

    def open_camera(self):
        # 打开摄像头
        self.is_close = False
        cap = cv2.VideoCapture(0)
        w, h = 640, 360

        cap.set(3, w)
        cap.set(4, h)

        while True:

            success, img = cap.read()
            mirrow = cv2.flip(img, 1)
            width, height = mirrow.shape[:2]  # 行:宽,列:高

            if cv2.waitKey(1) == 27:
                break

            # 显示图片
            image_show = cv2.cvtColor(mirrow, cv2.COLOR_BGR2RGB)  # opencv读的通道是BGR,要转成RGB

            self.showImage = QtGui.QImage(image_show.data, height, width, QImage.Format_RGB888)
            self.label_video.setPixmap(QPixmap.fromImage(self.showImage))  # 往显示视频的Label里显示QImage

            # 是否关闭摄像头
            if self.is_close:
                break

        # 释放摄像头 release camera
        cap.release()
        self.label_video.clear()  # 清除label组件上的图片

    def close_camera(self):
        # 关闭摄像头
        self.is_close = True


if __name__ == '__main__':
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)  # 自适应分辨率

    app = QtWidgets.QApplication(sys.argv)
    ui = OpenCamera()
    ui.show()
    sys.exit(app.exec_())

6、效果图

相关推荐
用户805533698033 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner3 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz8 天前
QML Hello World 入门示例
qt
xcyxiner11 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner12 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner12 天前
DicomViewer (添加模型类)3
qt
xcyxiner13 天前
DicomViewer (目录调整) 2
qt
xcyxiner13 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00614 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术14 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript