How to Generate QR Codes in Python

ASHISH GUPTA

ASHISH GUPTA

4 min read

  • python
BannerQrcodeGeneratorwithPython

Introduction

Hey, are you looking to make your own qrcode and you don't want to share your data with third party apps or website. your data is very important for companies for showing advertisement but i have a solution for you make your own qrcode generator and make your data safe.

QR codes are very useful in these days,easy way to gathering data of event tickets and business cards to product packaging and websites. They are a simplest way to encode and share information in qrcode. In this blog post, we'll learn the process of generating QR codes using Python.

Generating QR codes with Python is candid, special thanks to libraries like qrcode and Pillow (for image processing).

simpleqr

Prerequisites

Before we write code, let's make sure you have these things:

  1. Python installed on your system.

  2. The qrcode library.

  3. The Pillow library (for handling images).

You can install the required libraries using pip:

pip install qrcode[pil]

Run the code above in the terminal from any folder location you prefer.

Error
ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: 'C:\\Python311\\Scripts\\prichunkpng'
Consider using the `--user` option or check the permissions.

if you get the above run this command in terminal. open command prompt and do Run as administrator for globalization

pip install --user qrcode[pil]

Step by step process :

these process is help you to make your qrcode you can encode your message as well as personal links.

Step 1: Import the Libraries

Initially , import the required libraries in your Python script in vscode or you can do in python idle also don't run this code in terminal:

import qrcode
from PIL import Image

Step 2: Generate a QR Code

Next, generate a QR code by specifying the data you want to encode. For example, let's encode a URL:

# Data to be encoded
data = "https://www.biyondbytes.com" #we took as example here you can write message or other links

# Create a QR Code object
qr = qrcode.QRCode(
    version=1,  # controls the size of the QR Code
    error_correction=qrcode.constants.ERROR_CORRECT_L,  # controls the error correction used for the QR Code
    box_size=10,  # controls how many pixels each "box" of the QR code is
    border=4,  # controls how many boxes thick the border should be
)

# Add data to the QR Code
qr.add_data(data)
qr.make(fit=True)

Step 3: modify and Save the QR Code

Now, Modification in your QR code like appearance and save this image in whatever image type like .jpeg, .jpg or in .png. Save this image in your destination folder or working folder, for modification it's totally based on your desire.


img = qr.make_image(fill_color="yellow", back_color="red")

# Save the image file in your working folder
img.save("qrcode.png")

#for specific file location
img.save("C:/path/to/your/folder/qrcode.png") # enter your destination folder like in desktop location.

Step 4: Present the QR Code (not necessary)

If you want to display the QR code directly in your Python file means after generating automatically open the qrcode on your screen, you can use the show() method from the Pillow library:

img.show()

Run the code and you are good to go generate your qrcode.

Complete Code

import qrcode
from PIL import Image
data = "https://www.biyondbytes.com"


qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)

# Add message or link to the QR Code
qr.add_data(data)
qr.make(fit=True)

# Create an image from the QR Code choose whatever your color you want
img = qr.make_image(fill_color="yellow", back_color="pink")

# Save the image file
img.save("qrcode.png")

# Display the QR code
img.show()

Conclusion

Generating QR codes in Python is a simple because of qrcode library thanks and give applause to it. even if you're generating a QR code for a website, a visiting card, or any other type of data, this guide should help you get started. Try with different configurations and customization options to create QR codes that fit your needs.this is not the end of qr generation using python we can generate designer qrcode like someone image in qrcode or in different shapes here are some examples.

designerqrcode

these are some branding and designed qrcode using python. explore the qrcode.pypi documentation and design your amazing qr. Hit the like button and if you want to learn how to generate designer qr comment "yes".

Happy coding!

ASHISH GUPTA

ASHISH GUPTA

Creative, Elegant and Visionary

Latest

from the blog

The latest industry news,interviews and resources

Python's One-Line if else Magic

Python's One-Line if else Magic

Python's One-Line if else Magic

Best Alternatives to Adobe's Generative Fill AI Free

Best Alternatives to Adobe's Generative Fill AI Free

Fill for free online which enhance and boost your design works

Join our newsletter

we'll send you a nice letter once per week. No spam.

BiyondBytes

© 2024 BiyondBytes. All rights reserved.