Embedded_game/qianrushi/makeQR/makeQR.py
2025-01-02 12:48:11 +08:00

130 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding:utf-8 -*-
# @Author len
# @Create 2023/10/16 19:58
import cv2
import qrcode
from PIL import Image
import random
import numpy as np
# 生成二维码图像
def makeQR():
# print(color_map)
width = 300 # 图像宽度
# height = 300 # 图像高度
data = "https://www.example.com" # 要编码的数据
qr = qrcode.QRCode()
qr.add_data(data)
qr.make(fit=True)
qr_img = qr.make_image()
# 转换为RGBA图像以便修改颜色
qr_img = qr_img.convert("RGBA")
# 创建自定义颜色映射
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)
}, {
(0, 0, 0, 255): (0, 128, 0, 255) # 将黑色替换为深绿 (R, G, B, Alpha)
}, {
(0, 0, 0, 255): (144, 238, 144, 255) # 将黑色替换为浅绿 (R, G, B, Alpha)
}, {
(0, 0, 0, 255): (0, 0, 139, 255) # 将黑色替换为深蓝 (R, G, B, Alpha)
}, {
(0, 0, 0, 255): (173, 216, 230, 255) # 将黑色替换为浅蓝 (R, G, B, Alpha)
}, {
(0, 0, 0, 255): (153, 51, 255, 255)
}, {
# 不变
}
]
color_map = random.choice(color_map_list)
# 遍历图像的每个像素,根据颜色映射替换颜色
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)
qr_img = qr_img.resize((width, width))
return qr_img
def rotate_image(image, angle):
image = np.array(image) # 转换为opencv
# 获取图像的尺寸和中心点
h, w = image.shape[:2]
center = (w // 2, h // 2)
# 获取旋转矩阵
M = cv2.getRotationMatrix2D(center=center, angle=-angle, scale=1.0)
# 进行仿射变换borderValue设置为白色255, 255, 255
rotated_image = cv2.warpAffine(src=image, M=M, dsize=(w, h), borderValue=(255, 255, 255))
return rotated_image
def makecanvas(count, number):
# 创建一个空白画布
canvas_width = 1200
canvas_height = 1200
background_color = (255, 255, 255) # 背景颜色(白色)
canvas = Image.new("RGB", (canvas_width, canvas_height), background_color)
images = []
for i in range(number):
image = makeQR()
# print()
images.append(image)
# 定义已放置图片的位置列表
placed_positions = []
# 设置旋转角度范围
min_rotation = 0
max_rotation = 360
# 在画布上放置图片
for image in images:
placed = False
while not placed:
# 随机生成位置
x = random.randint(0, canvas_width - image.width)
y = random.randint(0, canvas_height - image.height)
# 随机生成旋转角度
rotation_angle = random.randrange(min_rotation, max_rotation)
# 检查位置是否与已放置图片重叠
overlap = False
for position in placed_positions:
if (
x < position[0] + images[position[2]].width and
x + image.width > position[0] and
y < position[1] + images[position[2]].height and
y + image.height > position[1]
):
overlap = True
break
# 如果位置不重叠,则将图片粘贴到画布上,并更新已放置图片的位置列表
if not overlap:
# rotated_image = image.rotate(rotation_angle) # 对图片进行旋转
rotated_image = rotate_image(image, rotation_angle)
# 将NumPy数组转换为PIL图像对象
rotated_image = Image.fromarray(rotated_image)
canvas.paste(rotated_image, (x, y))
placed_positions.append((x, y, len(placed_positions)))
placed = True
# 显示画布
# canvas.show()
canvas.save(f'QRA4/{count}.jpg')
for i in range(30):
makecanvas(i, random.randint(3,5))