QR codes have become an easy and efficient way to share information. Whether it’s a website link, contact details, or a product, QR codes are widely used because they’re simple and fast to scan. In this guide, you’ll learn how to create your own QR code using Python in just a few steps!
Let’s dive in!
Step 1: Install Requirements
Before we start coding, you’ll need to install a Python library called qrcode
. You’ll also need the Pillow
library to handle image processing. Here’s how to install them:
- Open your terminal or command prompt.
- Type the following command and hit enter:
pip install qrcode[pil]
That’s it! You’re now ready to create your QR code.
Step 2: Write the Python Script
Now that we have the necessary libraries installed, let’s write a Python script that generates a QR code.
import qrcode
from PIL import Image
def generate_qr(url, fg_color="black", bg_color="white"):
"""
Generate a QR code with the given URL and colors.
Parameters:
url (str): The URL or data to encode in the QR code.
fg_color (str): Foreground color of the QR code (default is black).
bg_color (str): Background color of the QR code (default is white).
Returns:
None
"""
# Create a QR code object with basic settings
qr = qrcode.QRCode(
version=1, # controls the size of the QR Code
error_correction=qrcode.constants.ERROR_CORRECT_L, # error tolerance
box_size=10, # size of each box in the QR code grid
border=4, # thickness of the border
)
# Add the URL/data to the QR code
qr.add_data(url)
qr.make(fit=True)
# Create an image from the QR code with the specified colors
img = qr.make_image(fill_color=fg_color, back_color=bg_color).convert('RGB')
# Save the image
img.save("custom_qr_code.png")
print(f"QR Code generated and saved as 'custom_qr_code.png'")
# Example usage
if __name__ == "__main__":
# URL and colors (pass your own or use defaults)
url_to_encode = "https://allinpython.com"
foreground_color = "purple" # Change if needed
background_color = "white" # Change if needed
# Generate the QR code
generate_qr(url_to_encode, fg_color=foreground_color,bg_color=background_color)
Output:
Code Explanation
In this script, you can pass three things:
- URL: The website or data you want to encode.
- Foreground color: The color of the QR code itself. By default, it’s black.
- Background color: The color behind the QR code. By default, it’s white.
If you don’t specify any colors, the default colors (black and white) will be used.
How It Works
- Creating the QR Code Object: We use the
QRCode
class to create a new QR code. The parameters control its size, error correction (how much damage it can handle), and the size of its grid boxes. - Adding Data: The
add_data()
function adds your URL or any other text to the QR code. - Generating the Image: The
make_image()
function creates the QR code image with the specified foreground and background colors. You can change these to make your QR code more visually appealing! - Saving the Image: Finally, the generated QR code is saved as an image file (
custom_qr_code.png
), which you can open and use.
Conclusion
And that’s it! You’ve successfully created a QR code generator using Python. You can easily change the colors and the data it encodes to fit your needs.
Whether you’re generating a QR code for your website or a personal project, this simple tool gives you all the flexibility you need. Thank you for reading this article, and I hope you learned something new from it. For more Python projects, click here, and I’ll see you in the next one.