Python

Python QR Code generator

What are QR codes?

QR codes are some of the fastest-growing trends in today’s world when it comes to sharing any sort of information. It is quickly becoming adopted for all sorts of different uses which are not limited to sharing lengthy URLs, making payments and unlocking devices and whatnot. A QR code is simply a square picture that is cluttered with white and black boxes of different shapes and sizes. This arrangement of boxes is what allows the information to be stored in a simple picture. This piece of code is machine-readable through the help of a camera. What usually happens is that the QR code is scanned by the camera through a specific application that has a concern with QR code we are scanning. The application accesses the phone’s camera to take a picture of the code and the resulting picture is then analyzed by the application which allows information to be extracted.

Where are QR codes used?

The QR code technology originated in 1994 from Japan where it was used in the automotive industry. The need for the QR code came from being unable to store large amounts of information in the standard barcode that we still see today. From there, the QR code gained rapid popularity and saw its use being adopted in all sorts of industries all over the world.

Whenever an event is about to happen, it is advertised through various different means. Those means are often posters splattered across towns and online advertisements. These ads now contain a QR code that we can scan that takes us to the events page. The page could be located on either Facebook or have its own website. We will simply pull up our phones and scan the code and end up on the exact page we would want to look up.

Locks have also started to incorporate this technology in various different ways. One of the simplest out there is used for cycle locks. The lock can be unlocked simply by scanning the QR code available on it. The app that exists for such locks makes sure that the user which is scanning the lock is the actual owner of the bicycle too. It won’t just unlock from anyone’s phone even if they have the app. This shows us that QR codes are very secure as well.

One of my favorite ways in which QR codes are being used today is to make payments. The process of purchasing and selling things has become so simple now. All of you have to do is open the payment app and point your phone’s camera at the QR code of the place where you want to make the purchase from. Once the code has been scanned, the money is deducted from your account and delivered to the recipients account almost instantaneously. The reason I love this particular use of QR is that I don’t need to carry even a card to make purchases anymore. This really takes my mind off from taking care of another belonging.

How can we generate QR codes?

You may already have guessed it, the answer is Python! The beauty of Python lies in its ability to perform almost any task in the world and QR code generation is one of them. There are obviously several other ways to generate them but we will only be discussing Python here.

First thing’s first, we will make sure that we have a stable and working Python environment ready on our Linux systems.

Open up the terminal and run ‘sudo apt-get update’ and get the necessary updates for your packages. Then identify that you have Python installed by running ‘python3 -v’. If everything is installed correctly you will see the version of Python installed as the output to the above program. Now we will open our IDE of choice and begin to create our very own QR code!

Let the coding begin!

If you don’t have ‘pyqrcode’ and ‘pypng’ already installed in your system then you have to go back to the terminal and run ‘pip install pyqrcode && pip install pypng’. Once that has been taken care of, start by doing the necessary imports as shown in the code below and mimic the code too. You can replace the link in the variable to any valid URL you want to customize your small project.

The name of the image created can be changed by replacing the name argument of the url.png call. This method allows us to create a .png image which is now usable for scanning. If you have a preference for the type of image you want to create, you can also choose to create an SVG image by changing the function call from ‘url.png’ to ‘url.svg’. The picture below shows the result from the terminal. You can even scan this picture from your mobile app and use it.

How do I use the image I just created?

All you need to scan that image is a QR code scanner app installed on your phone.

How do I use the image I just created?

All you need to scan that image is a QR code scanner app installed on your phone.

You can get very creative with your QR codes too. A lot of companies often times have easter eggs and similar stuff hidden in their codes which can give the user quite a surprise.

About the author

Zeeman Memon

Zeeman Memon

Zeeman is a freelance content marketer, software engineer and tech blogger who loves to blog on his tech blog in his free time.