Generate QR code image with Python, Pillow, qrcode

Posted: | Tags: Python, Pillow, Image Processing

You can easily generate QR code images using Python's QR code generation library qrcode. It is also possible to embed a QR code in another image or embed an image in a QR code using Pillow.

This article describes the following contents.

  • Python QR Code image generator: qrcode
  • Generate QR code image from the command line
  • Generate QR code image with Python code
    • QR code version of the generated image: version
    • Error correction level: error_correction
    • Cell size: box_size
    • Margin width: border
    • Color: fill_color, back_color
  • Embed the QR code into the image
  • Embed the image into the QR code

See the following article on how to detect and read QR codes. Although not thoroughly verified, ZBar (pyzbar) seems to have better detection accuracy.

Python QR Code image generator: qrcode

The QR code is a two-dimensional code composed of square black and white dots (cells) standardized by ISO (ISO / IEC 18004) and JIS (JIS-X-0510).

Python has a library qrcode for generating QR code images.

It can be installed with pip (or pip3).

$ pip install qrcode

Please also install Pillow (PIL).

$ pip install pillow

Generate QR code image from the command line

If you only want to create a simple QR code image, you can use the command line.

Once you have qrcode installed, you can use the qr command.

$ qr "text for qrcode" > qrcode.png

In this example, a QR code image file named qrcode.png containing string data text for qrcode is generated. The image size is adjusted automatically according to the length of the string.

QR code contains only a string. How to process a string depends on the application that reads QR code.

For example, in many smartphone camera apps that support QR code reading, if QR code contains URL, a notification whether to open the URL is displayed, and if the user allows it, it opens in the browser.

Generate QR code image with Python code

qrcode.make() creates PilImage object.

With PilImage object, you can use the attributes and methods of the Pillow(PIL) Image object, such as size.

Use save() to save as an image file.

import qrcode

img = qrcode.make('test text')

print(type(img))
print(img.size)
# <class 'qrcode.image.pil.PilImage'>
# (290, 290)

img.save('data/dst/qrcode_test.png')

python qrcode

You can also use the QRCode class to change the details.

The basic flow is as follows.

qr = qrcode.QRCode()
qr.add_data('test text')
qr.make()
img = qr.make_image()
img.save('data/dst/qrcode_test2.png')

You can make various settings.

qr = qrcode.QRCode(
    version=12,
    error_correction=qrcode.constants.ERROR_CORRECT_H,
    box_size=2,
    border=8
)
qr.add_data('test text')
qr.make()
img = qr.make_image(fill_color="red", back_color="#23dda0")
img.save('data/dst/qrcode_test2_2.png')

python qrcode advance

Each parameter will be described below.

QR code version of the generated image: version

The version of the QR code of the generated image can be set to version.

For the relationship between the information capacity and versions of QR code, refer to the official page of Denso Wave below.

As the value of version increases, the number of cells (square black and white dots) included in the QR code increases, and the image size and the information capacity increase. The minimum value of version is 1, and the maximum is 40.

By specifying version, you can generate a QR code image larger than the optimal version (minimum size), so it is useful when you want to create multiple QR codes with different data contents with the same size.

Note that even if you specify a smaller version, if the data does not fit, it will automatically be the minimum version that fits the data.

Error correction level: error_correction

You can set the error correction level with error_correction.

There are the following four levels. The values in parentheses indicate error correction ability (error correction rate for all codewords).

  • qrcode.constants.ERROR_CORRECT_L (Approx 7%)
  • qrcode.constants.ERROR_CORRECT_M (Approx 15%, default)
  • qrcode.constants.ERROR_CORRECT_Q (Approx 25%)
  • qrcode.constants.ERROR_CORRECT_H (Approx 30%)

For more information about the QR code error correction feature, refer to the official page of Denso Wave below.

When generating a QR code image from string of the same length, increasing the error correction level increases the size of the generated QR code image.

Cell size: box_size

With box_size, you can set the size of the cell (square black and white dot) in pixels.

Even if version is the same (the number of cells is the same), you can change the size of the generated image by changing box_size.

The default value is 10.

Margin width: border

You can use border to set the width (number of cells) of the outer margin.

The number of pixels in the margin width is box_size * border.

The default value is 4, which is the minimum width in the specification.

Color: fill_color, back_color

You can set the color with the fill_color and back_color of the make_image() method.

Specify fill_color and back_color using color names black,white, red, etc. or the #xxxxxx style.

Embed the QR code into the image

PilImage objects can be pasted directly to other images using the Pillow Image object's paste() method.

The sample code to paste the QR code in the lower right corner is as follows.

import qrcode
from PIL import Image

img_bg = Image.open('data/src/lena.jpg')

qr = qrcode.QRCode(box_size=2)
qr.add_data('I am Lena')
qr.make()
img_qr = qr.make_image()

pos = (img_bg.size[0] - img_qr.size[0], img_bg.size[1] - img_qr.size[1])

img_bg.paste(img_qr, pos)
img_bg.save('data/dst/qr_lena.png')

python qrcode pillow image

For more information about paste(), see the following articles:

Embed the image into the QR code

It is easy to embed the image into the QR code.

Note that superimposing an image on a QR code is not officially recommended.

It is all right to use QR Codes with colors or an inserted illustration?

Users of QR Code must be careful about deforming the code by overlaying it with an illustration or putting a a design on it. If it is deformed, the QR Code's error correction function is invoked where even a tiny missing or smeared part renders the reading operation impossible, or the response of the operation may become slow.
DENSO WAVE recommends that you use the QR Code following the specifications in the JIS or ISO standard to assure stable reading of the QR Code. Furthermore, if the QR Code is overlaid with an illustration or design, the resultant code may not be called a QR Code any longer, because there is no standard for doing this kind of operation. Although DENSO WAVE waives its rights to QR Codes as long as they follow the JIS or ISO standard, this is not necessarily the case with codes that do not follow the standard. DENSO WAVE may decide to exercise its patent rights against such codes.
If you are contemplating arranging an illustration or design involving a QR Code, please consult with DENSO WAVE about your project beforehand.

face = Image.open('data/src/lena.jpg').crop((175, 90, 235, 150))

qr_big = qrcode.QRCode(
    error_correction=qrcode.constants.ERROR_CORRECT_H
)
qr_big.add_data('I am Lena')
qr_big.make()
img_qr_big = qr_big.make_image().convert('RGB')

pos = ((img_qr_big.size[0] - face.size[0]) // 2, (img_qr_big.size[1] - face.size[1]) // 2)

img_qr_big.paste(face, pos)
img_qr_big.save('data/dst/qr_lena2.png')

python qrcode pillow image2

Depending on the size of the image to be embedded, it is safe to set the error correction level (error_correction) to a high level. Also, when embedding a color image in a black and white QR code, it is necessary to convert the QR code to the RGB mode with convert('RGB').

Related Categories

Related Articles