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 venvpython3 -m venv venvpython3 -m venv venv
Enter fullscreen mode Exit fullscreen mode
The next step would be to activate the virtual machine
source venv/bin/activatesource venv/bin/activatesource venv/bin/activate
Enter fullscreen mode Exit fullscreen mode
To deactivate a virtual environment you will need to type:
deactivatedeactivatedeactivate
Enter fullscreen mode Exit fullscreen mode
Next step would be to install the qrcode package
pip install qrcodepip install qrcodepip install qrcode
Enter fullscreen mode Exit fullscreen mode
In your Python file make sure you import the qrcode module
import qrcodeimport qrcodeimport 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
暂无评论内容