-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.py
70 lines (53 loc) · 2.67 KB
/
text.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
from PIL import Image, ImageFont, ImageDraw
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
FONT_COLOR = "#000000"
def generate_certificate_for_name(name, template_file, output_dir, vertical_offset, horizontal_offset, font_size, font_path):
name = str(name).strip().upper() # Change all names to capital letters and remove extra spaces
if not name:
print("Skipped empty name")
return
template = Image.open(template_file)
width, height = template.size
image_source = template.copy()
draw = ImageDraw.Draw(image_source)
font = ImageFont.truetype(font_path, font_size)
text_bbox = draw.textbbox((0, 0), name, font=font)
text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
# Apply the user-defined vertical and horizontal offsets
draw.text(((width - text_width) / 2 + horizontal_offset, (height - text_height) / 2 + vertical_offset), name,
fill=FONT_COLOR, font=font)
output_file = os.path.join(output_dir, f"{name}.png")
try:
image_source.save(output_file)
print('Saving Certificate for:', name)
except Exception as e:
print(f"Error saving {output_file}: {e}")
def make_certificates_txt(file_path, template_file, output_dir, vertical_offset, horizontal_offset, font_size, font_path):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
if not os.path.exists(template_file):
print(f"Template file not found: {template_file}")
return
with open(file_path, "r") as file:
names = file.read().splitlines()
# Dynamic thread allocation based on the number of available CPUs
num_threads = os.cpu_count() or 1 # Defaults to 1 if os.cpu_count() returns None
print("num_threads",num_threads)
with ThreadPoolExecutor(max_workers=num_threads) as executor:
futures = [executor.submit(generate_certificate_for_name, name, template_file, output_dir, vertical_offset, horizontal_offset, font_size, font_path) for name in names]
for future in as_completed(futures):
future.result() # To capture exceptions, if any
# Delete the template file after processing all names
if os.path.exists(template_file):
os.remove(template_file)
print(f"Deleted template file: {template_file}")
else:
print(f"Template file already deleted or not found: {template_file}")
# Delete the temp_file.txt after processing
temp_file = "temp_file.txt"
if os.path.exists(temp_file):
os.remove(temp_file)
print(f"Deleted temporary file: {temp_file}")
else:
print(f"Temporary file already deleted or not found: {temp_file}")