"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How I created a QR Code Generator in Python

How I created a QR Code Generator in Python

Published on 2024-11-08
Browse:813

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

The next step would be to activate the virtual machine

source venv/bin/activate

To deactivate a virtual environment you will need to type:

deactivate

Next step would be to install the qrcode package

pip install qrcode

In your Python file make sure you import the qrcode module

import qrcode

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()

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}')

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

Release Statement This article is reproduced at: https://dev.to/abeck617/how-i-created-a-qr-code-generator-in-python-5dmo?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3