# -*- coding:utf-8 -*- # @Author len # @Create 2023/10/18 11:08 from PIL import Image import qrcode # 创建一个空白图像,用于放置三个二维码 width = 800 # 图像宽度 height = 480 # 图像高度 background_color = (255, 255, 255) # 背景颜色(白色) image = Image.new("RGB", (width, height), background_color) # 生成三个二维码 data1 = "https://www.example.com" data2 = "https://www.google.com" data3 = "https://www.openai.com" qr1 = qrcode.QRCode() qr1.add_data(data1) qr1.make(fit=True) qr_img1 = qr1.make_image() qr2 = qrcode.QRCode() qr2.add_data(data2) qr2.make(fit=True) qr_img2 = qr2.make_image() qr3 = qrcode.QRCode() qr3.add_data(data3) qr3.make(fit=True) qr_img3 = qr3.make_image() qr4 = qrcode.QRCode() qr4.add_data(data3) qr4.make(fit=True) qr_img4 = qr4.make_image() # 将二维码放置在图像上 x_offset = 50 # 横向偏移量 y_offset = 50 # 纵向偏移量 qr_img1 = qr_img1.resize((200, 200)) qr_img2 = qr_img2.resize((200, 200)) qr_img3 = qr_img3.resize((200, 200)) qr_img4 = qr_img4.resize((200, 200)) color_map_list = [{ (0, 0, 0, 255): (255, 0, 0, 255) # 将黑色替换为红色 (R, G, B, Alpha) }, { (0, 0, 0, 255): (0, 255, 0, 255) # 将黑色替换为绿色 (R, G, B, Alpha) }, { (0, 0, 0, 255): (0, 0, 255, 255) # 将黑色替换为蓝色 (R, G, B, Alpha) }, { (0, 0, 0, 255): (255, 255, 0, 255) # 将黑色替换为黄色 (R, G, B, Alpha) }] color_map = color_map_list[1] # 遍历图像的每个像素,根据颜色映射替换颜色 new_img_data = [] for item in qr_img.getdata(): new_color = color_map.get(item[:4], item[:4]) new_img_data.append(new_color) # 更新图像数据 qr_img.putdata(new_img_data) image.paste(qr_img1, (200, 0)) image.paste(qr_img2, (400, 0)) image.paste(qr_img3, (200, 270)) image.paste(qr_img4, (400, 270)) # 显示图像 image.show()