Software Testing | How to use Python to generate QR codes

Insert image description here

Introduction

QR Code is a matrix two-dimensional barcode that can be quickly scanned and recognized. It is now widely used in payment, product traceability, event promotion and other scenarios in physical stores. As a powerful and easy-to-learn programming language, Python provides a variety of libraries and tools for generating QR codes, allowing developers to easily generate customized QR codes. In this article, we will use the Python programming language and a library called qrcode to generate QR codes. This library makes generating QR codes very easy.

Environmental preparation

After we install the Python environment, we also need to install qrcodethe library. The installation command is as follows:

pip install qrcode

After the installation is complete, we can start using it to generate QR codes.

Generate a simple QR code

Here is a Python example that generates a simple text QR code:

import qrcode

# 创建一个QRCode对象
qr = qrcode.QRCode(
    version=1,  # 版本号,从1到40,表示二维码的大小
    error_correction=qrcode.constants.ERROR_CORRECT_L,  # 误差纠正水平:L(低)、M(中)、Q(高)、H(最高)
    box_size=10,  # 每个模块的像素大小
    border=4,  # 二维码的边距大小
)

# 设置要编码的数据
data = "Hello, World!"

# 添加数据到QRCode对象
qr.add_data(data)

# 创建QRCode图像
qr.make(fit=True)

# 创建Image对象
img = qr.make_image(fill_color="black", back_color="white")

# 保存图像
img.save("hello_world.png")

![Insert image description here](https://img-blog.csdnimg.cn/dd42091d94e949d899af6dc50f6cb919.png

In the above example, we first imported qrcodethe library, then created an QRCodeobject and set parameters such as version, error correction level, module size, and margins. We then add the data to be encoded to QRCodethe object and use the make method to generate QRCodethe image. Finally, we make_imagecreate an image object using the method, set the foreground and background colors, and save the image to a file.

After running the above code, we will find a file named hello_world.png in the current working directory, which contains the generated QR code image. The resulting image looks like this:

Insert image description here

Generate QR code containing URL

Generating a QR code containing a URL is also very simple. Here is an example:

import qrcode

# 设置要生成的网址
url = "https://www.baidu.com"

# 创建QRCode对象
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)

# 添加网址数据到QRCode对象
qr.add_data(url)

# 创建QRCode图像
qr.make(fit=True)

# 创建Image对象
img = qr.make_image(fill_color="black", back_color="white")

# 保存图像
img.save("website_qrcode.png")

In this example, we only need to set the URL to be generated as the url variable. In this article, we take the Baidu URL as an example, and then add it to the QRCode object. The remaining steps are the same as in the previous example.

After running the above code, we will find a file named in the current working directory website_qrcode.png, which contains the generated QR code image containing the URL. We can directly open Baidu's webpage by scanning the QR code with our mobile phone. The image is as follows:

Insert image description here

Generate QR code with logo

If we want to add a custom logo icon to the QR code, we can use it in combination qrcodewith pillowthe library. Here is our sample code:

import qrcode
from PIL import Image

# 创建一个QRCode对象
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)

# 设置要编码的数据
data = "Hello, World!"

# 添加数据到QRCode对象
qr.add_data(data)

# 创建QRCode图像
qr.make(fit=True)

# 创建Image对象
img = qr.make_image(fill_color="black", back_color="white")

# 打开Logo图像
logo = Image.open("logo.jpg")  # 替换为你的Logo文件路径

# 计算Logo的大小
img_width, img_height = img.size
logo = logo.resize((img_width // 4, img_height // 4))  # 调整Logo大小,可以根据需要调整比例

# 计算Logo的位置
logo_x = (img_width - logo.width) // 2
logo_y = (img_height - logo.height) // 2

# 将Logo添加到QRCode图像中
img.paste(logo, (logo_x, logo_y))

# 保存带Logo的二维码图像
img.save("hello_world_with_logo.png")

In this example, we first create a QRCode object and generate a QRCode image. Then, we opened the Logo image using the PIL library (Python Imaging Library). Next, we calculated the size of the logo to fit the QRCode image, and then calculated the position of the logo to make sure it was in the middle of the QRCode image. Finally, we use pastethe method to add the Logo to the QRCode image and save the result to a file.

Make sure to replace "logo.png" in the code with the actual path to your Logo file. After running the above code, we will get a QR code image containing the Logo, and the Logo will be in the middle of the QR code. As shown below:

Insert image description here

Summarize

Through this simple Python example, we can easily generate various types of QR codes to meet different needs. We can also further explore qrcodethe library's documentation for more advanced features and options. Hope this article is helpful to everyone.

Guess you like

Origin blog.csdn.net/Tester_muller/article/details/132717927