简体   繁体   English

PyQt5:去除二维码中的背景色

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

this is a QrCode generator in python via PyQt5 and the module qrcode I have a widget with its own color;这是 python 中的 QrCode 生成器,通过PyQt5和模块qrcode我有一个带有自己颜色的小部件;

I want to remove the WHITE COLOR to TRANSPARENT or NO COLOR我想将 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_())

I tried this, but to no avail:我试过这个,但无济于事:

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

these attempts didn't work,...Any idea这些尝试没有用,...任何想法

You can achieve it in many ways I guess, for me I mostly use 2 ways of achieving that.我想你可以通过多种方式实现它,对我来说,我主要使用两种方式来实现它。

# Method (1) -->go with Qt.transparent and play with Qt.TransparentMode with 0 for transparency and 1 opaque... # 方法 (1) --> 使用Qt.transparent并使用Qt.TransparentMode与 0 为透明度和 1 不透明...

Find more info on doc.qt.io this page here此处查找有关 doc.qt.io 此页面的更多信息

在此处输入图像描述

# Method (2) ---> playing tricks around your App background-color: # 方法(2)---> 玩弄你的App背景色:

--> if your Background is RED then set self._image.fill(Qt.red) , eg: --> 如果您的背景是红色,则设置self._image.fill(Qt.red) ,例如:

在此处输入图像描述

(ignore the save button i added it just for some testings with non related topics...) (忽略我添加的保存按钮只是为了一些不相关主题的测试......)

in this case in twisted QtCore.Qt.black to QtCore.Qt.white在这种情况下,在扭曲的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)

You're using an image format that does not support transparency:您正在使用支持透明度的图像格式:

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

As the documentation explains :正如文档所解释的:

The image is stored using a 16-bit RGB format (5-6-5).图像使用 16 位 RGB 格式 (5-6-5) 存储。

This means that it's a standard (and very limited) 3-components color space where channels are only red (5 bits, 32 levels), green (6 bits, 64 levels) and blue (5 bits, 32 levels again), and clearly no alpha channel.这意味着它是一个标准(并且非常有限) 的 3 分量色彩空间,其中通道只有红色(5 位,32 级)、绿色(6 位,64 级)和蓝色(5 位,又是 32 级),并且很明显没有阿尔法通道。

Since the requirement is only to be able to show and save the image, there's no need to use QImage and specify the format, and QPixmap is certainly more easier and appropriate, especially considering that it natively supports transparency.由于要求只是能够显示和保存图像,所以不需要使用 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