1. Install the Python qrcode library
First, we need to installqrcode
Library. Can be passedpip
Install the library:
pip install qrcode[pil]
Herepil
It is a dependency of Python Imaging Library (PIL).qrcode
The library relies on it to generate image files for QR codes.
2. Basic usage method
1. Generate a simple QR code
The most basic method of generating QR codes is to use()
Class to create a QR code object, and then callmake()
andmake_image()
Method to generate QR code and save it as an image file.
import qrcode # Create QRCode objectqr = ( version=1, # Control the size of the QR code. The larger the value, the more complex the QR code error_correction=.ERROR_CORRECT_L, # Error correction level box_size=10, # Control the pixel size of each small module in the QR code border=4, # Control the border size of the QR code) # Add data to the QR codedata = "" qr.add_data(data) (fit=True) # Generate QR code imageimg = qr.make_image(fill="black", back_color="white") # Save QR code image("simple_qrcode.png")
2. Generate a QR code with a logo
If you want to generate a QR code with a corporate logo or personal logo, you can do it by modifying the QR code image. Generally speaking, we will place a logo image in the center of the QR code, and the specific code is as follows:
from PIL import Image # Generate QR codeqr = ( version=1, error_correction=.ERROR_CORRECT_H, # Set a higher bug correction level box_size=10, border=4, ) qr.add_data(data) (fit=True) img = qr.make_image(fill="black", back_color="white") # Add Logologo = ("") # Load the logo imagelogo = ("RGBA") # Make sure the logo is in RGBA formatlogo = ((50, 50)) # Resize the logo # Place the logo in the center of the QR codeqr_width, qr_height = logo_width, logo_height = (logo, ((qr_width - logo_width) // 2, (qr_height - logo_height) // 2), logo) # Save the QR code with logo("qr_with_logo.png")
3. Set the error correction level of QR code
The QR code can set different error correction levels. The higher the error correction level, the QR code can still be scanned when it is damaged.qrcode
The library provides the following common bug fix levels:
-
ERROR_CORRECT_L
: Approximately 7% of data loss allowed -
ERROR_CORRECT_M
: Approximately 15% of data loss allowed -
ERROR_CORRECT_Q
: Approximately 25% of data loss allowed -
ERROR_CORRECT_H
: Approximately 30% of data loss allowed
You can adjust the bug correction level according to actual needs. For example, set the error correction level toH
:
qr = ( version=1, error_correction=.ERROR_CORRECT_H, box_size=10, border=4, ) qr.add_data(data) (fit=True) img = qr.make_image(fill="black", back_color="white") ("high_error_correction_qrcode.png")
3. Advanced applications
1. Customize the QR code color
In addition to the black and white QR code, we can also customize the color of the QR code and change the foreground color and background color. For example, we can generate a QR code with blue foreground color and yellow background color:
img = qr.make_image(fill="blue", back_color="yellow") ("custom_color_qrcode.png")
2. Dynamically generate QR codes
If you want to generate QR codes in real time, you can combine them with web frameworks such as Flask or FastAPI to generate and return QR codes dynamically. The following is usedFastAPI
andqrcode
Examples of library combination:
from fastapi import FastAPI from import StreamingResponse import io import qrcode app = FastAPI() @("/generate_qr/") def generate_qr(data: str): # Generate QR code qr = ( version=1, error_correction=.ERROR_CORRECT_L, box_size=10, border=4, ) qr.add_data(data) (fit=True) img = qr.make_image(fill="black", back_color="white") # Save the image to memory and return img_byte_arr = () (img_byte_arr, format="PNG") img_byte_arr.seek(0) return StreamingResponse(img_byte_arr, media_type="image/png")
When you visit/generate_qr/?data=your_data_here
When the server returns a QR code image containing the incoming data.
3. Bulk QR code generation
If you need to generate QR codes in batches, you can usefor
Loop to batch generate multiple QR codes and save them as different image files. For example, generate QR codes corresponding to multiple URLs:
urls = ["https://", "https://", "https://"] for i, url in enumerate(urls): qr = ( version=1, error_correction=.ERROR_CORRECT_L, box_size=10, border=4, ) qr.add_data(url) (fit=True) img = qr.make_image(fill="black", back_color="white") (f"qrcode_{i + 1}.png")
4. Frequently Asked Questions and Solutions
1. The QR code cannot be scanned after it is generated
If the generated QR code cannot be scanned, it may be due to the following reasons:
- The bug fix level is too low: If the error correction level of the QR code is too low, you can try to increase the error correction level.
-
The QR code is too small: The size of the QR code (
box_size
) is too small and the scanner may not be able to correctly recognize it. You can increase it appropriatelybox_size
。 - Data is too long: When the data volume of QR code is too large, the generated QR code may be too complicated, which makes scanning difficult. It is possible to consider compressing or dividing the data into multiple QR codes.
2. How to ensure the quality of QR codes
The quality of the QR code is not only related to the size of the picture, but also closely related to factors such as encoding method and error correction level. By reasonably setting the parameters of the QR code (such asversion
、error_correction
etc.), we can ensure that the QR code has a higher recognition rate.
5. Summary
Python qrcode
The library provides us with a simple and powerful QR code generation tool, which can help us easily generate QR codes of different styles and customize them according to our needs. In this article, we explain in detail how to install and use it.qrcode
Library and combine actual examples to introduce various advanced applications generated by QR codes. Through reasonable useqrcode
Library, we can integrate the QR code generation process into actual projects, thereby improving work efficiency and bringing more possibilities for innovation.
This is the article about the operation guide for using the qrcode library to generate QR codes in Python. For more related content on Python qrcode to generate QR codes, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!