Python-based QR code generation Software

Source: Internet
Author: User

Python-based QR code generation Software

A few days ago, I figured out how to create a QR Code Applet and display it on the screen using wxPython. Of course, I want to implement it in Python only. After a while, I found three candidates:

Python-qrcode on github
Pyqrcode on sourceforge
Pyqrnative on Goolge code

I tried python-qrcode and pyqrnative because they can run on Windows/Mac/Linux. You do not need to rely on additional libraries except the Python Image Library. The pyqrcode project requires other prerequisites and cannot run on Windows, so I don't want to get involved. I finally used some previously written code of a Photo Viewer program, and then slightly modified it to become the QRCode Viewer.

Start

As I mentioned above, you only need the Python image library. We will use wxPython In the GUI section. Python-qrcode generates images faster than pyqrnative, and contains most of the QR code types you have seen.

Generate QR Codes

After you have prepared all the required items, you can run the following code to see what Python has done:

import osimport wx try:  import qrcodeexcept ImportError:  qrcode = None try:  import PyQRNativeexcept ImportError:  PyQRNative = None ########################################################################class QRPanel(wx.Panel):  """"""   #----------------------------------------------------------------------  def __init__(self, parent):    """Constructor"""    wx.Panel.__init__(self, parent=parent)    self.photo_max_size = 240    sp = wx.StandardPaths.Get()    self.defaultLocation = sp.GetDocumentsDir()     img = wx.EmptyImage(240,240)    self.imageCtrl = wx.StaticBitmap(self, wx.ID_ANY,                     wx.BitmapFromImage(img))     qrDataLbl = wx.StaticText(self, label="Text to turn into QR Code:")    self.qrDataTxt = wx.TextCtrl(self, value="http://www.mousevspython.com", size=(200,-1))    instructions = "Name QR image file"    instructLbl = wx.StaticText(self, label=instructions)    self.qrPhotoTxt = wx.TextCtrl(self, size=(200,-1))    browseBtn = wx.Button(self, label='Change Save Location')    browseBtn.Bind(wx.EVT_BUTTON, self.onBrowse)    defLbl = "Default save location: " + self.defaultLocation    self.defaultLocationLbl = wx.StaticText(self, label=defLbl)     qrcodeBtn = wx.Button(self, label="Create QR with qrcode")    qrcodeBtn.Bind(wx.EVT_BUTTON, self.onUseQrcode)    pyQRNativeBtn = wx.Button(self, label="Create QR with PyQRNative")    pyQRNativeBtn.Bind(wx.EVT_BUTTON, self.onUsePyQR)     # Create sizer    self.mainSizer = wx.BoxSizer(wx.VERTICAL)    qrDataSizer = wx.BoxSizer(wx.HORIZONTAL)    locationSizer = wx.BoxSizer(wx.HORIZONTAL)    qrBtnSizer = wx.BoxSizer(wx.VERTICAL)     qrDataSizer.Add(qrDataLbl, 0, wx.ALL, 5)    qrDataSizer.Add(self.qrDataTxt, 1, wx.ALL|wx.EXPAND, 5)    self.mainSizer.Add(wx.StaticLine(self, wx.ID_ANY),              0, wx.ALL|wx.EXPAND, 5)    self.mainSizer.Add(qrDataSizer, 0, wx.EXPAND)    self.mainSizer.Add(self.imageCtrl, 0, wx.ALL, 5)    locationSizer.Add(instructLbl, 0, wx.ALL, 5)    locationSizer.Add(self.qrPhotoTxt, 0, wx.ALL, 5)    locationSizer.Add(browseBtn, 0, wx.ALL, 5)    self.mainSizer.Add(locationSizer, 0, wx.ALL, 5)    self.mainSizer.Add(self.defaultLocationLbl, 0, wx.ALL, 5)     qrBtnSizer.Add(qrcodeBtn, 0, wx.ALL, 5)    qrBtnSizer.Add(pyQRNativeBtn, 0, wx.ALL, 5)    self.mainSizer.Add(qrBtnSizer, 0, wx.ALL|wx.CENTER, 10)     self.SetSizer(self.mainSizer)    self.Layout()   #----------------------------------------------------------------------  def onBrowse(self, event):    """"""    dlg = wx.DirDialog(self, "Choose a directory:",              style=wx.DD_DEFAULT_STYLE)    if dlg.ShowModal() == wx.ID_OK:      path = dlg.GetPath()      self.defaultLocation = path      self.defaultLocationLbl.SetLabel("Save location: %s" % path)    dlg.Destroy()   #----------------------------------------------------------------------  def onUseQrcode(self, event):    """    https://github.com/lincolnloop/python-qrcode    """    qr = qrcode.QRCode(version=1, box_size=10, border=4)    qr.add_data(self.qrDataTxt.GetValue())    qr.make(fit=True)    x = qr.make_image()     qr_file = os.path.join(self.defaultLocation, self.qrPhotoTxt.GetValue() + ".jpg")    img_file = open(qr_file, 'wb')    x.save(img_file, 'JPEG')    img_file.close()    self.showQRCode(qr_file)   #----------------------------------------------------------------------  def onUsePyQR(self, event):    """http://code.google.com/p/pyqrnative/    """    qr = PyQRNative.QRCode(20, PyQRNative.QRErrorCorrectLevel.L)    qr.addData(self.qrDataTxt.GetValue())    qr.make()    im = qr.makeImage()     qr_file = os.path.join(self.defaultLocation, self.qrPhotoTxt.GetValue() + ".jpg")    img_file = open(qr_file, 'wb')    im.save(img_file, 'JPEG')    img_file.close()    self.showQRCode(qr_file)   #----------------------------------------------------------------------  def showQRCode(self, filepath):    """"""    img = wx.Image(filepath, wx.BITMAP_TYPE_ANY)    # scale the image, preserving the aspect ratio    W = img.GetWidth()    H = img.GetHeight()    if W > H:      NewW = self.photo_max_size      NewH = self.photo_max_size * H / W    else:      NewH = self.photo_max_size      NewW = self.photo_max_size * W / H    img = img.Scale(NewW,NewH)     self.imageCtrl.SetBitmap(wx.BitmapFromImage(img))    self.Refresh()  ########################################################################class QRFrame(wx.Frame):  """"""   #----------------------------------------------------------------------  def __init__(self):    """Constructor"""    wx.Frame.__init__(self, None, title="QR Code Viewer", size=(550,500))    panel = QRPanel(self) if __name__ == "__main__":  app = wx.App(False)  frame = QRFrame()  frame.Show()  app.MainLoop()

Python-qrcode generation:

PyQRNative generation:


How to Use python to parse the QR code?

Google has such a service using third-party APIs.
 
How can I handle the verification code for python Renren's Automatic Logon? Otherwise, automatic logon cannot be implemented.

What should I do if I encounter a verification code? There are two solutions:
-
1. google's verification code, cool
-
2. simple verification code: the number of characters is limited. It only uses simple translation or rotation to add noise without being distorted. This kind of verification code can still be processed. The general idea is to rotate it back, noise Removal,
Divide a single character, divide it, and then use the feature extraction method (such as PCA) to reduce the dimension and generate a feature library, and then compare the verification code with the feature library. This is complicated. I can't finish a blog post.
.
-
3. In fact, some verification codes are still very weak. I won't name them here. Anyway, I have extracted very accurate verification codes through the 2 method, SO 2 is actually feasible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.