Let’s create a simple Python script that generate a QR code. We can use the python “qrcode” library for generating QR code images.
pip install qrcode
This code generates a QR code with the data “https://www.mmkernel.com” and saves it as a PNG image with the filename “qr_code.png”.
import qrcode
# Creating an instance of QRCode class
qr = qrcode.QRCode(
version=1,
box_size=10,
border=5
)
# Adding data to the instance 'qr'
data = "https://www.mmkernel.com"
qr.add_data(data)
qr.make(fit=True)
# Create and save the QR image
img = qr.make_image(fill_color="black", back_color="white")
img.save("qr_code.png")
Output:
Now let’s take a look at the code:
- The
version
argument specifies the size of the QR code: version = 1 is the smallest, version = 40 is the largest - The
box_size
argument specifies the size of each box in the QR code - The
border
argument specifies the size of the border around the QR code. - The
fill_color
andback_color
arguments specify the colors of the boxes and the background, respectively.
You can change the line number 11 with:
data = input("Change me")
This way, you can enter manually the data you want to be generated (some text, number, link, etc…)