Generate QR code image with Python, Pillow, qrcode
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:
- Error correction level:
- Cell size:
- Margin width:
- QR code version of the generated image:
- Embed the QR code into the image
- Embed the image into the QR code
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 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 "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 read 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
PilImage object, you can use the attributes and methods of the Pillow(PIL)
Image object, such as
- Image Module PIL.Image.Image — Pillow (PIL Fork) 6.2.1 documentation
- How to use Pillow (PIL: Python Imaging Library)
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')
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')
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
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
1 and the maximum value is
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
There are the following four levels. The values in parentheses indicate error correction ability (error correction rate for all codewords).
qrcode.constants.ERROR_CORRECT_M(Approx 15%, default)
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
box_size you can set the size of the cell (square black and white dot) in pixels.
version is the same (the number of cells is the same), you can change the size of the generated image by changing
The default value is
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
back_color of the
back_color using color names
red, etc. or the
Embed the QR code into the image
PilImage objects can be pasted directly to other images using the Pillow
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 - img_qr.size, img_bg.size - img_qr.size) img_bg.paste(img_qr, pos) img_bg.save('data/dst/qr_lena.png')
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 - face.size) // 2, (img_qr_big.size - face.size) // 2) img_qr_big.paste(face, pos) img_qr_big.save('data/dst/qr_lena2.png')
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