Generating QR codes with Python

Generating QR codes with Python

Hello everyone!
Ever wondered what a QR code is? Or how a QR code is built? Or why do we even use QR codes? If not, this article is for you...

toa-heftiba-i05sF4afLYs-unsplash.jpg

You would have definitely seen these black and white patterns in a box in payments, visiting cards, tickets, gyms, electronics manuals, etc where some information has to be shared. Let's find out what exactly are these.

Quick Response code, in short, QR code, was first designed in 1994 for the automotive industries in Japan. These small codes contain some information that the creator would want to share. Just by scanning these on our mobile phones, we can get that particular data. This data can be a link to download an app, a phone number, a wifi password, or even a simple text message.

A QR code works in a similar way as a barcode at the supermarket, but it can store information more than 300 times the barcode can do as QR code stores data in 2-dimension. The black squares and white dots represent certain pieces of information. The modern-day QR code consists of seven parts. Each of these parts creates a sort of pixel pattern. Each element has a specific purpose that conveys certain information through the Code.

Enough of talks and definitions. Let's go to the practical aspect.

image.png

Scan the above code and you will land on the LCO website. Let's find out how can we generate these QR codes using Python programing language in a simple and easy way.

Let's get into the code part...

Firstly, let us install qrcode package. To do this, simply run the below command in the terminal.

pip install qrcode

pip is a package manager for Python that allows you to install and manage additional libraries and dependencies that are not distributed as part of the standard library.

By this time, qrcode package must be installed. To verify this, run the below command in the terminal. This lists out some of the data related to the package such as version, author etc.

pip show qrcode

Screenshot (195).png


Open your favourite IDE (you can use any of Spyder, VScode, PyCharm, Atom, etc) and start writing the codes.

import qrcode as qr

Firstly, let us import qrcode package and call it as qr (shorthand notation).



link = "https://web.learncodeonline.in/"
img = qr.make(link)
img.save("qrcode.jpg")

We have stored the URL of the website in a variable called link. In the next line, we have made use of make function of qrcode class and have sent the URL of the website as a parameter. Later, we save this using the save method and call the image as "qrcode.jpg".

By running this file, an image is created with a name of "qrcode.jpg" in the current directory. Open the image and scan the QR code generated using your smartphone and you will be taken to the website.

Output: image.png


This is how you can easily create a QR code using Python. Ofcourse, the possibilities are endless. For example instead of a link, we can store a simple text message in the QR code as follows:

img = qr.make("It's so simple")
img.save("qrcode.jpg")

Output: image.png


We can make the QR code a little complicated by introducing QRcode class and passing some parameters into it such as version number, border, box_size etc as follows:

qr_code = qr.QRCode(
    version=1,
    box_size=5,
    border=2,
)

qr_code.add_data("A little complicated, but not hard!")
qr_code.make(fit=True)

img = qr_code.make_image()
img.save('image.jpg')

The version parameter is an integer from 1 to 40 that controls the size of the QR Code (the smallest, version 1, is a 21x21 matrix).

The box_size parameter controls how many pixels each box of the QR code is.

The border parameter controls how many boxes thick the border should be.

The add_data takes the information to be stored in the code as a parameter.

make(fit=True) function ensures that the entire dimension of the QR Code is used.

make_image is used to convert it into an image file and store it.

The save function stores the image as "image.jpg" file in the current directory. You can also store it in a png format by writing

img.save('image.png')

Output : image.png



fill_color and back_color can change the background and the color of the QR code, when using the default image factory.

import qrcode as qr

qr_code = qr.QRCode(
    version=1,
    box_size=5,
    border=2,
)

qr_code.add_data("A little complicated, but not hard!")
qr_code.make(fit=True)

img = qr_code.make_image(fill_color="yellow", back_color="red")
img.save('image.jpg')

Output : image.png


Can QR codes be dangerous?

QR codes have the potential to save time and help connect you to important services. But these QR codes can be used for malicious activities too. It can do much more than simply display texts or a website.

The security risks arise when hackers store malware software to a certain QR Code. But it is impossible for a QR code to be hacked as it requires the dots of the code to be changed. The content inside them cannot be changed once generated.

I would still recommend you to use QR codes as they are instant and easy to use. But whenever you are scanning QR codes, make sure that it is from a trusted source.




That's the end of this article. I hope you have learned a lot and enjoyed it as well. Thanks for your valuable time.

- Maneesh

Did you find this article valuable?

Support Learn Code Online by becoming a sponsor. Any amount is appreciated!