create qr code in python

To create a QR code in Python, you can use the qrcode library.

First, you need to install the library using pip. Open your terminal or command prompt and enter the following command:

main.py
pip install qrcode[pil]
24 chars
2 lines

This command installs the qrcode library along with the Pillow library, which is required for generating the QR code image.

Once you've installed the library, you can use the following code to generate a QR code:

main.py
import qrcode
from PIL import Image

# Create a QR code instance
qr = qrcode.QRCode(version=1, box_size=10, border=5)

# Add data to the QR code
data = "https://www.example.com"
qr.add_data(data)
qr.make(fit=True)

# Generate an image from the QR code instance
img = qr.make_image(fill_color="black", back_color="white")

# Save the image
img.save("example_qr_code.png")
371 chars
17 lines

In this code, we first imported the qrcode library and the Image module from the Pillow library. We then created a QRCode instance with a version of 1, a box size of 10, and a border of 5. We added data to the QR code by calling the add_data() method and passing in the URL we want to encode. We then called the make() method with the fit parameter set to True to generate the QR code. Finally, we called the make_image() method to generate an image from the QR code instance with black fill color and white background color. We saved the image to a PNG file named "example_qr_code.png".

You can modify the version, box size, border, data, and other parameters as per your requirements.

gistlibby LogSnag