Member-only story
Colorful QR Code using Python
2 min readOct 24, 2024
import qrcode
from PIL import Image
data = input("Enter anything to generate QR: ")
qr = qrcode.QRCode(version=2, box_size=10, border=4)
qr.add_data(data)
qr.make(fit=True)
image = qr.make_image(fill="black", back_color="cyan")
image.save("qr_code.png")
Image.open("qr_code.png")
Let’s break down the code step by step:
1. Imports
import qrcode
from PIL import Image
qrcode
: This library allows you to generate QR codes in Python. It provides tools to create QR codes from data such as text, URLs, etc.PIL (Pillow)
is an imaging library in Python, andImage
is used to handle image operations such as opening, saving, and displaying images.
2. Get User Input
data = input("Enter anything to generate QR: ")
input()
prompts the user to enter some text or data.- The value entered by the user is stored in the
data
variable. This value will be encoded into a QR code.
3. Create a QR Code Object
qr = qrcode.QRCode(version=2, box_size=10, border=4)
qrcode.QRCode()
creates an instance of the QRCode class.version=2
: Specifies the version of the QR code. Higher…