Here's a demo that eliminates the PIL dependency (which is just as well, because I found PIL crashes with certain inputs):
from PyQt4
import QtGui, QtCore
import qrcode
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 = QtGui.QImage(
size, size, QtGui.QImage.Format_RGB16)
self._image.fill(QtCore.Qt.white)
def pixmap(self):
return QtGui.QPixmap.fromImage(self._image)
def drawrect(self, row, col):
painter = QtGui.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)
def save(self, stream, kind = None):
pass
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.label = QtGui.QLabel(self)
self.edit = QtGui.QLineEdit(self)
self.edit.returnPressed.connect(self.handleTextEntered)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.edit)
def handleTextEntered(self):
text = unicode(self.edit.text())
self.label.setPixmap(
qrcode.make(text, image_factory = Image).pixmap())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 200, 200)
window.show()
sys.exit(app.exec_())
You can also use PNG as an intermediate format and store it in memory using StringIO.
import qrcode import StringIO def set_qr_label(label, text): "" " set qrcode image on QLabel @param label: QLabel @param text: text for the QR code "" " buf = StringIO.StringIO() img = qrcode.make(text) img.save(buf, "PNG") label.setText("") qt_pixmap = QtGui.QPixmap() qt_pixmap.loadFromData(buf.getvalue(), "PNG") label.setPixmap(qt_pixmap)
Great answer by mgmax but Python2 only. For Python3 use:
import qrcode from io import BytesIO def set_qr_label(label, text): "" " set qrcode image on QLabel @param label: QLabel @param text: text for the QR code "" " buf = BytesIO() img = qrcode.make(text) img.save(buf, "PNG") label.setText("") qt_pixmap = QtGui.QPixmap() qt_pixmap.loadFromData(buf.getvalue(), "PNG") label.setPixmap(qt_pixmap)
This is ekhumoro answer working in pyqt5 in python3 hope it helps someone down the line.
from PyQt5
import QtWidgets, QtCore, QtGui
import qrcode
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 = QtGui.QImage(
size, size, QtGui.QImage.Format_RGB16)
self._image.fill(QtCore.Qt.white)
def pixmap(self):
return QtGui.QPixmap.fromImage(self._image)
def drawrect(self, row, col):
painter = QtGui.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)
def save(self, stream, kind = None):
pass
class Window(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.label = QtWidgets.QLabel(self)
self.edit = QtWidgets.QLineEdit(self)
self.edit.returnPressed.connect(self.handleTextEntered)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.edit)
def handleTextEntered(self):
text = self.edit.text() #text = unicode(self.edit.text())
self.label.setPixmap(
qrcode.make(text, image_factory = Image).pixmap())
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 200, 200)
window.show()
sys.exit(app.exec_())
Last Updated : 30 Jun, 2022
In order to make this we will use the libraries given below
PyQt5 is cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library. Below is the command to install the PyQt5
pip install PyQt5
qrcode :For generating a Quick Response code is a two-dimensional pictographic code used for its fast readability and comparatively large storage capacity. The code consists of black modules arranged in a square pattern on a white background. The information encoded can be made up of any kind of data (e.g., binary, alphanumeric, or Kanji symbols). Below is the command to install the qrcode module
pip install qrcode
We can generate a simple QR code using the make function of qrcode and pass the data as its parameter.,Let us consider the following example that produces a QR code that reads "Welcome to Javatpoint".,The programmers can customize a QR code using a QRCode object which consists of the parameters shown in the following table:,Let us consider the following example in order to generate a QR code which points towards the Python tutorial.
Information: https: //www.javatpoint.com/python-tutorial
From the qrcode docs, it appears you can anycodings_qr-code create your own image_factory, which anycodings_qr-code might allow you to streamline the anycodings_qr-code process.,Great answer by mgmax but Python2 only. anycodings_qr-code For Python3 use:,This is ekhumoro answer working in pyqt5 anycodings_qr-code in python3 hope it helps someone down anycodings_qr-code the line.,My question: is there anything I could do to anycodings_qpixmap improve this, as it seems there are so many anycodings_qpixmap conversions involved.
I'm currently using PyQt4 and qrcode4.0.4.
from PyQt4
import QtGui, QtCore
from PIL.ImageQt
import ImageQt
import qrcode
class QRLabel(QtGui.QLabel):
def __init__(self, text = ""):
super(QRLabel, self).__init__()
self.setCode(text)
def setCode(self, text = ""):
self.text = text
qrImg = qrcode.make(text)
imgQt = ImageQt(qrImg.convert("RGB")) # keep a reference!
pixm = QtGui.QPixmap.fromImage(imgQt)
self.setPixmap(pixm.scaled(self.size(), QtCore.Qt.KeepAspectRatio))
Here's a demo that eliminates the PIL anycodings_qr-code dependency (which is just as well, anycodings_qr-code because I found PIL crashes with certain anycodings_qr-code inputs):
from PyQt4
import QtGui, QtCore
import qrcode
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 = QtGui.QImage(
size, size, QtGui.QImage.Format_RGB16)
self._image.fill(QtCore.Qt.white)
def pixmap(self):
return QtGui.QPixmap.fromImage(self._image)
def drawrect(self, row, col):
painter = QtGui.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)
def save(self, stream, kind = None):
pass
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.label = QtGui.QLabel(self)
self.edit = QtGui.QLineEdit(self)
self.edit.returnPressed.connect(self.handleTextEntered)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.edit)
def handleTextEntered(self):
text = unicode(self.edit.text())
self.label.setPixmap(
qrcode.make(text, image_factory = Image).pixmap())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 200, 200)
window.show()
sys.exit(app.exec_())
You can also use PNG as an intermediate anycodings_qr-code format and store it in memory using anycodings_qr-code StringIO.
import qrcode import StringIO def set_qr_label(label, text): "" " set qrcode image on QLabel @param label: QLabel @param text: text for the QR code "" " buf = StringIO.StringIO() img = qrcode.make(text) img.save(buf, "PNG") label.setText("") qt_pixmap = QtGui.QPixmap() qt_pixmap.loadFromData(buf.getvalue(), "PNG") label.setPixmap(qt_pixmap)
Great answer by mgmax but Python2 only. anycodings_qr-code For Python3 use:
import qrcode from io import BytesIO def set_qr_label(label, text): "" " set qrcode image on QLabel @param label: QLabel @param text: text for the QR code "" " buf = BytesIO() img = qrcode.make(text) img.save(buf, "PNG") label.setText("") qt_pixmap = QtGui.QPixmap() qt_pixmap.loadFromData(buf.getvalue(), "PNG") label.setPixmap(qt_pixmap)
This is ekhumoro answer working in pyqt5 anycodings_qr-code in python3 hope it helps someone down anycodings_qr-code the line.
from PyQt5
import QtWidgets, QtCore, QtGui
import qrcode
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 = QtGui.QImage(
size, size, QtGui.QImage.Format_RGB16)
self._image.fill(QtCore.Qt.white)
def pixmap(self):
return QtGui.QPixmap.fromImage(self._image)
def drawrect(self, row, col):
painter = QtGui.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)
def save(self, stream, kind = None):
pass
class Window(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.label = QtWidgets.QLabel(self)
self.edit = QtWidgets.QLineEdit(self)
self.edit.returnPressed.connect(self.handleTextEntered)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.edit)
def handleTextEntered(self):
text = self.edit.text() #text = unicode(self.edit.text())
self.label.setPixmap(
qrcode.make(text, image_factory = Image).pixmap())
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 200, 200)
window.show()
sys.exit(app.exec_())
Posted by vocoder on Sun, 19 Apr 2020 00:55:01 -0700
#
return Html page
class MyHttpBaseHandler(BaseHTTPRequestHandler):
def do_GET(self):
print(self.path)
enc = "UTF-8"
encoded = ''.join(self.path).encode(enc)
f = io.BytesIO()
f.write(encoded)
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html; charset=%s" % enc)
self.send_header("Content-Length", str(len(encoded)))
self.end_headers()
shutil.copyfileobj(f, self.wfile)
# This method serves the 'POST'
request type, only allowed
for CGI scripts.
def do_POST(self):
pass
#
return static - m eg: python - m http.server 8080
class MyHttpSimpleHandler(SimpleHTTPRequestHandler):
pass
httpd = HTTPServer((self.addressIP, self.port), MyHttpSimpleHandler) print("Server started on " + self.addressIP + ",port " + str(self.port) + ".....") httpd.serve_forever()
class MainWidgetUI(QDialog):
def __init__(self, parent = None):
super(MainWidgetUI, self).__init__(parent)
self.setFixedSize(640, 480) # PyQT Disable window resizing
self.setWindowTitle('Python Create a local server environment to generate QR codes')
self.setWindowIcon(QtGui.QIcon("favicon.ico"))
# Main layout
main_layout = QVBoxLayout()
self.methodtype = QComboBox()
self.methodTopLayout = QHBoxLayout()
self.methodtype.addItem('file', QVariant(1)) # Use QVariant Preservation Key
self.methodtype.addItem('address', QVariant(2))
self.methodtype.setFixedWidth(90) # Set fixed width to 90 px
self.pushButton = QPushButton("Select File")
self.Url = QLineEdit()
self.Url.hide()
self.methodTopLayout.addWidget(self.methodtype) # Add a widget
self.methodTopLayout.addWidget(self.Url) # Add a widget
self.methodTopLayout.addSpacing(10) # Add a 10 px Spatial distance without elasticity
self.methodTopLayout.addWidget(self.pushButton)
self.qrcodeGroup = QVBoxLayout()
self.groupBox = QGroupBox("QR code")
self.QrLabel = QLabel(self.groupBox)
self.qrcodeGroup.addWidget(self.groupBox)
main_layout.addLayout(self.methodTopLayout) # Add a layout
main_layout.addLayout(self.qrcodeGroup)
self.setLayout(main_layout)
self.QrLabel.setGeometry(QRect(30, 30, 540, 380)) # Set up qrLabel Graphic Position
# self.QrLabel.setScaledContents(True) # Scale 2 D code display
self.pushButton.clicked.connect(self.FileOperator) # Click the button
self.methodtype.currentIndexChanged.connect(self.comboxchange) # Events when drop - down box changes
self.Url.textChanged.connect(self.comboxchange) # Triggered when the contents of the address text box change
# Get the server(address) to generate the QR code
def ShowQrCode(self, strings):
if not strings: # The parameter is empty, pixmap Is empty
self.QrLabel.setPixmap(QtGui.QPixmap(""))
else:
qr = qrcode.QRCode(version = None, error_correction = qrcode.constants.ERROR_CORRECT_L, box_size = 10, border = 2, )
qr.add_data(strings)
qr.make(fit = True)
img = qr.make_image()
qraddr = tempDir + 'qr.png'
print(qraddr)
img.save(qraddr)
addressIP = socket.gethostbyname_ex(socket.gethostname())[-1][0] # Get LAN IP