简体   繁体   中英

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;

I want to remove the WHITE COLOR to TRANSPARENT or NO 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...

Find more info on doc.qt.io this page here

在此处输入图像描述

# Method (2) ---> playing tricks around your App background-color:

--> if your Background is RED then set self._image.fill(Qt.red) , eg:

在此处输入图像描述

(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

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).

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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