-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcankao.py
77 lines (54 loc) · 2.32 KB
/
cankao.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import csv
import qrcode
import os
import re
from PIL import Image
def clean_filename(filename):
"""
Clean and sanitize the filename.
"""
# Remove special characters and spaces, replace them with underscores
filename = re.sub(r'[\\/*?:"<>| ]', "_", filename)
return filename
def generate_qr_codes_with_colored_icon(csv_file_path, output_folder):
# Create output folder if it doesn't exist
if not os.path.exists(output_folder):
os.makedirs(output_folder)
icon = Image.open('your_image.png') # Load the colored icon image
with open(csv_file_path, mode='r', encoding='utf-8-sig') as file:
reader = csv.DictReader(file)
counter = 1 # Initialize a counter
for row in reader:
name = clean_filename(row['name'])
url = row['url']
qr = qrcode.QRCode(
version=8,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=3,
)
qr.add_data(url)
qr.make(fit=True)
# ... [previous code] ...
# ... [之前的代码] ...
# 将 QR 码图像转换为 RGBA 模式以便与图标兼容
qr_image = qr.make_image(fill_color="black", back_color="white").convert('RGBA')
# 计算放置图标的中心位置
qr_size = qr_image.size[0]
icon_size = qr_image.size[0] # 根据需要调整图标大小
x = (qr_size - icon_size) // 2
y = (qr_size - icon_size) // 2
# 调整图标大小并确保其为 RGBA 模式
resized_icon = icon.resize((icon_size, icon_size)).convert('RGBA')
# 将调整后的图标粘贴到 QR 码上
qr_image.paste(resized_icon, (x, y), mask=resized_icon)
# 如有需要,将最终图像转换为 CMYK 模式
final_image = qr_image.convert('CMYK')
# ... [rest of the code] ...
# Add a sequential number to the filename
filename_with_number = f"[{counter}] {name}.jpg"
png_file_path = os.path.join(output_folder, filename_with_number)
final_image.save(png_file_path)
counter += 1 # Increment the counter
# 使用示例
generate_qr_codes_with_colored_icon('csvfile.csv', 'output/')