-
Notifications
You must be signed in to change notification settings - Fork 5
/
helper.py
180 lines (141 loc) · 6.06 KB
/
helper.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import csv
from PIL import Image, ImageEnhance
import os
def split_string(string, max_chars_per_line):
words = string.split()
lines = []
current_line = ""
for word in words:
if len(current_line + " " + word) > max_chars_per_line:
lines.append(current_line.strip())
current_line = ""
current_line += " " + word
if current_line:
lines.append(current_line.strip())
# Re-combine lines to achieve even distribution of words
num_lines = len(lines)
if num_lines > 1:
total_words = len(words)
ideal_words_per_line = (total_words + num_lines - 1) // num_lines
excess_words = total_words - ideal_words_per_line * (num_lines - 1)
even_lines = []
i = 0
while i < num_lines - 1:
line_words = words[:ideal_words_per_line]
if excess_words > 0:
line_words.append(words[ideal_words_per_line])
excess_words -= 1
words.pop(ideal_words_per_line)
even_lines.append(" ".join(line_words))
words = words[ideal_words_per_line:]
i += 1
even_lines.append(" ".join(words))
return "\n".join(even_lines)
else:
return lines[0]
def darken_images(images_folder, output_folder):
# Set desired darkness
dark = 0.5
# Loop through all the images in the directory
for filename in os.listdir(images_folder):
if filename.endswith(".jpg") or filename.endswith(".png"):
# Open the image
filepath = os.path.join(images_folder, filename)
img = Image.open(filepath)
# Create an enhancer object for the image
enhancer = ImageEnhance.Brightness(img)
# Reduce the brightness by a factor of 'dark'
dark_img = enhancer.enhance(dark)
# Save the cropped image
dark_img.save(f"{output_folder}/{filename}")
def cut_images_old(images_folder, output_folder):
# Set the target size
target_size = (1080, 1350)
# Loop through all the images in the directory
for filename in os.listdir(images_folder):
if filename.endswith(".jpg") or filename.endswith(".png"):
# Open the image
filepath = os.path.join(images_folder, filename)
img = Image.open(filepath)
resize_ration = min(target_size[0] / img.width, target_size[1] / img.height)
# Get the size of the image
width, height = img.size
# Calculate the coordinates for cropping
left = (width - target_size[0]) // 2
top = (height - target_size[1]) // 2
right = left + target_size[0]
bottom = top + target_size[1]
# Crop the image
img = img.crop((left, top, right, bottom))
# Save the cropped image
img.save(f"{output_folder}/{filename}")
def cut_images(images_folder, output_folder):
# Set desired ratio
desired_ratio = 1080 / 1350
# Loop through all files in input folder
for filename in os.listdir(images_folder):
if filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.png'):
# Open the image
img = Image.open(os.path.join(images_folder, filename))
# Get image dimensions
width, height = img.size
ratio = width / height
# Calculate new dimensions
if ratio > desired_ratio:
# Image is wider than desired ratio, crop width
new_width = round(height * desired_ratio)
new_height = height
else:
# Image is taller than desired ratio, crop height
new_width = width
new_height = round(width / desired_ratio)
# Crop the image in the center
left = (width - new_width) / 2
top = (height - new_height) / 2
right = left + new_width
bottom = top + new_height
img = img.crop((left, top, right, bottom))
# Resize the image if necessary
if img.size != (1080, 1350):
img = img.resize((1080, 1350))
# Save the image to output folder
img.save(os.path.join(output_folder, filename))
def create_new_topic_dirs(topic, project_dir):
# /customers/___
if not os.path.exists(f"{project_dir}/customers/{topic}"):
os.makedirs(f"{project_dir}/customers/{topic}")
# /sources/images/___
if not os.path.exists(f"{project_dir}/sources/images/{topic}"):
os.makedirs(f"{project_dir}/sources/images/{topic}")
os.makedirs(f"{project_dir}/sources/images/{topic}/cropped")
os.makedirs(f"{project_dir}/sources/images/{topic}/cropped/darken")
def fix_text_syntax(font: str, text_file):
with open(text_file, 'r', encoding="utf-8") as file:
lines = file.readlines()
# Bebas can't display ’ -> replace with '
if font.__contains__("Bebas"):
# open file in read mode
file = open(text_file, "r")
replaced_content = ""
# looping through the file
for line in file:
# stripping line break
line = line.strip()
# replacing the texts
new_line = line.replace("’", "'").replace("’", "'")
# concatenate the new string and add an end-line break
replaced_content = replaced_content + new_line + "\n"
# close the file
file.close()
# Open file in write mode
write_file = open(text_file, "w")
# overwriting the old file contents with the new/replaced content
write_file.write(replaced_content)
# close the file
write_file.close()
def add_sheets(file_names: str, output_path: str, customer_name: str, authors: str, quotes: str):
with open(f'{output_path}/{customer_name}.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["File Name", "Reference", "Verse"])
for i in range(len(file_names)):
writer.writerow([file_names[i], authors[i], quotes[i]])