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

How I created a QR Code Generator in Python

Patricia Arquette
Release: 2024-10-19 10:40:02
Original
824 people have browsed it

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
Copy after login

The next step would be to activate the virtual machine

source venv/bin/activate
Copy after login

To deactivate a virtual environment you will need to type:

deactivate
Copy after login

Next step would be to install the qrcode package

pip install qrcode
Copy after login

In your Python file make sure you import the qrcode module

import qrcode
Copy after login

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()
Copy after login

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}')
Copy after login

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!

source:dev.to
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template