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.py24 chars2 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.py371 chars17 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