How to make a QR code with an image in Python

The faces of the inspectors, to whom queues often line up, look, to put it mildly, dissatisfied. And if this set of pixels is lost in the gallery… And so a couple of times a day. This whole procedure is annoying and infuriating!

Finding a solution

I’m tired of showing dull QR codes to dull administrators and security guards of establishments, and I decided to cheer up this rotten occupation a little.

The idea was to generate funny QR codes. Immediately ran to the Internet in search of ready-made libraries and modules.

From a worthy module caught my eye MyQR (Amazing-QR):

Quality leaves much to be desired
Quality leaves much to be desired
  • Pros: In Python, the ability to create gif images

  • Minuses: Jackal quality, aesthetically incompatible with the image of the strip

  • A source – https://github.com/x-hw/amazing-qr


Unfortunately, all of them turned out to be too primitive, generating a compressed 8/10 Jpeg jackals with scary stripes in the QR code, which did not suit me at all.

Crutch Time

It was decided to write crutch add-on module for qrcode (Python).

Decided on the minimum requirements for it:

  • Good output image quality

  • Lack of incomprehensible positional bands

  • Possibility to change the style and color of the pixels* of the code

All code is on Githubthe link will be below, in the sources

So, let’s begin…

First of all, install all dependencies with the command: pip install -r requirements.txt

Depending on your OS, install the pyzbar module. Instruction: https://pypi.org/project/pyzbar/

If you have MacOS and zbar errors come out, then before each OS start, run the command: export DYLD_LIBRARY_PATH=/opt/homebrew/lib

Everything else for quick use is already set up and ready to go, just open the file example.py

from path import Path
from main import gen_qr_code


text = "текст для шифровки в QR код"
path_to_download = Path().joinpath("example", "example.jpg")  
path_to_save = Path().joinpath("example", "example.png") 

gen_qr_code(text, path_to_download, path_to_save)

Module Path needed to work with paths. Function gen_qr_code is a QR code generator, we prepare for it:

  • text – text, url, what will be encrypted in the QR code

  • path_to_download – path to the background image (together with the name and format of the image itself)

  • path_to_save – path where to save the finished image (in .png format)

1) Pass 3 arguments to gen_qr_code()

2) Run from the root directory of the command – python example.py

3) Done!

Read excellently
Read excellently

Operation logic banal – The background image is automatically adjusted to the future dimensions of the QR code, then the qrcode module generates a number matrix from the text that was passed to it, after the module Pillow draws a png image from the matrix, and finally the png is overlaid on the background image.

Thanks!

I want to thank you for your attention, my dears, good luck to you and happiness in the piggy bank. Let’s make this world a little more fun!

If anyone is interested in more detailed functionality (changing the color, brightness, shape of qr code pixels – please write to me, I will share with pleasure)

Sources:

Source code on Github: https://github.com/Ruslanch0s/qrgenerator

Working example of functionality (bot in telegram): https://t.me/QrHub_bot

Similar Posts

Leave a Reply