DEV Community

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

Posted on

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

Top comments (0)