Skip to content

Commit

Permalink
Fix path error dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Belval authored May 9, 2020
1 parent a22fb33 commit df8db8a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 43 deletions.
4 changes: 1 addition & 3 deletions trdg/background_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ def image(height, width, image_dir):

if len(images) > 0:
pic = Image.open(
os.path.join(
image_dir, images[rnd.randint(0, len(images) - 1)]
)
os.path.join(image_dir, images[rnd.randint(0, len(images) - 1)])
)

if pic.size[0] < width:
Expand Down
27 changes: 22 additions & 5 deletions trdg/computer_text_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@


def generate(
text, font, text_color, font_size, orientation, space_width, character_spacing, fit, word_split
text,
font,
text_color,
font_size,
orientation,
space_width,
character_spacing,
fit,
word_split,
):
if orientation == 0:
return _generate_horizontal_text(
text, font, text_color, font_size, space_width, character_spacing, fit, word_split
text,
font,
text_color,
font_size,
space_width,
character_spacing,
fit,
word_split,
)
elif orientation == 1:
return _generate_vertical_text(
Expand All @@ -27,14 +42,16 @@ def _generate_horizontal_text(

if word_split:
splitted_text = []
for w in text.split(' '):
for w in text.split(" "):
splitted_text.append(w)
splitted_text.append(' ')
splitted_text.append(" ")
splitted_text.pop()
else:
splitted_text = text

piece_widths = [image_font.getsize(p)[0] if p != " " else space_width for p in splitted_text]
piece_widths = [
image_font.getsize(p)[0] if p != " " else space_width for p in splitted_text
]
text_width = sum(piece_widths)
if not word_split:
text_width += character_spacing * (len(text) - 1)
Expand Down
4 changes: 3 additions & 1 deletion trdg/generators/from_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def __init__(
fit=False,
output_mask=False,
word_split=False,
image_dir=os.path.join("..", os.path.split(os.path.realpath(__file__))[0], "images"),
image_dir=os.path.join(
"..", os.path.split(os.path.realpath(__file__))[0], "images"
),
):
self.count = count
self.length = length
Expand Down
4 changes: 3 additions & 1 deletion trdg/generators/from_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def __init__(
fit=False,
output_mask=False,
word_split=False,
image_dir=os.path.join("..", os.path.split(os.path.realpath(__file__))[0], "images"),
image_dir=os.path.join(
"..", os.path.split(os.path.realpath(__file__))[0], "images"
),
):
self.count = count
self.length = length
Expand Down
4 changes: 3 additions & 1 deletion trdg/generators/from_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def __init__(
fit=False,
output_mask=False,
word_split=False,
image_dir=os.path.join("..", os.path.split(os.path.realpath(__file__))[0], "images"),
image_dir=os.path.join(
"..", os.path.split(os.path.realpath(__file__))[0], "images"
),
):
self.count = count
self.strings = strings
Expand Down
4 changes: 3 additions & 1 deletion trdg/generators/from_wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def __init__(
fit=False,
output_mask=False,
word_split=False,
image_dir=os.path.join("..", os.path.split(os.path.realpath(__file__))[0], "images"),
image_dir=os.path.join(
"..", os.path.split(os.path.realpath(__file__))[0], "images"
),
):
self.count = count
self.minimum_length = minimum_length
Expand Down
38 changes: 10 additions & 28 deletions trdg/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def parse_arguments():
type=str,
nargs="?",
help="The language to use, should be fr (French), en (English), es (Spanish), de (German), ar (Arabic), cn (Chinese), or hi (Hindi)",
default="en"
default="en",
)
parser.add_argument(
"-c",
Expand Down Expand Up @@ -289,7 +289,7 @@ def parse_arguments():
type=str,
nargs="?",
help="Define an image directory to use when background is set to image",
default=os.path.join(os.path.split(os.path.realpath(__file__))[0], "images")
default=os.path.join(os.path.split(os.path.realpath(__file__))[0], "images"),
)
parser.add_argument(
"-ca",
Expand All @@ -302,36 +302,14 @@ def parse_arguments():
"-dt", "--dict", type=str, nargs="?", help="Define the dictionary to be used"
)
parser.add_argument(
"-ws", "--word_split",
"-ws",
"--word_split",
action="store_true",
help="Split on words instead of on characters (preserves ligatures, no character spacing)",
default=False,
)
return parser.parse_args()

def load_dict(lang):
"""
Read the dictionnary file and returns all words in it.
"""

lang_dict = []
with open(os.path.join('dicts', lang + '.txt'), 'r', encoding="utf8", errors='ignore') as d:
lang_dict = d.readlines()
return lang_dict

def load_fonts(lang):
"""
Load all fonts in the fonts directories
"""

if lang == 'ar':
return [os.path.join('fonts/ar', font) for font in os.listdir('fonts/ar')]
elif lang == 'cn':
return [os.path.join('fonts/cn', font) for font in os.listdir('fonts/cn')]
elif lang == 'hi':
return [os.path.join('fonts/hi', font) for font in os.listdir('fonts/hi')]
else:
return [os.path.join('fonts/latin', font) for font in os.listdir('fonts/latin')]

def main():
"""
Expand Down Expand Up @@ -403,10 +381,14 @@ def main():
args.length, args.random, args.count, lang_dict
)

if args.language == 'ar':
if args.language == "ar":
from arabic_reshaper import ArabicReshaper

arabic_reshaper = ArabicReshaper()
strings = [' '.join([arabic_reshaper.reshape(w) for w in s.split(' ')[::-1]]) for s in strings]
strings = [
" ".join([arabic_reshaper.reshape(w) for w in s.split(" ")[::-1]])
for s in strings
]
if args.case == "upper":
strings = [x.upper() for x in strings]
if args.case == "lower":
Expand Down
9 changes: 6 additions & 3 deletions trdg/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ def load_dict(lang):
lang_dict = [l for l in d.read().splitlines() if len(l) > 0]
return lang_dict


def load_fonts(lang):
"""Load all fonts in the fonts directories
"""

if lang == "cn":
if lang in ("ar", "cn", "hi"):
return [
os.path.join(os.path.dirname(__file__), "fonts/cn", font)
for font in os.listdir(os.path.join(os.path.dirname(__file__), "fonts/cn"))
os.path.join(os.path.dirname(__file__), "fonts/{}".format(lang), font)
for font in os.listdir(
os.path.join(os.path.dirname(__file__), "fonts/{}".format(lang))
)
]
else:
return [
Expand Down

0 comments on commit df8db8a

Please sign in to comment.