This tutorial is about creating a QR code (quick response code) using Raspberry Pi B and Python 3.7. QR code is a machine-readable optical code. Optical means it can be decoded using any camera device. A direct competitor to the QR code is the barcode. The advantage of QR over barcode is that it is faster to read and can store much more information than barcode. Any device with a camera can be turned into a QR code reader, while barcode needs a dedicated laser device to decrypt the barcodes.
For Raspberry Pi, a dedicated QR library/package is available for QR creation. To import the QR code package into the app code, you first need to install it. To install the package, open the Raspberry Pi Terminal 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 your libraries or packages.
QR coding
A simple QR code generation using the qrcode package is below.
First, I imported the qrcode package into the application code. So the instruction 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 image size is 290×290. Image parameters such as size, color, background, borders and QR box size can be adjusted using the qrcode library functions. You can import other image manipulation packages into the application code, and filters can be applied to the output image to increase brightness, contrast, and sharpness.
Decoding QR
To decode the qrcode, we need to install some more packages. Zbarlight is a popular wrapper for python. It is used to decode and extract qrcode from an image file.
Open Terminal on the Raspberry Pi and type the following:
$ apt-get install libzbar0 libzbar-dev
$ pip3 install zbarlight
Use pip if you are working with python 2. You may experience errors during compilation. Most likely, the error is a missing package. Zbarlight depends on other pre-installed packages in the Python directory, but sometimes you need to install them manually. Install all packages one by one (for each package installation, visit If it says PIL not found, install it using the command
pip install –no-index -f -U PIL
After successful installation, import the zbarlight package into your code. You also need to import the image attribute from the PIL package. The PIL package is used to import and work with images.
Description of variables and declarations
- The file_path variable receives the image address.
- With open(file_path, 'rb') as image_file: Open the file as an image file
- Image = Image.open(image_file) the image object is created
- load loads the image object containing the pixel data
- Codes = zbarlight.scan_codes(('qrcode'),image) Searches/decodes the text present in the pixels.
Finally, the print statement prints the decoded text.
I used Thonny ide present in 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 Thonny ide qr in the extraction code. The qr image file path is pointing to the desktop. The decoding result is displayed in the shell window below the code window.
Let's do this DIY project: Where to buy parts?
Raspberry pi (Mouser Electronics)