Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,7 @@ ENV/
.mypy_cache/

.vscode/*
/.vs
/out
/tests
/trdg/dicts/ar - Copy.txt
16 changes: 16 additions & 0 deletions basic_py_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

from trdg.generators import (
#GeneratorFromDict,
#GeneratorFromRandom,
GeneratorFromStrings,
#GeneratorFromWikipedia,
)

texts = ['تست', 'تست2', 'تست 3']

# The generators use the same arguments as the CLI, only as parameters
generator = GeneratorFromStrings(texts, count=3, blur=2, random_blur=True, language='ar')

for img, lbl in generator:
# Do something with the pillow images here.
img.save('tests/out_3/' + lbl + '.png')
1 change: 1 addition & 0 deletions run_example_windows.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python trdg\run.py -f 45 -c 20 -w 3 -r -bl 1 -rbl -l "fa" -e "png" --background 2 --name_format 0 --stroke_width 1 --skew_angle 2 -rk --distorsion 3
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