SoFunction
Updated on 2024-11-21

Example of python generating QR code in detail

Example of python generating QR code in detail

Version Related

Operating System: Mac OS X EI Caption

Python version: 2.7

IDE:Sublime Text 3

dependency library (computing)

The dependent libraries needed for Python to generate QR codes are PIL and QRcode.

The pitiful thing is that Baidu did not find PIL for a long time, I do not know when the name was changed, or other reasons, pillow is the legendary PIL.

Installation commands:sudo pip install pillow、sudo pip install qrcode

Verify whether the installation is successful, use the command from PIL import Image, you can verify whether the PIL is installed successfully, qrcode use import qrcode, two commands do not report an error, it shows that the dependent environment has been completed.

Using qrcode

qrcode is also very simple to use, the code is as follows:

import qrcode


qr = (
  version=7,
  error_correction=.ERROR_CORRECT_L,
  box_size=10,
  border=4
)
qr.add_data("Hello")
(fit=True)
img = qr.make_image()
("")

The parameter version indicates the size of the generated QR code, the value range is from 1 to 40, the minimum size of 1 will generate a QR code of 21 * 21, every increase of 1 in version will add 4 dimensions to the generated QR code, for example, if the version is 2, it will generate a QR code of 25 * 25.

The parameter error_correction specifies the error tolerance coefficients of the QR code, which are the following four coefficients respectively:

1.ERROR_CORRECT_L: 7% of the character code is error tolerant
2.ERROR_CORRECT_M: 15% of the character code is error tolerant
3.ERROR_CORRECT_Q: 25% of the character code is error tolerant
4.ERROR_CORRECT_H: 30% of the character code is error tolerant

The parameter box_size represents the pixel size of each grid in the QR code.

The parameter border indicates how thick the border grid is (default is 4).

After running the code, a QR code will be generated in the current directory of the code, and you can scan it with your phone to see Hello.

QR code with logo

The principle of generating this QR code is to use the PIL library to manipulate the icon and put the icon in the center of the QR code. the PIL manipulation didn't go to understand it, so I'll just use my code to do the example directly.

import Image
import qrcode


qr = (
  version=2,
  error_correction=.ERROR_CORRECT_H,
  box_size=10,
  border=1
)
qr.add_data("/")
(fit=True)

img = qr.make_image()
img = ("RGBA")

icon = ("")

img_w, img_h = 
factor = 4
size_w = int(img_w / factor)
size_h = int(img_h / factor)

icon_w, icon_h = 
if icon_w > size_w:
  icon_w = size_w
if icon_h > size_h:
  icon_h = size_h
icon = ((icon_w, icon_h), )

w = int((img_w - icon_w) / 2)
h = int((img_h - icon_h) / 2)
(icon, (w, h), icon)

("dhqme_qrcode.png")

If you have any questions, please leave a message or go to this site community exchange and discussion, thank you for reading, I hope to help everyone, thank you for your support of this site!