Ask Your Question

Adding a QR Code to an image?

asked 2019-05-28 10:53:14 -0500

johnnyrobot gravatar image

updated 2019-05-28 10:57:19 -0500

Hi all,

I've got an inspection project that I'm working on. Here's what I'd like to do. (maybe there's a better way to do this, feel free to let me know)

  1. acquire the image from a camera. (Done)
  2. Inspect the image for for a PASS/FAIL condition. (Done)
  3. Create a QR Code containing Image date, filename, and inspection result. (Done)
  4. Add the QR Code in the upper left of the image acquired by the camera... (Can't figure that one out)

I'm using the Python qrcode module to create the qr code. I can save the qrcode to .png and then imread it with Opencv and do it that way, but I'd like to avoid unnecessarily writing to disk.

I should add that the reason I am doing this is so that I can later run a python script that will read these QR Codes. For example, sorting the image files by PASS/FAIL result.

If there's a more elegant way to add this data to the image (custom meta-data?) I'm totally open to try it.

Any suggestions?

import cv2
import qrcode
import numpy
import scipy.misc

image = cv2.imread('0000001.bmp' ) # This is where I would be acquiring image from camera

qr = qrcode.QRCode(

# These strings will be written by the inspection process
i_result = 'Failed'
i_timestamp = 'This time yall'
i_filename = '00001.bmp'

# Add the strings to the QR Code

# Make the QR Code
qrcode = qr.make_image(fill_color="black", back_color="white")

# How to make the QR code an OpenCV object???
edit retag flag offensive close merge delete

1 answer

Sort by ┬╗ oldest newest most voted

answered 2019-05-30 10:59:52 -0500

johnnyrobot gravatar image

I Figured out a way to do it!

image description

Reading through the documentation for PyQRCode, I found that there is the option to export the QR code as text. From there I was able to write it to a numpy array. Hopefully someone finds this helpful in the future.

import cv2
import pyqrcode
import numpy as np
import datetime
import math

image = cv2.imread('chain.jpg', )

timestamp = str(

# Make the QR Code (CSV) - These values are populated by the inspection process
temp_qrcode = pyqrcode.create(filename+','+timestamp+','+result)

# Convert the QR Code to a string
temp_qrcode = temp_qrcode.text()

# Remove all instances of "Newline" from string
temp_qrcode = temp_qrcode.replace('\n', '')

# Get the total number of pixels (characters) from the QR Code string
size = len(temp_qrcode)

# The QRcode is square, square root of the size will give us image dimensions.
shape = int(math.sqrt(size))

# Create a numpy array that will become our QR Code image
qr = np.zeros([shape, shape], dtype='uint8')

# Set counters for iteration.
row = column = 0

for char in temp_qrcode:
    if char == '0':
        qr[row][column] = 255
        column = column + 1

    if char == '1':
        qr[row][column] = 0
        column = column + 1

    if column == shape:
        row = row + 1
        column = 0

# Scale up the QR code.
qr = cv2.resize(qr, (100, 100), fx=0, fy=0, interpolation=cv2.INTER_LANCZOS4)

# Apply border to QR code
qr = cv2.copyMakeBorder(qr, 2, 2, 2, 2, cv2.BORDER_CONSTANT, value=0)

# Overlay the QR Code onto the
x_offset = y_offset = 0
image[y_offset:y_offset+stamp.qr[0], x_offset:x_offset+qr.shape[1]] = qr

cv2.imwrite(filename, image)
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-05-28 10:53:14 -0500

Seen: 599 times

Last updated: May 30 '19