QR Code Generator
QR codes have become increasingly prevalent in our world and are rapidly replacing the traditional barcode. This trend is likely to continue as QR codes offer a more efficient way of storing and retrieving information.
Are you ready to create your own QR code ?
Prerequisites :
The code starts by importing the ‘qrcode’ library and the ‘Pillow’ library (also known as PIL).
pip install qrcode
pip install pillow
Program :
To start, the user is prompted to input a link to convert into a QR code.
# Ask user to enter URL link
data = input("Enter URL link : ")
For example : https://xportal.com
Next, the user is prompted to input the filename and desired saving path for the QR code image. If the ".png" extension is not included in the user-entered filename, the code automatically adds it.
# Ask user to enter the filename # CHANGE THE PATH
file_path = "/Your/Saving/Path/For/QR_code/"
file_name = input("Enter the file name : ")
if not file_name.endswith(".png"):
file_name = file_name + ".png"
After this, a QRCode object is created with certain features such as the version, error correction, box size, and border. The data entered by the user is added to this QRCode object.
Next, the QR code image is generated from the QRCode object and saved to the file specified by the user.
# Create QRcode Object
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=2,
)
# add data to QRcode
qr.add_data(data)
qr.make(fit=True)
# Generate the QRcode image
img = qr.make_image(fill_color="black", back_color="white")
# Save the QRcode image
img.save(file_path + file_name)
Finally, a confirmation is displayed that the QR code has been successfully generated and saved to the specified file.
print("QRcode successfully generated in the file " + file_name + " in the path " + file_path)
Conclusion :
In summary, this code allows the user to convert any link into a QR code, then save it as a PNG image with a custom filename and saving path.
Leave a comment if you have any questions
If you enjoy my blog posts, you can support me by a tip in my wallet.
Thanks for you support
BTC : bc1qvfmetg2d36mmntrg56ld0tdrte8cqeygjxdpsg
ETH | USDC | USDT : 0x02AbfBf22fA72d068Ff305e58dF782e58F863274
DOGE : DLtGbPrFvwW5y7jFuvuDAkZGNB2eAErAxA
See you soon ! 🤙
Casper_X 👻
Here is the full program :
import qrcode
# Ask user to enter URL link
data = input("Enter URL link : ")
# Ask user to enter the filename
file_path = "/Your/Saving/Path/For/QR_code/"
file_name = input("Enter the file name : ")
if not file_name.endswith(".png"):
file_name = file_name + ".png"
# Create QRcode object
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=2,
)
# Add data to QRcode
qr.add_data(data)
qr.make(fit=True)
# Generate QRcode image
img = qr.make_image(fill_color="black", back_color="white")
# Save QRcode image
img.save(file_path + file_name)
print("QRcode successfully generated in the file " + file_name + " in the path " + file_path)