Home  >  Article  >  Backend Development  >  How I created a QR Code Generator in Python

How I created a QR Code Generator in Python

Patricia Arquette
Patricia ArquetteOriginal
2024-10-19 10:40:02696browse

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

The above is the detailed content of How I created a QR Code Generator in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn