简体   繁体   English

如何显示透明图像 PyQt QWidget

[英]How to display transparent images PyQt QWidget

I'm using OpenCV for some image processing and want to create a transparent overlay on my screen using PyQt widgets.我正在使用 OpenCV 进行一些图像处理,并希望使用 PyQt 小部件在我的屏幕上创建一个透明的叠加层。 Below I have a basic example of sending a basic frame from opencv to PyQt through a signal/slot and displaying it on the window.下面我有一个基本示例,通过信号/插槽将基本帧从 opencv 发送到 PyQt 并将其显示在窗口上。 The issue is I can't get the transparent background using this method, instead it is just a black background:问题是我无法使用此方法获得透明背景,而只是黑色背景:

from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QVBoxLayout
from PyQt5.QtGui import QPixmap
import sys
import cv2
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QThread
import numpy as np
import os


class VideoThread(QThread):
    change_pixmap_signal = pyqtSignal(np.ndarray)

    def run(self):
        img = np.zeros((500, 500, 4), dtype=np.uint8)
        cv2.rectangle(img, (0, 0), (200, 200), (0, 0, 255), 2)
        while True
            self.change_pixmap_signal.emit(img)
        


class App(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Qt live label demo")
        self.disply_width = 1920
        self.display_height = 1080
        # create the label that holds the image
        self.image_label = QLabel(self)
        self.image_label.resize(self.disply_width, self.display_height)
        # create a text label
        self.textLabel = QLabel('Webcam')
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.setStyleSheet("background-color:transparent;")

        # create a vertical box layout and add the two labels
        vbox = QVBoxLayout()
        vbox.addWidget(self.image_label)
        vbox.addWidget(self.textLabel)
        # set the vbox layout as the widgets layout
        self.setLayout(vbox)

        # create the video capture thread
        self.thread = VideoThread()
        # connect its signal to the update_image slot
        self.thread.change_pixmap_signal.connect(self.update_image)
        # start the thread
        self.thread.start()

    @pyqtSlot(np.ndarray)
    def update_image(self, cv_img):
        """Updates the image_label with a new opencv image"""
        qt_img = self.convert_cv_qt(cv_img)
        self.image_label.setPixmap(qt_img)

    def convert_cv_qt(self, cv_img):
        """Convert from an opencv image to QPixmap"""
        rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
        h, w, ch = rgb_image.shape
        bytes_per_line = ch * w
        convert_to_Qt_format = QtGui.QImage(
            rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)
        p = convert_to_Qt_format.scaled(
            self.disply_width, self.display_height, Qt.KeepAspectRatio)
        return QPixmap.fromImage(p)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    a = App()
    a.show()
    sys.exit(app.exec_())

If it isn't possible to send a frame with an alpha channel to PyQt, I was wondering if it's possible to just send the rectangle 4 point location and use the PyQt paint to draw a rectangle on screen?如果无法将带有 alpha 通道的帧发送到 PyQt,我想知道是否可以只发送矩形 4 点位置并使用 PyQt 绘制在屏幕上绘制矩形? I think this would require a widget.update() but I'm not sure where to invoke that.我认为这需要一个 widget.update() 但我不确定在哪里调用它。

If you are going to use transparencies then the colors and the image must be 4-channel, in your case you are passing a 3-channel color to the cv2.rectangle method and then converting you use a 3-channel format in QImage.如果您打算使用透明胶片,那么颜色和图像必须是 4 通道,在您的情况下,您将 3 通道颜色传递给 cv2.rectangle 方法,然后在 QImage 中使用 3 通道格式进行转换。 On the other hand cv2.rectangle returns the drawn image, it does not modify the input array.另一方面 cv2.rectangle 返回绘制的图像,它不会修改输入数组。

def run(self):
    img = np.zeros((500, 500, 4), dtype=np.uint8)
    output = cv2.rectangle(img, (0, 0), (200, 200), (0, 0, 255, 255), 2)
    while True:
        self.change_pixmap_signal.emit(output)
        QThread.msleep(1)
def convert_cv_qt(self, cv_img):
    h, w, ch = cv_img.shape
    bytes_per_line = ch * w
    convert_to_Qt_format = QtGui.QImage(
        cv_img.data, w, h, bytes_per_line, QtGui.QImage.Format_RGBA8888
    )
    p = convert_to_Qt_format.scaled(
        self.disply_width, self.display_height, Qt.KeepAspectRatio
    )
    return QPixmap.fromImage(p)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM