-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_data.py
More file actions
121 lines (98 loc) · 3.65 KB
/
Copy pathencode_data.py
File metadata and controls
121 lines (98 loc) · 3.65 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from PIL import Image
import qrcode
import math
path = "./imginput.jpg"
general_info = f"{path};{path.split('.')[-1]}"
print(general_info)
# Read data
data = open(path, "rb").read()
# --- Create QR codes from chunks ---
chunk_size = 750 # bytes per QR
qr_codes = []
data_chunks = []
total = math.ceil(len(data)/chunk_size)
for i in range(total):
chunk = data[i*chunk_size:(i+1)*chunk_size]
chunk_str = chunk.decode("latin-1")
data_chunks.append(chunk_str)
payload = f"{i+1}/{total+1}|{chunk_str}"
qr = qrcode.QRCode(
version=None, # automatic
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=1
)
qr.add_data(payload, optimize=0)
qr.make(fit=True)
qr_img = qr.make_image(fill_color="black", back_color="white").convert("RGB")
qr_codes.append(qr_img)
# --- SETTINGS ---
page_width_mm = 210
page_height_mm = 297
dpi = 300 # printing DPI
margin_mm = 1
spacing_mm = 1
images_per_row = 3
images_per_column = 4 # odd for center
def mm_to_px(mm, dpi):
return int(mm * dpi / 25.4)
page_width_px = mm_to_px(page_width_mm, dpi)
page_height_px = mm_to_px(page_height_mm, dpi)
margin_px = mm_to_px(margin_mm, dpi)
spacing_px = mm_to_px(spacing_mm, dpi)
# Custom center QR code
center_qr = qrcode.QRCode(
version=None,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=2
)
# Center
center_qr.add_data(f"0/{total+1}|" + general_info)
center_qr.make(fit=True)
center_image = center_qr.make_image(fill_color="black", back_color="white").convert("RGB")
# Calculate max size per cell
grid_width_px = page_width_px - 2*margin_px - (images_per_row-1)*spacing_px
grid_height_px = page_height_px - 2*margin_px - (images_per_column-1)*spacing_px
cell_width_px = grid_width_px // images_per_row
cell_height_px = grid_height_px // images_per_column
# Pagination
pages = []
images_per_page = images_per_row * images_per_column - 1 # leave center free
num_pages = math.ceil(len(qr_codes) / images_per_page)
for page_idx in range(num_pages):
page = Image.new("RGB", (page_width_px, page_height_px), color="white")
start_idx = page_idx * images_per_page
end_idx = start_idx + images_per_page
page_images = qr_codes[start_idx:end_idx]
img_counter = 0
for row in range(images_per_column):
for col in range(images_per_row):
# Center cell
if row == images_per_column//2 and col == images_per_row//2:
img = center_image
else:
if img_counter >= len(page_images):
continue
img = page_images[img_counter]
img_counter += 1
# Resize to fit cell
img_ratio = img.width / img.height
cell_ratio = cell_width_px / cell_height_px
if img_ratio > cell_ratio:
new_width = cell_width_px
new_height = int(cell_width_px / img_ratio)
else:
new_height = cell_height_px
new_width = int(cell_height_px * img_ratio)
img_resized = img.resize((new_width, new_height), Image.LANCZOS)
# Position
x = margin_px + col * (cell_width_px + spacing_px) + (cell_width_px - new_width)//2
y = margin_px + row * (cell_height_px + spacing_px) + (cell_height_px - new_height)//2
page.paste(img_resized, (x, y))
pages.append(page)
# Show first page for testing
# pages[0].save("./qr_page.png")
# Export to PDF
pages[0].save("qr_pages.png", save_all=True, append_images=pages[1:])
print(f"Generated {num_pages} page(s) with center QR code.")