import qrcode
# Data to be encoded
custom_data = "This is a custom QR code made with Python!"
# Instantiate the QRCode class with custom parameters
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H, # High error correction
box_size=15,
border=5,
)
# Add the data to the QR code instance
qr.add_data(custom_data)
qr.make(fit=True)
# Create the image with custom colors
# fill_color is the color of the QR code blocks
# back_color is the background color
img_custom = qr.make_image(fill_color="darkblue", back_color="white")
# Save the custom image
img_custom.save("custom_python_qr.png")
print("Customized QR code generated successfully and saved as 'custom_python_qr.png'")
In this example:
• We create a
QRCode object with specific settings for version, error_correction, box_size, and border.• We use
qr.add_data() to add our content.•
qr.make(fit=True) finalizes the QR code structure.•
qr.make_image() generates the actual image object, where we can specify the colors.• Finally, we save the resulting image.
---
Conclusion
You now have the tools to generate QR codes effortlessly using Python. You've learned how to create a quick, standard QR code with
qrcode.make() and how to build a fully customized one using the QRCode class to control everything from size and durability to color.#### What's Next?
• Integrate into Applications: Add QR code generation to a web application (e.g., a Flask or Django app) to create user-specific codes.
• Generate Different Content: Encode Wi-Fi network details, contact information (vCards), or calendar events.
• Dynamic Generation: Build a script that takes user input to generate QR codes on the fly.
━━━━━━━━━━━━━━━
By: @DataScience4 ✨