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
python3 -m venv venv
python3 -m venv venv

Enter fullscreen mode Exit fullscreen mode

The next step would be to activate the virtual machine

source venv/bin/activate
source venv/bin/activate
source venv/bin/activate

Enter fullscreen mode Exit fullscreen mode

To deactivate a virtual environment you will need to type:

deactivate
deactivate
deactivate

Enter fullscreen mode Exit fullscreen mode

Next step would be to install the qrcode package

pip install qrcode
pip install qrcode
pip install qrcode

Enter fullscreen mode Exit fullscreen mode

In your Python file make sure you import the qrcode module

import qrcode
import qrcode
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()
data = input('Enter a text or URL ').strip()
filename = input('Enter the filename ').strip()
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}')
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}')
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

原文链接:How I created a QR Code Generator in Python

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
The future you will certainly thank yourself now desperately.
未来的你一定会感谢现在拼命的自己
评论 抢沙发

请登录后发表评论

    暂无评论内容