Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions trdg/data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def generate(
cls,
index,
text,
language,
font,
out_dir,
size,
Expand Down Expand Up @@ -244,9 +245,17 @@ def generate(
#####################################
# Generate name for resulting image #
#####################################
if language == "ar" or language == "fa":
from arabic_reshaper import ArabicReshaper
from bidi.algorithm import get_display

arabic_reshaper = ArabicReshaper()
text = " ".join([get_display(arabic_reshaper.reshape(w)) for w in text.split(" ")[::-1]])

# We remove spaces if space_width == 0
if space_width == 0:
text = text.replace(" ", "")

if name_format == 0:
image_name = "{}_{}.{}".format(text, str(index), extension)
mask_name = "{}_{}_mask.png".format(text, str(index))
Expand Down
Binary file added trdg/fonts/fa/BNAZANIN.TTF
Binary file not shown.
11 changes: 9 additions & 2 deletions trdg/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def main():
args.length, args.random, args.count, lang_dict
)

if args.language == "ar":
if args.language == "ar" or args.language == "fa":
from arabic_reshaper import ArabicReshaper
from bidi.algorithm import get_display

Expand All @@ -428,6 +428,7 @@ def main():
zip(
[i for i in range(0, string_count)],
strings,
[args.language] * string_count,
[fonts[rnd.randrange(0, len(fonts))] for _ in range(0, string_count)],
[args.output_dir] * string_count,
[args.format] * string_count,
Expand Down Expand Up @@ -471,7 +472,13 @@ def main():
file_name = str(i) + "." + args.extension
if args.space_width == 0:
file_name = file_name.replace(" ", "")
f.write("{} {}\n".format(file_name, strings[i]))

if args.language == "ar" or args.language == "fa":
sentence = " ".join([get_display(arabic_reshaper.reshape(w)) for w in strings[i].split(" ")[::-1]])
else:
sentence = strings[i]

f.write("{} {}\n".format(file_name, sentence))


if __name__ == "__main__":
Expand Down