QR (Quick Response) Codes and Barcodes are great ways to store and retrieve data. Often, QR Codes and Barcodes are used for storing contact information and URLs. More and more, apps and websites use QR Codes and Barcodes to make the process of sharing information easier and faster. If you want to be able to generate QR and Barcodes in Python, this is the tutorial you want to use. We’ll walk you through generating QR Codes and Barcodes in Python. We will be using some of the python modules called QRcode, python-barcode, and pillow.
What is a QR code:
QR codes (abbreviated from Quick Response Code) are two-dimensional barcodes first designed for the automotive industry in Japan. A part of the range of ISO/IEC18004, they are also referred to as Q codes and come in a variety of designs. A QR code consists of black modules (square dots) arranged in a square pattern on a white background. The information encoded can be text, URL, contact information, or other types of data. QR codes are read by dedicated QR code readers and scanner applications.
What is a barcode:
Barcode is basically an encoded image that’s machine-readable and the machine just reads the number that is below the barcode and it also reads the parallel lines like the spacing between the parallel lines as shown in this picture. It’s is mostly used in product identification. Barcode actually encodes visual patterns line, for example, the spacing between the lines or the number, and that’s how it gets the information.
Now let’s move ahead with the steps in generating QR Codes and Barcodes in Python:
How to generate QR code:
Required Modules:
QRcode: The Python-QRcode module is a pure Python library for generating QR codes. It can be used in both simple and complex QR code applications. It offers methods to encode arbitrary sequences of data into QR codes as well as a way to decode QR codes. The module can be installed using the command: pip install python-QRcode.
pip3 install QRcode
Code:
Here we’ll be generating our simple QR code that will contain only text.
#improting the module
import qrcode
#creating a variable for the text in which we want the QR code to link us
qr = qrcode.make('Hello Programmers')
#saving the QR code as a png file
qr.save('qrcode.png')
First, we imported the QRcode module then we wanted to write the text in which we want the QRcode to link for that we created a variable called QR and then we used a function from our QRcode module. The function is called make and that will just let us give the text to QR code then we used the save function to save our QR code in a png file.
Output:
From this you should get an output like this; it contains the text that we added earlier in the QR variable. The png file of the QR code will be stored in your project directory with the name specified above. And if you try to scan the code you should see the text Hello Programmers in your message.
Here we’ll be generating a little more complex QR code that will contain URL / Links.
#improting the module
import qrcode
# creating the variable for the QR code
qr = qrcode.QRCode(
version = 1,
box_size = 15,
border = 10
)
# adding a link for the QR code to open
data = 'https://www.google.co.in/'
qr.add_data(data)
qr.make(fit=True)
# adding the color
img = qr.make_image(fill = 'black', back_color = 'white')
img.save('qrcodewithlink.png')
here we imported the QRcode module same as the previous part then created a variable called QR, used function QRcode and added parameters in the function for version, size of the QRcode, and added one for the border, then we added data, for that we used string here and added the link
Output:
From this you should get an output like this; it contains the Link that we added earlier in the data. The png file of the QR code will be stored in your project directory with the name specified above. And if you try to scan the code you should be directed to the link that you added. In this example, if you scan the QR code you can go to google.com.
How to generate Barcode:
Required Modules
python-barcode: It is a Python module that creates barcodes, including Code 128 and Code 39. It’s just the bare minimum needed to get an SVG image of a barcode without having to manually draw out paths and characters yourself. It also provides us with different standard types of barcodes such as EAN-8, EAN-13, EAN-14, UPC-A, JAN, ISBN-10, ISBN-13, and many more. Run the following command in your terminal/command prompt to install it:
pip3 install python-barcode
pillow: It is also a required module, it will help us create the barcode in image formats. Run the following command in your terminal/command prompt to install it:
pip3 install pillow
Code:
Here we’ll be generating our barcode with the EAN-13 format module.
# importing EAN13 from the python-barcode module
from barcode import EAN13
# add any 12 digit number you would like to
number = '5909876123457'
# Now, let's create an object of EAN13
# make a class and pass the variable number created above
my_code = EAN13(number)
# save it
my_code.save("bar_code")
Output:
From this you should get an output like this; It would be stored in your project directory with the name specified above.
Here, as you can see our code worked but it is currently outputting in SVG format. To get the output in PNG format, we can make the following change in our code:
# importing EAN13 from the python-barcode module
from barcode import EAN13
# importing ImageWriter from python-barcode to generate an image file
from barcode.writer import ImageWriter
# add any 12 digit number you would like to
number = '5909876123457'
# make a class and pass the number with the ImageWriter() as the writer from the variable number created above
my_code = EAN13(number, writer=ImageWriter())
# save it with any desired name
my_code.save("bar_code_png")
Output:
Now you should get the file in PNG format, it should appear as bar_code_png.png in your project directory and would look similar to this:
QR codes are quite popular nowadays. They can store up to 4,296 characters in the code format. They are most often used to store URLs. In this tutorial, we have learned how to generate QR codes from Python source code with the python-qrcode library.
QR codes can be generated using the python-qrcode library. The library includes functions for generating QR codes based on URL requests, drawings, input boxes. If you need to use the QR code facility of your Python program, consider installing this library (if not already installed). We have used two modules, python-barcode for generating QR code barcodes with the code formatting and the qrcode which can be used to create QR codes visually with controllable parameters. Furthermore, we have explored a few more examples using the library. So, was it easy for you generating QR Codes and Barcodes in Python?
Here are some useful tutorials that you can read:
- How to receive Github webhooks in Python?
- How to convert an image to 8-bit image
- Create various charts using Python
- Extract stored Chrome Passwords and Decrypt them using Python
- Create API in Django Rest Framework Viewset
- Create Sudoku game in Python using Pygame
- Get Weather Information using Python
- Detect the number of faces using Python and OpenCV