This tutorial is about creating a QR code (quick response code) using Raspberry Pi B and Python 3.7. QR code is an optical machine-readable code. Optical means it can be decoded using any camera device. A direct competitor of QR code is Bar code. QR’s advantage over Bar Code is it’s faster to read and can store much more information than Bar code. Any device with a camera can be turned in to QR code reader, whereas bar code needs a laser dedicated device to decrypt Bar codes.
For Raspberry Pi, a dedicated QR library/package is available for QR creation. To import the QR code package in the application code, you first need to install it. To install the package, open the Terminal of Raspberry Pi and type:
$ pip3 install qrcode
Note: If you are working with python 2 instead of pip3, use pip to install the library. You must use pip or pip3 to install all of your libraries or packages.
A simple QR code generation using qrcode package is below.
First, I imported the qrcode package into the application code. Then the statement qrcode.make(‘ENGINEERS GARAGE’) is encoding the text ‘ENGINEERS GARAGE’ in qrcode format. The encoded QR is saved in the code variable. Finally, to save the QR code in the image, I passed the file name code.save(‘qrtest.png’) in which the qrcode will be saved.
The output image is shown above. The size of the image is 290×290. Image parameters like size, color, background, borders, and QR box size can be adjusted using the qrcode library’s functions. You can import other image manipulation packages in the application code, and filters can be applied to the output image to enhance brightness, contrast and sharpness.
To decode qrcode, we need to install some more packages. Zbarlight is a popular wrapper for python. It is used to decode and extract the qrcode from an image file.
Open Terminal in raspberry pi and enter the following:
$ apt-get install libzbar0 libzbar-dev
$ pip3 install zbarlight
Use pip if you are working with python 2. You may incur errors during compilation. Most probably, the error is a missing package. Zbarlight depends on other packages preinstalled in the Python directory, but sometimes you need to install them manually. Install all packages one by one (For each package installation, visit https://pypi.org/). If it says, PIL not found, install it using the command
pip install –no-index -f https://dist.plone.org/thirdparty/ -U PIL
After successful installation, import the zbarlight package in your code. You also need to import the image attribute from the PIL package. PIL package is used to import and work with images.
Variables and statements description
- file_path variable is assigned the image address.
- With open(file_path, ‘rb’) as image_file: Opens the file as image file
- Image = Image.open(image_file) image object is created
- load() loads the image object containing the pixel data
- Codes = zbarlight.scan_codes([‘qrcode’],image) Fetches/decodes the text present in the pixels.
Finally print statement prints the decoded text.
I used Thonny ide present in the Raspbian operating system to write and test code. On the left side, you can see the generated image placed on the desktop. You can see the Thonny ide qr in the extraction code. The qr image file path is pointing to the desktop. The result of the decode is displayed in the shell window beneath the code window.
Let’s Diy this project: Where to purchase parts?
Raspberry pi (Mouser Electronics)