Home > Backend Development > Python Tutorial > How to use Python to generate QR codes for pictures

How to use Python to generate QR codes for pictures

WBOY
Release: 2023-08-18 22:21:12
Original
2789 people have browsed it

How to use Python to generate QR codes for pictures

How to use Python to generate QR codes for pictures

QR code is an image code that can be used to store information. It is widely used in modern society use. In Python, we can use third-party libraries to generate and manipulate QR codes. This article will introduce how to use Python to generate QR codes for images and provide code examples.

First, we need to install a Python library called qrcode. You can use the pip command to install:

pip install qrcode
Copy after login

After the installation is complete, we can import the qrcode library in the Python code:

import qrcode
Copy after login

Next, we need to create a Qrcode object and use Its add_data() method sets the information to be stored in the QR code:

qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
qr.add_data("Hello, World!")
qr.make(fit=True)
Copy after login

In the above code, we create a Qrcode object and use the version, error_correction, box_size and border parameters to control the QR code Code size and border size. Next, we use the add_data() method to set the information to be stored in the QR code, which can be any string. Then, we call the make() method to generate the QR code.

Next, we need to create an Image object and use the make_image() method of the Qrcode object to generate the image data of the QR code:

img = qr.make_image(fill_color="black", back_color="white")
Copy after login

In the above code, we use the fill_color parameter To set the foreground color of the QR code, the default is black; use the back_color parameter to set the background color of the QR code, the default is white. Then, we call the make_image() method to generate the image data of the QR code.

Finally, we can save the generated QR code image data as a picture file:

img.save("qrcode.png")
Copy after login

In the above code, we use the save() method to save the QR code as a file named qrcode .png image files.

The complete code example is as follows:

import qrcode

qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
qr.add_data("Hello, World!")
qr.make(fit=True)

img = qr.make_image(fill_color="black", back_color="white")
img.save("qrcode.png")
Copy after login

After running the above code, you will see the generated qrcode.png image file in the current working directory, which is the image data of the QR code.

Through the above code examples, we can see that it is very simple to use Python to generate QR codes. You can adjust parameters such as QR code size, border size, foreground color, and background color according to your needs. At the same time, you can also apply the generated QR code image data to more scenarios, such as adding it to web pages, generating business cards, etc.

I hope the content of this article will be helpful to you, and I wish you success in using Python to generate QR codes!

The above is the detailed content of How to use Python to generate QR codes for pictures. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template