This is the 8th day of my participation in the August More Text Challenge. For details, see:August is more challenging
How to generate an elegant QR code in Python
As a tool of information transmission, TWO-DIMENSIONAL code plays an important role in today’s society. From mobile phone user login to mobile phone payment, the existence of TWO-DIMENSIONAL code can be seen in every corner of life, so how do we generate a two-dimensional code? Using Python, we can quickly generate a QR code, and we can define the information contained in the QR code. This information can be text, pictures, or websites. Let’s see how to generate a QR code.
First, use MyQR to generate two-dimensional code
There are many ways to generate a QR code. Let’s first look at how to generate a QR code using the MyQR module.
(1) Module installation
We need to install the module before we can start using it. Here use PIP direct download, here choose the domestic source:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ myqr
Copy the code
Once the installation is complete, we are ready to use it. Sir Into a simple QR code:
from MyQR import myqr # Be case sensitive
myqr.run(words='Do not go gentle into that good night! ') # Generate qr code
Copy the code
After we run this program, the py file will generate the picture qrcode. PNG in the same directory, which is our TWO-DIMENSIONAL code picture, scan out is the text information we set above.
(2) Generate an image TWO-DIMENSIONAL code
Two-dimensional code in our daily life are relatively monotonous, there are pure two-dimensional code, the whole two-dimensional code is only black and white square; There are also qr codes with pictures, usually with a picture in the center of the qr code, and what we’re going to do now is a QR code with a picture as a whole. That is, an image in the background. The QR code is also very simple to implement:
from MyQR import myqr
myqr.run(
words='http://www.baidu.com'.# Include information
picture='lbxx.jpg'.# Background Image
colorized=True.# Whether there is a color, if False, black and white
save_name='code.png' # output file name
)
Copy the code
The effect of generating two-dimensional code is as follows:As you can see, the information contained in our QR code here is a website. At this time, we scan the QR code and jump directly to the web page. As the code, we only need to set the picture parameter to a GIF, and the output file suffix to GIF:
Two, use Qrcode to generate two-dimensional code
Qrcode is also a convenient tool, using this module we can also quickly achieve the generation of TWO-DIMENSIONAL code.
(1) Module installation
Here again using PIP, we execute the following statement on the command line window:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ qrcode
Copy the code
Once the installation is complete we can start to generate our first QR code:
import qrcode
img = qrcode.make('http://www.baidu.com')
img.save('qrcode.jpg')
Copy the code
After we call the save method, the project will generate a qrcode. PNG picture, the picture is our TWO-DIMENSIONAL code picture, we scan out is also a direct jump to the page.
(2) More accurate generation of TWO-DIMENSIONAL code
In addition to the above methods, we can also use the QRCode class to generate the QRCode, in this way we can control more information about the QRCode:
from qrcode import QRCode
qr = QRCode() Create a QR code object
qr.add_data('http://www.baidu.com') Set qr code data
img = qr.make_image() Create qr code image
img.save('qrcode.png') Save the qr code picture
Copy the code
In this way, we can also generate a TWO-DIMENSIONAL code, of course, we can also enrich:
import qrcode
qr = qrcode.QRCode(
version=5.The value ranges from 1 to 40
box_size=10.# The number of pixels in the smallest square of the QR code
error_correction=qrcode.constants.ERROR_CORRECT_H, # Error correction level of QR code
border=5 # Size of the white border
)
qr.add_data('http://www.baidu.com') Set qr code data
img = qr.make_image() Create qr code image
img.save('qrcode.png') # Save qr code
Copy the code
Version contains the size information. When set to 1, a 12×12 size QR code is generated in the unit of box_size pixels. We can set version to None and add a qr. Make (fit=True) and the program will automatically generate the appropriate size qr code. In addition, error_correction is the setting of the error correction level. This is the knowledge about the QR code itself.
- ERROR_CORRECT_L: About 7% or less of the errors can be corrected.
- ERROR_CORRECT_M (default) : About 15% or less of the errors can be corrected.
- ROR_CORRECT_H: About 30% or less of errors can be corrected.
Above are a few built-in constants that we can choose from.
(3) Read the data in the TWO-DIMENSIONAL code
Above we have been talking about how to generate two-dimensional code, but we ourselves can not read the information in the two-dimensional code, this is to use our equipment. In Python, we can use the Pyzbar module to recognize qr codes, but there are other methods. Here we use Pyzbar to see how to recognize qr codes. First we need to install the module:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pyzbar
Copy the code
In addition, we need to install the OpencV module:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ opencv-python
Copy the code
Then you can start to recognize the QR code:
import cv2
from pyzbar import pyzbar
im = cv2.imread('qrcode.png') # Read qr code
data = pyzbar.decode(im) # Parse qr code
print(data)
Copy the code
We used the following picture as a test:The information contained in it ishttp://www.baidu.com
, let’s look at the output:
[Decoded(data=b'http://www.baidu.com'.type='QRCODE', rect=Rect(left=5, top=5, width=29, height=29), polygon=[Point(x=5, y=5), Point(x=5, y=34), Point(x=34, y=34), Point(x=34, y=5)])]
Copy the code
It’s obviously something we can’t read, but we see the word http://www.baidu.com in it. We can parse the content in the following way:
import cv2
from pyzbar import pyzbar
im = cv2.imread('qrcode.png') # Read qr code
data = pyzbar.decode(im) # Parse qr code
text = data[0].data.decode('utf-8') # Parse data
print(text)
Copy the code
The output is as follows:
http://www.baidu.com
Copy the code
So we’ve actually parsed it out.