Make Your Own QR Code Easily with Python🐍

Just a few steps

Β·

3 min read

Hello everyone πŸ‘‹ I'm back with another blog for you all which will help you to generate your QR code in just a few simple steps by using Python.

So, without further ado let's get started.


Prerequisites

Before deep diving into the code, you should be aware of the prerequisites needed.

  1. Python (installed on your system)

  2. Code Editor such as Visual Studio Code

  3. You should know the basics of Python.


Problem Description

Here we'll be taking a link(URL) as input from the user and then a QR code will be generated for the input URL. Now you may use this QR code image to share with your friends and it will be redirected to the input URL for which the image was generated whenever someone scans the code.


My Approach

I'll use a Python module called "qrcode" to solve this problem. First, create a folder in your system and then open it up in your favorite code editor.

Create a new file called app.py and open it up. Now open your terminal inside the folder and type the following command to install the module.

pip install qrcode

Wait for some time till it gets installed on your system.

After it gets installed, import it into your python file. At this point, I'd suggest checking out the official documentation of the qrcode module.


Complete Code

# install qrcode module
# create a function that collects the text and converts it into qr code

import qrcode


def generate_qrcode(text):
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10, # giving box size
        border=5, # giving border size
    )

    qr.add_data(text)
    qr.make(fit=True)

    # giving colors to the QR image
    img = qr.make_image(fill_color='black', back_color='white') 

    # saving the QR code in a image file in the current folder
    img.save("qr-code-image.png")


# taking url as input from the user
url = input("Enter your url: ")

# calling the generate_qrcode() function to generate the output.
generate_qrcode(url)

You may copy this code or type it out in your Python file.


Output

To check the output, open up your terminal inside the current folder and type the following command.

python app.py

If the above command gives an error for you, then you may try out the following:

python3 app.py

Then it will ask you for a valid URL.

terminal output

Here I've used my portfolio link and it has already generated a QR code for me. Let's check it out.

My Portfolio QR

Here's my portfolio link:- https://susmita-dey.vercel.app/

You may now test the QR code generated by Python. I've already tested and it works fine.


Conclusion

I hope that you have learned something new via this blog. It's quite simple, easy and super fun. If you have followed this blog, then I'm pretty sure that you have already generated one for yourself.

Now it's time to flex your QR code on your socials. 😜 Feel free to share your feedback and queries in the comments.

Thank you for reading, and let's connect!!!

Happy Hacking 🌻

Did you find this article valuable?

Support Susmita Dey by becoming a sponsor. Any amount is appreciated!

Β