Python generates two-dimensional code
QR Code is also known as QR Code, QR full Quick Response, is a super popular coding method on mobile devices in recent years, it can store more information than the traditional Bar Code, but also can represent more data types: such as: characters, numbers, Japanese, Chinese and so on.
Version specifies the size of the TWO-DIMENSIONAL code. Version 1 is the matrix 21 x 21, Version 2 is the matrix 25 x 25, and Version 3 is the size 29×29. Each additional Version will increase the size of 4. The formula is :(v-1)x4 +21 (V is the Version number) the highest Version 40, (40-1)*4+21 = 177, so the highest is 177 x 177 square.
QR codes can be quickly read from 360° in either direction. The mystery is the QR code in the 3 location pattern, can help QR code is not affected by the background style, to achieve fast and stable reading.
There are four error-correction Code levels in the QR Code, which is why the qr Code can be scanned if it has a defect, and why it can be scanned if someone adds an icon in the center of the QR Code.
Error correction capacity | |
---|---|
L levels | 7% of the codes can be corrected |
M | Fifteen percent of the code can be corrected |
Q level | Twenty-five percent of the codes can be corrected |
H level | 30% of the codes can be corrected |
How do I select version?
For example, if you need to enter 100 digits, perform the following steps to select the digits.
Qrcode for Python modules
Two-dimensional code can be written into any content, including text, website links, etc.
Qrcode module installation is simple:
pip install qrcode
Copy the code
The code below generates a QR code that scans and displays the words “Donald Trump is king!” :
#coding=gbk
import qrcode
img = qrcode.make('Donald Trump knows the king! ')
img.save("qr1.png")
Copy the code
You can also write a website link in the TWO-DIMENSIONAL code, and it will automatically jump to the website after scanning:
import qrcode
img = qrcode.make('https://blog.csdn.net/Dillon2015')
img.save("qr2.png")
Copy the code
Qrcode generates qr codes for more customization:
import qrcode
data = 'https://blog.csdn.net/Dillon2015'
img_file = r'qr3.png'
# instantiate QRCode to generate a QR object
#version: An integer ranging from 1 to 40, representing the size of the QR code (the minimum value is 1, which is a 21x21 matrix). If you want it to be generated automatically, set the value to None and use the fit=True argument.
#error_correction: Error correction range of two-dimensional code
# boxSize: Number of pixels per square
#border: the distance between the qr code and the peripheral border of the image. The default value is 4, and the minimum value is 4
qr = qrcode.QRCode(
version=1.#
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4
)
# incoming data
qr.add_data(data)
qr.make(fit=True)
# Generate qr code
#fill_color and back_color can change the foreground and background colors of the QR code
img = qr.make_image(fill_color="green", back_color="black")
# Save qr code
img.save(img_file)
Copy the code
Due to the error correction function of the TWO-DIMENSIONAL code, images can also be pasted in the middle of the two-dimensional code (similar to wechat two-dimensional code), which can be achieved through the img.paste function of PIL library.
import qrcode
from PIL import Image
data = 'https://blog.csdn.net/Dillon2015'
img_file = r'qr5.png'
# instantiate QRCode to generate a QR object
#version: An integer ranging from 1 to 40, representing the size of the QR code (the minimum value is 1, which is a 21x21 matrix). If you want it to be generated automatically, set the value to None and use the fit=True argument.
#error_correction: Error correction range of two-dimensional code
# boxSize: Number of pixels per square
#border: the distance between the qr code and the peripheral border of the image. The default value is 4, and the minimum value is 4
qr = qrcode.QRCode(
version=1.#
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4
)
# incoming data
qr.add_data(data)
qr.make(fit=True)
# Generate qr code
#fill_color and back_color can change the foreground and background colors of the QR code
img = qr.make_image(fill_color="green", back_color="black")
Get the width and height of the QR code
img_w, img_h = img.size
# Add logo, open logo image
icon = Image.open("I:\\29.jpg")
The size of the logo is 1/6 of the SIZE of the QR code
factor = 6
size_w = int(img_w / factor)
size_h = int(img_h / factor)
icon_w, icon_h = icon.size
if icon_w > size_w:
icon_w = size_w
if icon_h > size_h:
icon_h = size_h
Resize the logo
icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
# Get the x and y coordinates of the drawing, and center them
x = int((img_w - icon_w) / 2)
y = int((img_h - icon_h) / 2)
# Paste the logo photo
img.paste(icon, (y, y), mask=None)
# Save qr code
img.save(img_file)
Copy the code
Python module myQR
Myqr relies on imageio and only supports PYTHon3:
pip install myqr
Copy the code
Generate the QR code with pictures, this is the color picture code, black and white pictures need to remove the parameter colorized=True:
from MyQR import myqr
myqr.run(words='https://blog.csdn.net/Dillon2015', picture='I:\\29.jpg',colorized=True)Output file names can be generated by default
Copy the code
The image can also be set as a GIF, or you can make more customization to the QR code:
from MyQR import myqr
myqr.run(
words='https://blog.csdn.net/Dillon2015',
version=1,
level='H'.# Control error correction level
picture='2.gif'.# dynamic figure
colorized=True.# Black and White (False), Color (True)
contrast=1.0.Adjust the contrast of the image. 1.0 indicates the original image. The default is 1.0.
brightness=1.0.Use to adjust the brightness of the picture.
save_name='qr4.gif'.JPG,.png,.bmp,.gif
)
Copy the code
If you are interested, please pay attention to wechat public account Video Coding