Python Pillow: Insert QRCode into an Image Using Image.paste()

In this tutorial, we will introduce how to add a qrcode image on an image using python pillow Image.paste().

Python Pillow - Insert QRCode into an Image Using Image.paste() for Beginners

1.Open an image

import qrcode
from PIL import Image

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

2.Create qrcode image

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

In this code, we have created a qrcode image img_qr.

3.Determine the position of img_qr in img_bg

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

In this code, we will put the qrcode image on the bottom and right of img_bg.

4.Put the qrcode image on the background image

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

Run this code, we will see this background image:

Python Pillow - Insert QRCode into an Image Using Image.paste()