Calmcode - qr code: settings

Settings

1 2 3 4

QR codes have a very cool property: they are resistant to errors! You can explore this by exploring the many settings in the QRCode()-call.

Error Correction Quality

You can specify the error correction quality when decoding a QR code.

qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_H, # L < M < Q < H
    box_size=5,
    border=4,
)
qr.add_data('https//calmcode.io')
qr.make(fit=True) # Setting fit=True ensures the minimum size.

img = qr.make_image(fill_color="black", back_color="white")
img

There are multiple options to pick from, but the order goes:

  • qrcode.constants.ERROR_CORRECT_L: About 7% or less errors can be corrected.
  • qrcode.constants.ERROR_CORRECT_M: About 15% or less errors can be corrected.
  • qrcode.constants.ERROR_CORRECT_Q: About 25% or less errors can be corrected.
  • qrcode.constants.ERROR_CORRECT_H: About 30% or less errors can be corrected.

Detect and Decode

If you're interested in running the same error-correcting experiment, you'll appreciate the following helper function.

import matplotlib.pylab as plt
from PIL import Image
import numpy as np

def convert_msg(img_path):
    img_broken = Image.open(img_path)
    np_arr = np.array(img_broken.convert('RGB'))
    data, bbox, straight_qrcode = detector.detectAndDecode(np_arr)
    print(f"found msg: {data}")
    return img_broken

convert_msg('high-quality.png')