Python Leiden (NL) meetup: creating QR codes with python - Rob Zwartenkot

Tags: python, pun

(One of my summaries of the Leiden (NL) Python meetup).

Qr codes are everywhere. They’re used to transport data, the best example is a link to a website, but you can use them for a lot of things. A nice different usage is a WIFI connection string, something like WIFI:T:WPA;S:NetworkName;p:password;; . Rob focuses on the url kind.

There’s a standard, ISO/IEC 18004. QR codes need to be square. With a bit of a margin around it. The cells should also be square. You sometimes see somewhat rounded cells, but that’s not according to the standard. You sometimes see a logo in the middle, but that actually destroys data! Luckily there’s error correction in the standard, that’s the only reason why it works. There’s more to QR codes than you think!

He uses the segno as qr code library (instead of “qrcode”). It is more complete, allows multiple output formats, it can control error correction:

import segno
qr = segno.make("https://pythonleiden.nl/")
qr.save("leiden.png")

Such an image is very small. If you scale it, it gets blurry. And there’s no border and no error correction. We can do better:

import segno
qr = segno.make("https://pythonleiden.nl/", error="h")
# "h" is the "high" level of error correction, it allows
# for up to 30% corruption.
qr.save(
    "leiden.png",
    scale=10,
    border=4,
)

Segno can also give you the raw matix of cells. That way you can do some further processing on it. For instance with PIL (the Python Imaging Library). As an example, he placed a logo in the middle of the QR code.

How you can work with the matrix:

... same as before ...
for line in qr.matrix:
    for cell in line:
        ....

He went totally overboard with round dots and colors and a logo in the middle. At least on my phone, it still worked! Funny.

https://reinout.vanrees.org/images/2026/qr-example.png