Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Raspberry Pi camera QR code encoding and decoding

By Usman ali Butt February 25, 2021

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.

Encoding QR

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.

Decoding QR

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)

You may also like:

  • Python Basics
    RPi Python Programming 06: Python basics
  • Raspberry Pi 4
    RPi Python Programming 01: Introduction to Raspberry Pi 4

  • Raspberry Pi-based smart home security system

  • Raspberry Pi camera QR-code scanning

  • Getting started with Raspberry Pi

Filed Under: Electronic Projects, Microcontroller Projects, Raspberry pi

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Right Half Plane Zero
  • dc-dc converter in series
  • Single ended measuring ports and balanced antenna
  • Thermal modelling of repetitive power pulse
  • Permittivity and Permealibility in CST

RSS Electro-Tech-Online.com Discussions

  • Fun with AI and swordfish basic
  • Microinverters and storeage batteries?
  • FFC connector white
  • Is AI making embedded software developers more productive?
  • Can I make two inputs from one??

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe