qrcode生成带自己LOGO的二维码
库依赖
pip install qrcode
pip install Pillow
缘起
公司小程序活动要做到二维码分享里
代码
import io
import os
from PIL import Image
import qrcode
####存放中心logo
logo_path = '../media/xtz.jpg'
###生成二维码函数
def makeQRCode():
global logo_path
"""
参数说明:
version:图多大,1~40的整数,1时为一个21*21的矩阵
error_correction:可以允许二维码多少数据缺失时无法读取,
后缀为H的时候为百分之30,M为百分之5
"""
qr = qrcode.QRCode(
version=None,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
text = '"阶砖不会拒绝磨蚀 窗花不可幽禁落霞"'
###添加内容到二维码
qr.add_data(text)
"""
version为None;fit 为True可以根据内容多少自适应图片大小,
以下为官方说明
Set to None and use the fit parameter when making the code to determine this automatically.
"""
qr.make(fit=True)
##生成图片
img = qr.make_image(fill_color="white", back_color="black")
img.convert('RGBA')
####判断logo还在不在,避免报错
if os.path.exists(logo_path):
######希望logo图片在二维码的中心,并且宽高为二维码的五分之一
HIGHT,WIDTH = int(img.size[0]/5),int(img.size[1]/5)
logo = Image.open(logo_path)
logo = logo.resize((HIGHT,WIDTH),Image.ANTIALIAS).convert('RGBA')
img_center =(int((img.size[0]-WIDTH)/2),int((img.size[1]-WIDTH)/2))
###放到中心
img.paste(logo,img_center,logo)
img.show()
return
if __name__ == '__main__':
makeQRCode()
生成的二维码上传七牛
import io
import time
from urllib import parse
import qiniu
QINIU_HALF_TTL = (3600 * 24 * 180)
QINIU_SHORT_TTL = (3600 * 12)
static_url_prefix = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
qiniu_anti_leech_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
q = qiniu.Auth('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
logo_path = '../media/xtz.jpg'
def get_media_url(path, public=True, view_mode=None, **query):
###如果传入是空字符串,返回none
if path == '':
return None
if not isinstance(path, str):
return path
result = parse.urlsplit(path)
path = result.path
query_dict = parse.parse_qs(result.query)
query_dict.update(query)
if public:
half_window = QINIU_HALF_TTL
deadline = int((time.time() // half_window + 2) * half_window)
else:
deadline = int(time.time() + QINIU_SHORT_TTL)
url = qiniu.create_timestamp_anti_leech_url(
static_url_prefix, path.lstrip('/'), query_dict,
qiniu_anti_leech_key, deadline)
if view_mode is not None:
url = '{}&{}'.format(url, view_mode)
return url
def makeQRCode():
import qrcode
global logo_path
output = io.BytesIO()
qr = qrcode.QRCode(
version=None,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
text = '"阶砖不会拒绝磨蚀 窗花不可幽禁落霞"'
qr.add_data(text)
qr.make(fit=True)
img = qr.make_image(fill_color="white", back_color="black")
img.convert('RGBA')
######希望logo图片在二维码的中心,并且宽高为二维码的五分之一
if os.path.exists(logo_path):
HIGHT,WIDTH = int(img.size[0]/5),int(img.size[1]/5)
logo = Image.open(logo_path)
logo = logo.resize((HIGHT,WIDTH),Image.ANTIALIAS).convert('RGBA')
img_center =(int((img.size[0]-WIDTH)/2),int((img.size[1]-WIDTH)/2))
img.paste(logo,img_center,logo)
img.save(output)
output.seek(0)
url = upload(output,'operate')
return url
def upload(img,model):
key = 'xxx.JPGE'
token = q.upload_token('xxx', key, 3600, )
qiniu.put_data(token, key, img)
url = 'https://xxx.xxx.xx/{}'.format(key)
readable_url = get_media_url(url)
return readable_url
if __name__ == '__main__':
print(makeQRCode())
以上