Introduction
QR codes are everywhere—from marketing materials to product packaging. They offer a quick and easy way to share information. In this tutorial, we’ll use the Segno library to create stunning QR codes in Python. Segno is known for its simplicity and high-quality output.
Why Choose Segno?
Segno is a modern library for creating QR codes that stands out for several reasons:
- High-Quality Output: Segno generates high-quality QR codes that are visually appealing and highly readable.
- Simplicity: It has a straightforward API that makes it easy to use, even for beginners.
- Customizability: Segno allows extensive customization, including changing colors and adding logos.
- No Dependencies: Unlike some other libraries, Segno does not require external libraries for image processing, making it lightweight and easy to install.
- Error Correction: It supports different levels of error correction, ensuring the QR codes are robust against damage.
Step 1: Install the Segno Library
First, you’ll need to install Segno. Open your terminal and run:
pip install segno
Step 2: Basic QR Code Generation
Create a Python file named qr_generator.py
and add the following code to generate a basic QR code:
import segno
def generate_qr_code(data, file_name):
# Create a QR code object
qr = segno.make(data)
# Save the QR code as an image file
qr.save(file_name, scale=10)
print(f"QR code saved as {file_name}")
# Example usage
if __name__ == "__main__":
data = input("Enter the data to encode in the QR code: ")
file_name = input("Enter the file name to save the QR code (e.g., qr_code.png): ")
generate_qr_code(data, file_name)
Explanation:
- segno.make(data): Creates a QR code object with the given data.
- qr.save(file_name, scale=10): Saves the QR code image with a scale of 10 for better quality.
Step 3: Customizing the QR Code
Segno allows you to customize the appearance of your QR codes. Here’s how you can modify the code to change the color and add a logo
import segno
def generate_custom_qr_code(data, file_name):
qr = segno.make(data)
# Customize the QR code
qr = qr.to_image(fill='blue', back_color='white')
# Optionally, add a logo
# logo = Image.open('logo.png')
# qr = segno.ImageWithLogo(qr, logo, size=50)
qr.save(file_name, scale=10)
print(f"Custom QR code saved as {file_name}")
# Example usage
if __name__ == "__main__":
data = input("Enter the data to encode in the QR code: ")
file_name = input("Enter the file name to save the QR code (e.g., custom_qr_code.png): ")
generate_custom_qr_code(data, file_name)
Explanation:
- qr.to_image(fill=’blue’, back_color=’white’): Changes the QR code color to blue with a white background.
- ImageWithLogo: (Commented out) This shows how you might add a logo if you choose to include it.
Step 4: Advanced Features
Segno also supports advanced features like adding error correction levels and setting the QR code size. Here’s an example:
import segno
def generate_advanced_qr_code(data, file_name):
qr = segno.make(data, error='H', version=1)
# Customize the QR code
qr = qr.to_image(fill='green', back_color='white')
qr.save(file_name, scale=10)
print(f"Advanced QR code saved as {file_name}")
# Example usage
if __name__ == "__main__":
data = input("Enter the data to encode in the QR code: ")
file_name = input("Enter the file name to save the QR code (e.g., advanced_qr_code.png): ")
generate_advanced_qr_code(data, file_name)
Explanation:
- error=’H’: Sets the error correction level to high.
- version=1: Defines the version of the QR code.
Conclusion
Congratulations! You’ve learned how to create stunning QR codes using the Segno library in Python. With Segno’s high-quality output and customization options, you can generate QR codes that not only function perfectly but also look great. Whether for personal projects or professional applications, Segno provides an excellent tool for all your QR code needs.