DEV Community

Cover image for How I created a QR Code Generator in Python
Anthony Beckford🚀
Anthony Beckford🚀

Posted on

2

How I created a QR Code Generator in Python

This will be a short article of how I created a simple QR Code Generator in Python

For this step you need to use the qrcode library: https://pypi.org/project/qrcode/

One of the very first steps I did after creating my projects folder is created a virtual environment. A virtual environment in Python is just another separated workspace on your computer where you can install your packages to run Python Projects.

Since I'm on Mac the command is

python3 -m venv venv
Enter fullscreen mode Exit fullscreen mode

The next step would be to activate the virtual machine

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

To deactivate a virtual environment you will need to type:

deactivate
Enter fullscreen mode Exit fullscreen mode

Next step would be to install the qrcode package

pip install qrcode
Enter fullscreen mode Exit fullscreen mode

In your Python file make sure you import the qrcode module

import qrcode
Enter fullscreen mode Exit fullscreen mode

In my code I have created two inputs which I store it into a variable called data and filename. The Strip() method Remove spaces at the beginning and at the end of the string:

data = input('Enter a text or URL ').strip()
filename = input('Enter the filename ').strip()
Enter fullscreen mode Exit fullscreen mode

Next here we go into the QR module and create the QR Code object

qr = qrcode.QRCode(box_size=10, border=4)

qr.add.data(data)

image = qr.make_image(fill_color = 'black', back_color = 'white')

image.save(filename)

print(f'QR Code saved as {filename}')
Enter fullscreen mode Exit fullscreen mode

This code you can run on the terminal and it will create a QR code with any URL you choose

Stay tune for more articles!
Look to follow me on Twitter(X) @abeck617

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (0)

Jetbrains image

Is Your CI/CD Server a Prime Target for Attack?

57% of organizations have suffered from a security incident related to DevOps toolchain exposures. It makes sense—CI/CD servers have access to source code, a highly valuable asset. Is yours secure? Check out nine practical tips to protect your CI/CD.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay