QR code with image

0 Comments

PythonのQRコード画像生成ライブラリ「qrcode」

pip install qrcode

QRコードの中に画像を埋め込む

  1. download lena.png
  2. create QR code with string: “I am Lena”
  3. add mini image overlay of QR code.
  4. save to new image: qr_lena2.png
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ref: https://note.nkmk.me/python-pillow-qrcode/
# pip install qrcode
# wget http://optipng.sourceforge.net/pngtech/img/lena.png
import qrcode
from PIL import Image
import requests
import os


def dw_img(fimg='lena.png', img_url=None):
    print("download image:", fimg)
    if img_url is None:
        image_url = 'http://optipng.sourceforge.net/pngtech/img/lena.png'
    img_data = requests.get(image_url).content
    with open(fimg, 'wb') as handler:
        handler.write(img_data)


# ----------draw image overlay------------------------------------------------------
fimg = "lena.png"
if not os.path.exists(fimg):
    print("down load file:", fimg)
    dw_img()

# face image for overlay
img = Image.open('lena.png')
face = img.crop((218, 219, 373, 374)).resize((60, 60), Image.LANCZOS)

qr_big = qrcode.QRCode(
    error_correction=qrcode.constants.ERROR_CORRECT_H
)
qr_big.add_data('I am Lena')
qr_big.make()
# convert qr code to RGB image.
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('qr_lena2.png')

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Try_build_chromium

ref:https://chromium.googlesource.com/chromium/src.git/+/65.0.3283.0/docs/linux_build_instructions.md?pli=1# 1.1 Install depot_tools cd ~/oh/gitdir git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git export PATH="$PATH:$HOME/oh/gitdir/depot_tools" echo 'export PATH="$PATH:$HOME/oh/gitdir/depot_tools"' >> ~/.bashrc 1.2 Get the code…

ARM_optee_kinibi基礎_

1.0 MMU关联常用简称 ref1: 【转】深入理解 TLB 原理 ref2: [mmu/cache]-ARM MMU的学习笔记-一篇就够了【转】 MMU: Memory Management Unit TLB: Translation lookaside buffer PGD: Page Global…