Skip to content

Commit df8db8a

Browse files
authored
Fix path error dicts
1 parent a22fb33 commit df8db8a

8 files changed

Lines changed: 51 additions & 43 deletions

File tree

trdg/background_generator.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ def image(height, width, image_dir):
6363

6464
if len(images) > 0:
6565
pic = Image.open(
66-
os.path.join(
67-
image_dir, images[rnd.randint(0, len(images) - 1)]
68-
)
66+
os.path.join(image_dir, images[rnd.randint(0, len(images) - 1)])
6967
)
7068

7169
if pic.size[0] < width:

trdg/computer_text_generator.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,26 @@
44

55

66
def generate(
7-
text, font, text_color, font_size, orientation, space_width, character_spacing, fit, word_split
7+
text,
8+
font,
9+
text_color,
10+
font_size,
11+
orientation,
12+
space_width,
13+
character_spacing,
14+
fit,
15+
word_split,
816
):
917
if orientation == 0:
1018
return _generate_horizontal_text(
11-
text, font, text_color, font_size, space_width, character_spacing, fit, word_split
19+
text,
20+
font,
21+
text_color,
22+
font_size,
23+
space_width,
24+
character_spacing,
25+
fit,
26+
word_split,
1227
)
1328
elif orientation == 1:
1429
return _generate_vertical_text(
@@ -27,14 +42,16 @@ def _generate_horizontal_text(
2742

2843
if word_split:
2944
splitted_text = []
30-
for w in text.split(' '):
45+
for w in text.split(" "):
3146
splitted_text.append(w)
32-
splitted_text.append(' ')
47+
splitted_text.append(" ")
3348
splitted_text.pop()
3449
else:
3550
splitted_text = text
3651

37-
piece_widths = [image_font.getsize(p)[0] if p != " " else space_width for p in splitted_text]
52+
piece_widths = [
53+
image_font.getsize(p)[0] if p != " " else space_width for p in splitted_text
54+
]
3855
text_width = sum(piece_widths)
3956
if not word_split:
4057
text_width += character_spacing * (len(text) - 1)

trdg/generators/from_dict.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def __init__(
3535
fit=False,
3636
output_mask=False,
3737
word_split=False,
38-
image_dir=os.path.join("..", os.path.split(os.path.realpath(__file__))[0], "images"),
38+
image_dir=os.path.join(
39+
"..", os.path.split(os.path.realpath(__file__))[0], "images"
40+
),
3941
):
4042
self.count = count
4143
self.length = length

trdg/generators/from_random.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ def __init__(
3838
fit=False,
3939
output_mask=False,
4040
word_split=False,
41-
image_dir=os.path.join("..", os.path.split(os.path.realpath(__file__))[0], "images"),
41+
image_dir=os.path.join(
42+
"..", os.path.split(os.path.realpath(__file__))[0], "images"
43+
),
4244
):
4345
self.count = count
4446
self.length = length

trdg/generators/from_strings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ def __init__(
3232
fit=False,
3333
output_mask=False,
3434
word_split=False,
35-
image_dir=os.path.join("..", os.path.split(os.path.realpath(__file__))[0], "images"),
35+
image_dir=os.path.join(
36+
"..", os.path.split(os.path.realpath(__file__))[0], "images"
37+
),
3638
):
3739
self.count = count
3840
self.strings = strings

trdg/generators/from_wikipedia.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def __init__(
3434
fit=False,
3535
output_mask=False,
3636
word_split=False,
37-
image_dir=os.path.join("..", os.path.split(os.path.realpath(__file__))[0], "images"),
37+
image_dir=os.path.join(
38+
"..", os.path.split(os.path.realpath(__file__))[0], "images"
39+
),
3840
):
3941
self.count = count
4042
self.minimum_length = minimum_length

trdg/run.py

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def parse_arguments():
5252
type=str,
5353
nargs="?",
5454
help="The language to use, should be fr (French), en (English), es (Spanish), de (German), ar (Arabic), cn (Chinese), or hi (Hindi)",
55-
default="en"
55+
default="en",
5656
)
5757
parser.add_argument(
5858
"-c",
@@ -289,7 +289,7 @@ def parse_arguments():
289289
type=str,
290290
nargs="?",
291291
help="Define an image directory to use when background is set to image",
292-
default=os.path.join(os.path.split(os.path.realpath(__file__))[0], "images")
292+
default=os.path.join(os.path.split(os.path.realpath(__file__))[0], "images"),
293293
)
294294
parser.add_argument(
295295
"-ca",
@@ -302,36 +302,14 @@ def parse_arguments():
302302
"-dt", "--dict", type=str, nargs="?", help="Define the dictionary to be used"
303303
)
304304
parser.add_argument(
305-
"-ws", "--word_split",
305+
"-ws",
306+
"--word_split",
306307
action="store_true",
307308
help="Split on words instead of on characters (preserves ligatures, no character spacing)",
308309
default=False,
309310
)
310311
return parser.parse_args()
311312

312-
def load_dict(lang):
313-
"""
314-
Read the dictionnary file and returns all words in it.
315-
"""
316-
317-
lang_dict = []
318-
with open(os.path.join('dicts', lang + '.txt'), 'r', encoding="utf8", errors='ignore') as d:
319-
lang_dict = d.readlines()
320-
return lang_dict
321-
322-
def load_fonts(lang):
323-
"""
324-
Load all fonts in the fonts directories
325-
"""
326-
327-
if lang == 'ar':
328-
return [os.path.join('fonts/ar', font) for font in os.listdir('fonts/ar')]
329-
elif lang == 'cn':
330-
return [os.path.join('fonts/cn', font) for font in os.listdir('fonts/cn')]
331-
elif lang == 'hi':
332-
return [os.path.join('fonts/hi', font) for font in os.listdir('fonts/hi')]
333-
else:
334-
return [os.path.join('fonts/latin', font) for font in os.listdir('fonts/latin')]
335313

336314
def main():
337315
"""
@@ -403,10 +381,14 @@ def main():
403381
args.length, args.random, args.count, lang_dict
404382
)
405383

406-
if args.language == 'ar':
384+
if args.language == "ar":
407385
from arabic_reshaper import ArabicReshaper
386+
408387
arabic_reshaper = ArabicReshaper()
409-
strings = [' '.join([arabic_reshaper.reshape(w) for w in s.split(' ')[::-1]]) for s in strings]
388+
strings = [
389+
" ".join([arabic_reshaper.reshape(w) for w in s.split(" ")[::-1]])
390+
for s in strings
391+
]
410392
if args.case == "upper":
411393
strings = [x.upper() for x in strings]
412394
if args.case == "lower":

trdg/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ def load_dict(lang):
1919
lang_dict = [l for l in d.read().splitlines() if len(l) > 0]
2020
return lang_dict
2121

22+
2223
def load_fonts(lang):
2324
"""Load all fonts in the fonts directories
2425
"""
2526

26-
if lang == "cn":
27+
if lang in ("ar", "cn", "hi"):
2728
return [
28-
os.path.join(os.path.dirname(__file__), "fonts/cn", font)
29-
for font in os.listdir(os.path.join(os.path.dirname(__file__), "fonts/cn"))
29+
os.path.join(os.path.dirname(__file__), "fonts/{}".format(lang), font)
30+
for font in os.listdir(
31+
os.path.join(os.path.dirname(__file__), "fonts/{}".format(lang))
32+
)
3033
]
3134
else:
3235
return [

0 commit comments

Comments
 (0)