Python製做二維碼,而且能夠添加logo

import  readline
import qrcode
from PIL import Image
import os

def Create_Qrcode(strings,path,logo=""):
    qr = qrcode.QRCode(
        version=2,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=6,
        border=1,
    )

    with open('test.txt','r') as fr:
        for lines in fr.readlines():
            lines = lines.replace("\n","").strip()
            ID = lines
            lines = strings + lines
            if len(lines) > 0:
                qr.add_data(lines)
                qr.make(fit=True)
                img = qr.make_p_w_picpath()
                img = img.convert("RGBA")
                if os.path.exists(path) and os.path.isfile(logo):
                    icon = Image.open(logo)
                    img_w,img_h = img.size
                    factor = 5
                    size_w = int(img_w / factor)
                    size_h = int(img_h / factor)

                    icon_w, icon_h = icon.size
                    if icon_w > size_w:
                        icon_w = size_w
                    if icon_h > size_h:
                        icon_h = size_h
                    icon = icon.resize((icon_w,icon_h),Image.ANTIALIAS)
                    w = int((img_w - icon_w) / 2)
                    h = int((img_h - icon_h) / 2)
                    icon = icon.convert("RGBA")
                    img.paste(icon,(w,h),icon)

                    img.save( ID + '.jpg')

if __name__ == "__main__":
    Create_Qrcode('http://hepaidai.com/?channel_code=hpd&sub_id=','E:\PythonProject\\test','E:\PythonProject\\test\hpd.jpg')


說明:bash

須要安裝第三方庫:qrcode ,PIL , Image (推薦使用pip安裝)
strings: 二維碼字符串
path: 生成的二維碼保存路徑
logo: 要添加的logo文件


生成的二位碼:ide

wKioL1XMRTuj1WGlAABp7Oft7GI482.jpg

注意:建議把logo設置的小一點不然製做的二維碼不可以被識別
code

相關文章
相關標籤/搜索