簡體   English   中英

PyQt5:去除二維碼中的背景色

[英]PyQt5: Remove background color in in a QrCode

這是 python 中的 QrCode 生成器,通過PyQt5和模塊qrcode我有一個帶有自己顏色的小部件;

我想將 WHITE COLOR 移除為TRANSPARENTNO COLOR

# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qrcode
import sys

class Image(qrcode.image.base.BaseImage):
    def __init__(self, border, width, box_size):
        self.border = border
        self.width = width
        self.box_size = box_size
        size = (width + border * 2) * box_size
        self._image = QImage(size, size, QImage.Format_RGB16)

        # initial image as white
        self._image.fill(Qt.white)

    def pixmap(self):
        return QPixmap.fromImage(self._image)

    def drawrect(self, row, col):
        painter = QPainter(self._image)
        painter.fillRect(
            (col + self.border) * self.box_size,
            (row + self.border) * self.box_size,
            self.box_size, self.box_size,
            QtCore.Qt.black)

class Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setGeometry(100, 100, 300, 300)
        self.label = QLabel(self)
        self.edit = QLineEdit(self)
        self.edit.returnPressed.connect(self.handleTextEntered)
        self.edit.setFont(QFont('Times', 9))
        self.edit.setAlignment(Qt.AlignCenter)
        layout = QVBoxLayout(self)
        layout.addWidget(self.label)
        layout.addWidget(self.edit)
        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def handleTextEntered(self):
        text = self.edit.text()
        qr_image = qrcode.make(text, image_factory = Image).pixmap()
        self.label.setPixmap(qr_image)

app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

我試過這個,但無濟於事:

self._image.fill(Qt.transparent)
self._image.fill(Qt.'rgba(122, 45, 78, 1)')
self._image.fill(None)
self._image.fill('')
self._image.fill()
# self._image.fill() # to remove the

這些嘗試沒有用,...任何想法

我想你可以通過多種方式實現它,對我來說,我主要使用兩種方式來實現它。

# 方法 (1) --> 使用Qt.transparent並使用Qt.TransparentMode與 0 為透明度和 1 不透明...

此處查找有關 doc.qt.io 此頁面的更多信息

在此處輸入圖像描述

# 方法(2)---> 玩弄你的App背景色:

--> 如果您的背景是紅色,則設置self._image.fill(Qt.red) ,例如:

在此處輸入圖像描述

(忽略我添加的保存按鈕只是為了一些不相關主題的測試......)

在這種情況下,在扭曲的QtCore.Qt.blackQtCore.Qt.white

def drawrect(self, row, col):
    painter = QPainter(self._image)
    painter.fillRect(
       (col + self.border) * self.box_size,
       (row + self.border) * self.box_size,
       self.box_size, self.box_size,
       QtCore.Qt.white)

您正在使用支持透明度的圖像格式:

self._image = QImage(size, size, QImage.Format_RGB16)

正如文檔所解釋的:

圖像使用 16 位 RGB 格式 (5-6-5) 存儲。

這意味着它是一個標准(並且非常有限) 的 3 分量色彩空間,其中通道只有紅色(5 位,32 級)、綠色(6 位,64 級)和藍色(5 位,又是 32 級),並且很明顯沒有阿爾法通道。

由於要求只是能夠顯示和保存圖像,所以不需要使用 QImage 並指定格式,QPixmap 肯定更容易和合適,特別是考慮到它原生支持透明度。

class Image(qrcode.image.base.BaseImage):
    def __init__(self, border, width, box_size):
        self.border = border
        self.width = width
        self.box_size = box_size
        size = (width + border * 2) * box_size
        self._image = QPixmap(size, size)
        self._image.fill(Qt.transparent)

    def pixmap(self):
        return self._image

    def drawrect(self, row, col):
        painter = QPainter(self._image)
        painter.fillRect(
            (col + self.border) * self.box_size,
            (row + self.border) * self.box_size,
            self.box_size, self.box_size,
            QtCore.Qt.black)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM