Generating QR-Codes in Python using QRCode Library
Hello everyone, In this tutorial, we will learn how we can Generate our own QR-Codes with just a few lines of codes. For this, we’ll be using ‘QRcode’ and ‘Pillow’ libraries. Pillow is a python imaging library(PIL) for image operations and manipulations which makes it easy to save our generated QR-Code as .png or .jpg format. Let’s start this tutorial with a brief introduction of QR-codes.
What is QR-Code?
QR-Code is a Quick Response Code which is is an optical image matrix that contains the data for which it is made. They are machine-readable codes whose data can be extracted with a QR-Coe Scanner and they are used in a wide range of applications like to store URLs omitting the need to write them, in-store product labeling, Tracking, Payments, etc.
Let’s make our own QR-Codes moving on with the installation of the required modules.
You can also generate QR code in Python using pyqrcode library
Installing QRcode in Python
To install the QRcode library run the following pip command.
pip install qrcode[pil]
This command will automatically install the QRcode library and all the dependencies including pillow in our python environment and you can refer to the official documentation here.
With the successful installation, let’s move forward making some QR-codes.
Making QR-Codes using Python
import qrcode my_qrcode = qrcode.make(r'https://www.codespeedy.com') my_qrcode.show()
The output of the above code –
Modifying QR-Code in Python
In this section, we’ll be creating a modified QRcode having different background and code color, size and will save it.
import qrcode mod_qrcode = qrcode.QRCode( version=2, box_size=5, border=1, ) mod_qrcode.add_data(r'https://www.codespeedy.com/blog/') mod_qrcode.make(fit=True) qrcode_image = mod_qrcode.make_image(fill_color="blue", back_color="yellow") qrcode_image.show() qrcode_image.save('codespeedy_code.png')
We used QRCode class which allows us to modify our QRcode in which we have defined the following parameters.
- version – The size of our QRcode is control by an integer value that ranges from (1 to 40). The default value is 1 corresponds to the matrix of dimension(21 x 21). The formula for dimension is (4*version + 17 dots each side). QRCode with a high version can store more data.
- box_size – It serves as a purpose of zoom to our QRcode.
- border – As per the name, It is the border width that we may want to apply to our QRcode.
QR code in Python – Color Background
After that, we have used make(fit=True) which means that the QRcode will automatically adjust the size to best fit in the window. Then, we have just made the image of our QRcode using make_image() with fill_color or pattern color as blue and back_color or background-color as yellow. They have default values of black and white respectively. In last we use save() to save the QRcode as an image.
This is how our QRcode will look.
We want you to generate your own QRCodes and Hope you like this tutorial. If you have any doubt feel free to comment below.
You may like to learn