二维码(带有图片)的生成
二维码(带有图片)的生成 - javaobject - 博客园 https://www.cnblogs.com/a8457013/p/7239722.html
import qrcode qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, ) str='https://www.toutiao.com/c/user/4073823366/' qr.add_data(str) qr.make(fit=True) img = qr.make_image() img.save('123.png')
from PIL import Image, ImageDraw, ImageFont import qrcode url_code='https://www.toutiao.com/c/user/6685910660/' url_code='https://www.toutiao.com/c/user/6631414881/' cav_img_name = 'favicon.png' qr = qrcode.QRCode(version=5, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=50, border=3) qr.add_data(url_code) qr.make(fit=True) img = qr.make_image() img = img.convert("RGBA") # 二维码中心logo logo='favicon.png' logo='1.png' mid_icon = Image.open(logo) logo_w,logo_h=mid_icon.size img_w, img_h = img.size factor =3 size_w = int(img_w / factor) size_h = int(img_h / factor) icon_w, icon_h = mid_icon.size if icon_w > size_w: icon_w = size_w if icon_h > size_h: icon_h = size_h mid_icon = mid_icon.resize((icon_w*5, icon_h*5), Image.ANTIALIAS) w = int((img_w - icon_w) / 2) h = int((img_h - icon_h) / 2) mid_icon = mid_icon.convert("RGBA") img.paste(mid_icon, (w, h), mid_icon) img.save( 'createlogo.png')
import sys import os curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(curPath)[0] sys.path.append(rootPath) filepath = 'toutiao_dl_img' requested_file_list = [] pathDir = os.listdir(filepath) for allDir in pathDir: child = os.path.join('%s%s' % (filepath, allDir)) requested_file = child.split('img')[-1].split('.')[0] requested_file_list.append(requested_file) from PIL import Image, ImageDraw, ImageFont import qrcode import time for i in requested_file_list: url_code = 'https://www.toutiao.com/c/user/UID/'.replace('UID',i) # border 白边 qr_content_ize, qr_border = 50, 2 qr_boxsize = qr_content_ize + 2 * qr_border qr = qrcode.QRCode(version=5, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=qr_boxsize, border=qr_border) qr.add_data(url_code) qr.make(fit=True) img = qr.make_image() img = img.convert("RGBA") img_w, img_h = img.size # 二维码中心logo 头条头像 120*120 logo = '{}{}{}{}'.format('toutiao_dl_img/','toutiao_uid_head_img', i, '.png') mid_icon = Image.open(logo) mid_icon_w, mid_icon_h = mid_icon.size mid_factor = 5 mid_icon_w, mid_icon_h = mid_icon_w * mid_factor, mid_icon_h * mid_factor mid_icon = mid_icon.resize((mid_icon_w, mid_icon_h), Image.ANTIALIAS) mid_icon = mid_icon.convert("RGBA") # x,y 左顶点的坐标 top_left_x, top_left_y = int(img_w / 2 - mid_icon_w / 2), int(img_h / 2 - mid_icon_h / 2) img.paste(mid_icon, (top_left_x, top_left_y), mid_icon) localtime_ = time.strftime("%y%m%d%H%M%S", time.localtime()) s = '{}{}{}{}{}{}'.format('toutiao_qr_head_img/', 'toutiao_qr_head_img', localtime_, '_', i, '.png') img.save(s)
调整二维码,大小
import sys import os curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(curPath)[0] sys.path.append(rootPath) filepath = 'toutiao_dl_img' requested_file_list = [] pathDir = os.listdir(filepath) for allDir in pathDir: child = os.path.join('%s%s' % (filepath, allDir)) requested_file = child.split('img')[-1].split('.')[0] requested_file_list.append(requested_file) from PIL import Image, ImageDraw, ImageFont import qrcode import time for i in requested_file_list: url_code = 'https://www.toutiao.com/c/user/UID/'.replace('UID', i) # border 白边 # 2018年3月26日 15:37:46 qr_content_ize, qr_border = 50, 2 # qr_content_ize, qr_border = 25, 1 qr_boxsize = qr_content_ize + 2 * qr_border qr = qrcode.QRCode(version=5, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=qr_boxsize, border=qr_border) qr.add_data(url_code) qr.make(fit=True) img = qr.make_image() img = img.convert("RGBA") img_w, img_h = img.size # 二维码中心logo 头条头像 120*120 logo = '{}{}{}{}'.format('toutiao_dl_img/', 'toutiao_uid_head_img', i, '.png') mid_icon = Image.open(logo) mid_icon_w, mid_icon_h = mid_icon.size mid_factor = 5 mid_icon_w, mid_icon_h = mid_icon_w * mid_factor, mid_icon_h * mid_factor mid_icon = mid_icon.resize((mid_icon_w, mid_icon_h), Image.ANTIALIAS) mid_icon = mid_icon.convert("RGBA") # x,y 左顶点的坐标 top_left_x, top_left_y = int(img_w / 2 - mid_icon_w / 2), int(img_h / 2 - mid_icon_h / 2) img.paste(mid_icon, (top_left_x, top_left_y), mid_icon) localtime_ = time.strftime("%y%m%d%H%M%S", time.localtime()) s = '{}{}{}{}{}{}'.format('toutiao_qr_head_img/', 'toutiao_qr_head_img', localtime_, '_', i, '.png') # 2018年3月26日 15:40:56 img_factor = 5 img_w, img_h = int(img_w / img_factor), int(img_h / img_factor) img = img.resize((img_w, img_h), Image.ANTIALIAS) img.save(s) d = 9
图片缩放
维持数据库文件名不变,覆盖文件
import sys import os from PIL import Image, ImageDraw, ImageFont curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(curPath)[0] sys.path.append(rootPath) # filepath = 'toutiao_dl_img' filepath = 'toutiao_qr_head_img0315-文件-大图 - resize' requested_file_list = [] pathDir = os.listdir(filepath) for allDir in pathDir: child = os.path.join('%s%s' % (filepath, allDir)) f = '{}/{}'.format(filepath, allDir) img = Image.open(f) img_w, img_h = img.size img_factor = 5 img_w, img_h = int(img_w / img_factor), int(img_h / img_factor) img = img.resize((img_w, img_h), Image.ANTIALIAS) img.save(f)