Firstly before writing the codes for the qr code generator let us first understand what we mean by qr code.
What are Qr Codes?
Qr Codes which are also known as Quick Response codes are a type of matrix barcodes developed in 1994 by the Japanese automotive company DENSO WAVE by a man named Masahiro hara. Qr Codes can also be defined as a machine-readable code that consists of an array of black and white squares, typically used for storing urls or other information for reading by the camera on a smartphone or smart device.
What are Barcodes?
A barcode on the other hand is a machine-readable optical label that can contain information about the item to which it is attached.
Coding Time.
So we are done with the definition of qr codes and barcodes now let's dive into the coding.
Steps to Create the Qr Code Generator.
Firstly make sure that python software is installed in your system (more specifically python3), if it is not then go to https://www.python.org/ In order to download the latest version of python.
Then install a code editor of your choice if it's not installed already or you can use the python IDLE.
If you have installed it then go ahead to any terminal on your system and type in “pip install qrcode”.
Create a new folder and name it whatever you like.
After creating a new folder create a new file e.g main.py or “yourchoice”.py just make sure that the name of the file ends with the .py extension.
Then open the file or folder in your code editor of choice (for me i am using visual studio code).
After You are done you then type the following lines of codes in the editor
#main.py - filename
# Import the library needed to create the qr code
import qrcode
# Declare a variable to contain the function for creating the qr code and then pass some values in order to customize it.
qr = qrcode.QRCode(
version = 15,
box_size = 10,
border = 5
)
# Create a variable to accept input for the information that will be in the qrcode.
data = input("Enter URL or mmessage: ")
# add the inputted data to the variable
qr.add_data(data)
# This .make function help creates the qr code and the fit=True ensures that the entire dimension of the qrcode is used
qr.make(fit = True)
# the img variable holds the .make_image function which creates the image of the qrcode
img = qr.make_image(fill="black",back_color = "white")
# the save function stores the qr code in the specified file
img.save('mycode.png')
After putting down that line of code it is now time to run it by clicking the run button in whatever code editor you are using, this is the output I got.
And after that a mycode.png file was created, that is it below
NB: The comments in the codes explain each line of the code but if you want a more detailed explanation you can contact me on whatsapp or twitter to get it.
Whatsapp Number: +2347080934619
Twitter handle: https://twitter.com/jaminonuegbu
Conclusion
In conclusion, we saw how to create a QR code with python and now you can create one too, for either your website or maybe for storing other information.
Comments
Post a Comment