Python生成带圆角图片的二维码

tech2022-10-24  106

示例代码1 #!/usr/bin/python # -*- coding: UTF-8 -*- # author: Carl time:2020/5/15 import qrcode from PIL import Image, ImageFile, ImageDraw ImageFile.LOAD_TRUNCATED_IMAGES = True # 设置中心图片四周圆角 def circle_crop_image(im): rad = 10 # 设置半径 circle = Image.new('L', (rad * 2, rad * 2), 0) draw = ImageDraw.Draw(circle) draw.ellipse((0, 0, rad * 2, rad * 2), fill=255) alpha = Image.new('L', im.size, 255) w, h = im.size alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0)) alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad)) alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0)) alpha.paste(circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad)) im.putalpha(alpha) return im # 生成二位码 def create_qr_code(url, file_name, icon_file=None): qr = qrcode.QRCode( version=5, # 设置容错率为最高 error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=6, border=4, ) qr.add_data(url) # 添加二维码内容,可以直接是字符串文本,也可url链接 qr.make(fit=True) img = qr.make_image(fill_color="#3ac25b", back_color="#FFF") # 设置二维码为彩色 img = img.convert("RGBA") if icon_file: icon = Image.open(icon_file) w, h = img.size factor = 4 size_w = int(w / factor) size_h = int(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((w - icon_w) / 2) h = int((h - icon_h) / 2) icon = icon.convert("RGBA") # ------以下是进一步处理中心图片,可省略------ # 白底图 white_img = Image.new("RGBA", (icon_w + 6, icon_h + 6), (255, 255, 255)) # 白底图圆角处理 # white_img = circle_crop_image(white_img) # 灰底图 gray_img = Image.new("RGBA", (icon_w + 2, icon_h + 2), (230, 230, 230)) # 灰底图圆角处理 gray_img = circle_crop_image(gray_img) # 粘贴灰底图 white_img.paste(gray_img, (2, 2)) # 粘贴白图 img.paste(white_img, (w - 2, h - 2)) # icon处理圆角 icon = circle_crop_image(icon) # ----------------以上---------------- # 粘贴icon img.paste(icon, (w + 1, h + 1)) img.show() img.save('' + file_name + '.png', quality=100) icon_path = "logo.jpg" # 中心图片 create_qr_code('http://www.baidu.com', "hnww", "logo.jpg") 示例代码2----中心图片周围有白边

# coding = utf-8 # 二维码生成 import qrcode from PIL import Image, ImageDraw # 设置圆角 def circle_crop_image(im, radii): # 创建一个黑色背景的画布 circle = Image.new('L', (radii * 2, radii * 2), 0) # 画黑色方形 draw = ImageDraw.Draw(circle) # 画白色圆形 draw.ellipse((0, 0, radii * 2, radii * 2), fill=255) # 把原图转换成RGBA模式,增加alpha通道 img = im.convert("RGBA") w, h = img.size # 画4个角(将整圆分离为4个部分)再粘贴到alpha通道 alpha = Image.new('L', img.size, 255) # 左上角 alpha.paste(circle.crop((0, 0, radii, radii)), (0, 0)) # 右上角 alpha.paste(circle.crop((radii, 0, radii * 2, radii)), (w - radii, 0)) # 右下角 alpha.paste(circle.crop((radii, radii, radii * 2, radii * 2)), (w - radii, h - radii)) # 左下角 alpha.paste(circle.crop((0, radii, radii, radii * 2)), (0, h - radii)) # 白色区域透明可见,黑色区域不可见 img.putalpha(alpha) return img # 生成二位码 def create_qr_code(url, icon_file, file_name, save_path): qr = qrcode.QRCode( version=5, # 设置容错率为最高 error_correction=qrcode.ERROR_CORRECT_H, box_size=6, border=4, ) qr.add_data(url) qr.make(fit=True) img = qr.make_image(fill_color="#3ac25b", back_color="#FFF") img = img.convert("RGBA") icon = Image.open(icon_file) # 把RGB的图转换成RGBA模式,处理alpha透明通道(后边替换透明为白色) icon = icon.convert("RGBA") w, h = img.size icon_w, icon_h = icon.size # 超过80的压缩到80 if icon_w > 80: icon = icon.resize((80, 80), Image.ANTIALIAS) icon_w, icon_h = icon.size w = int((w - 80) / 2) h = int((h - 80) / 2) else: w = int((w - icon_w) / 2) h = int((h - icon_h) / 2) # 把png背景色转换为白色,避免处理裁剪圆角时出现黑边 w_d = Image.new('RGBA', icon.size, (255, 255, 255)) w_d.paste(icon, (0, 0, icon_w, icon_h), icon) # r = icon_w // 15 r = 6 icon = circle_crop_image(w_d, r) # 白底图(尺寸大小) white_img = Image.new("RGBA", (icon_w+20, icon_h+20), (255, 255, 255)) white_img = circle_crop_image(white_img, r) # 灰底图 gray_img = Image.new("RGBA", (icon_w, icon_h), (230, 230, 230)) # 灰底图圆角处理 gray_img = circle_crop_image(gray_img, r) # 粘贴灰底图(起点坐标 = (白图长 - 灰图长) / 2) white_img.paste(gray_img, (10, 10), gray_img) # 粘贴白图(位置起点坐标) img.paste(white_img, (w-5, h-5), white_img) # 粘贴icon(起点坐标) img.paste(icon, (w + 5, h + 5), icon) save_file = save_path + file_name + '.png' img.save(save_file, quality=100) # img.show() return save_file if __name__ == '__main__': icon_path = 'logo.jpg' save_path = 'E:/qt_demo/' create_qr_code('http://www.zhousc.cn', icon_path, 'cs', save_path) 参考链接 QRCode二维码生成和识别qrcode库生成二维码时填充logo变成黑白色的解决Python 生成带Logo的圆角带边框二维码
最新回复(0)