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

105 lines
3.0 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 = 200 # 图像宽度
# 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 = 800
canvas_height = 480
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)
# 设置旋转角度范围
min_rotation = 0
max_rotation = 360
xy = [(200, 0), (400,0), (200, 270), (400, 270)]
c = 0
# 在画布上放置图片
for image in images:
(x, y) = xy[c]
c += 1
canvas.paste(image, (x, y))
# 显示画布
# canvas.show()
canvas.save(f'123/{count}.jpg')
for i in range(30):
makecanvas(i, 4)