[英]PyQt5: Remove background color in in a QrCode
这是 python 中的 QrCode 生成器,通过PyQt5
和模块qrcode
我有一个带有自己颜色的小部件;
我想将 WHITE COLOR 移除为TRANSPARENT或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_())
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
这些尝试没有用,...任何想法
我想你可以通过多种方式实现它,对我来说,我主要使用两种方式来实现它。
Qt.transparent
并使用Qt.TransparentMode
与 0 为透明度和 1 不透明...在此处查找有关 doc.qt.io 此页面的更多信息
--> 如果您的背景是红色,则设置self._image.fill(Qt.red)
,例如:
(忽略我添加的保存按钮只是为了一些不相关主题的测试......)
QtCore.Qt.black
到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)
您正在使用不支持透明度的图像格式:
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.