Skip to content

Commit

Permalink
Version 1.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Belval authored Dec 5, 2019
1 parent 565bce1 commit 01329e0
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 60 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ The path (`/output/path/`) must be absolute.
## New
- Add `--character_spacing` to control space between characters (in pixels)
- Add python module
- Move `run.py` to an executable python file ([`trdg/bin/trdg`](trdg/bin/trdg))
- Add `--font` to use only one font for all the generated images (Thank you @JulienCoutault!)
- Add `--fit` and `--margins` for finer layout control
- Change the text orientation using the `-or` parameter
Expand Down Expand Up @@ -174,7 +173,7 @@ If you want to add a new non-latin language, the amount of work is minimal.

1. Create a new folder with your language [two-letters code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
2. Add a .ttf font in it
3. Edit `bin/trdg` to add an if statement in `load_fonts()`
3. Edit `run.py` to add an if statement in `load_fonts()`
4. Add a text file in `dicts` with the same two-letters code
5. Run the tool as you normally would but add `-l` with your two-letters code

Expand Down
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name="trdg",
version="1.3.1",
version="1.3.2",
description="TextRecognitionDataGenerator: A synthetic data generator for text recognition",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down Expand Up @@ -48,5 +48,9 @@
"tqdm>=4.23.0",
"beautifulsoup4>=4.6.0"
],
scripts=["trdg/bin/trdg"],
entry_points={
"console_scripts": [
"trdg=trdg.run:main"
],
},
)
26 changes: 13 additions & 13 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,49 +683,49 @@ def test_generate_data_with_quasicrystal_background(self):

class CommandLineInterface(unittest.TestCase):
def test_output_dir(self):
args = ["./bin/trdg", "-c", "1", "--output_dir", "../tests/out_2/"]
args = ["python3", "run.py", "-c", "1", "--output_dir", "../tests/out_2/"]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out_2/")) == 1)
empty_directory("tests/out_2/")

def test_language_english(self):
args = ["./bin/trdg", "-l", "en", "-c", "1", "--output_dir", "../tests/out/"]
args = ["python3", "run.py", "-l", "en", "-c", "1", "--output_dir", "../tests/out/"]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")

def test_language_french(self):
args = ["./bin/trdg", "-l", "fr", "-c", "1", "--output_dir", "../tests/out/"]
args = ["python3", "run.py", "-l", "fr", "-c", "1", "--output_dir", "../tests/out/"]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")

def test_language_spanish(self):
args = ["./bin/trdg", "-l", "es", "-c", "1", "--output_dir", "../tests/out/"]
args = ["python3", "run.py", "-l", "es", "-c", "1", "--output_dir", "../tests/out/"]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")

def test_language_german(self):
args = ["./bin/trdg", "-l", "de", "-c", "1", "--output_dir", "../tests/out/"]
args = ["python3", "run.py", "-l", "de", "-c", "1", "--output_dir", "../tests/out/"]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")

def test_language_chinese(self):
args = ["./bin/trdg", "-l", "cn", "-c", "1", "--output_dir", "../tests/out/"]
args = ["python3", "run.py", "-l", "cn", "-c", "1", "--output_dir", "../tests/out/"]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")

def test_count_parameter(self):
args = ["./bin/trdg", "-c", "10", "--output_dir", "../tests/out/"]
args = ["python3", "run.py", "-c", "10", "--output_dir", "../tests/out/"]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 10)
empty_directory("tests/out/")

def test_random_sequences_letter_only(self):
args = ["./bin/trdg", "-rs", "-let", "-c", "1", "--output_dir", "../tests/out/"]
args = ["python3", "run.py", "-rs", "-let", "-c", "1", "--output_dir", "../tests/out/"]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(
all(
Expand All @@ -739,7 +739,7 @@ def test_random_sequences_letter_only(self):
empty_directory("tests/out/")

def test_random_sequences_number_only(self):
args = ["./bin/trdg", "-rs", "-num", "-c", "1", "--output_dir", "../tests/out/"]
args = ["python3", "run.py", "-rs", "-num", "-c", "1", "--output_dir", "../tests/out/"]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(
all(
Expand All @@ -753,7 +753,7 @@ def test_random_sequences_number_only(self):
empty_directory("tests/out/")

def test_random_sequences_symbols_only(self):
args = ["./bin/trdg", "-rs", "-sym", "-c", "1", "--output_dir", "../tests/out/"]
args = ["python3", "run.py", "-rs", "-sym", "-c", "1", "--output_dir", "../tests/out/"]
subprocess.Popen(args, cwd="trdg/").wait()
with open("tests/out/labels.txt", "r") as f:
self.assertTrue(
Expand All @@ -767,14 +767,14 @@ def test_random_sequences_symbols_only(self):
empty_directory("tests/out/")

def test_handwritten(self):
args = ["./bin/trdg", "-c", "1", "--output_dir", "../tests/out/"]
args = ["python3", "run.py", "-c", "1", "--output_dir", "../tests/out/"]
subprocess.Popen(args, cwd="trdg/").wait()
self.assertTrue(len(os.listdir("tests/out/")) == 1)
empty_directory("tests/out/")

def test_personalfont(self):
args = [
"./bin/trdg",
"python3", "run.py",
"--font",
"fonts/latin/Aller_Bd.ttf",
"-c",
Expand All @@ -788,7 +788,7 @@ def test_personalfont(self):

def test_personalfont_unlocated(self):
args = [
"./bin/trdg",
"python3", "run.py",
"--font",
"fonts/latin/unlocatedFont.ttf",
"-c",
Expand Down
23 changes: 16 additions & 7 deletions trdg/computer_text_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from PIL import Image, ImageColor, ImageFont, ImageDraw, ImageFilter


def generate(text, font, text_color, font_size, orientation, space_width, character_spacing, fit):
def generate(
text, font, text_color, font_size, orientation, space_width, character_spacing, fit
):
if orientation == 0:
return _generate_horizontal_text(
text, font, text_color, font_size, space_width, character_spacing, fit
Expand All @@ -16,14 +18,14 @@ def generate(text, font, text_color, font_size, orientation, space_width, charac
raise ValueError("Unknown orientation " + str(orientation))


def _generate_horizontal_text(text, font, text_color, font_size, space_width, character_spacing, fit):
def _generate_horizontal_text(
text, font, text_color, font_size, space_width, character_spacing, fit
):
image_font = ImageFont.truetype(font=font, size=font_size)

space_width = int(image_font.getsize(" ")[0] * space_width)

char_widths = [
image_font.getsize(c)[0] if c != " " else space_width for c in text
]
char_widths = [image_font.getsize(c)[0] if c != " " else space_width for c in text]
text_width = sum(char_widths) + character_spacing * (len(text) - 1)
text_height = max([image_font.getsize(c)[1] for c in text])

Expand Down Expand Up @@ -54,7 +56,9 @@ def _generate_horizontal_text(text, font, text_color, font_size, space_width, ch
return txt_img


def _generate_vertical_text(text, font, text_color, font_size, space_width, character_spacing, fit):
def _generate_vertical_text(
text, font, text_color, font_size, space_width, character_spacing, fit
):
image_font = ImageFont.truetype(font=font, size=font_size)

space_height = int(image_font.getsize(" ")[1] * space_width)
Expand All @@ -79,7 +83,12 @@ def _generate_vertical_text(text, font, text_color, font_size, space_width, char
)

for i, c in enumerate(text):
txt_draw.text((0, sum(char_heights[0:i]) + i * character_spacing), c, fill=fill, font=image_font)
txt_draw.text(
(0, sum(char_heights[0:i]) + i * character_spacing),
c,
fill=fill,
font=image_font,
)

if fit:
return txt_img.crop(txt_img.getbbox())
Expand Down
15 changes: 9 additions & 6 deletions trdg/data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@

from PIL import Image, ImageFilter

from trdg import (
computer_text_generator,
background_generator,
distorsion_generator,
)
from trdg import computer_text_generator, background_generator, distorsion_generator

try:
from trdg import handwritten_text_generator
Expand Down Expand Up @@ -66,7 +62,14 @@ def generate(
image = handwritten_text_generator.generate(text, text_color)
else:
image = computer_text_generator.generate(
text, font, text_color, size, orientation, space_width, character_spacing, fit
text,
font,
text_color,
size,
orientation,
space_width,
character_spacing,
fit,
)

random_angle = rnd.randint(0 - skewing_angle, skewing_angle)
Expand Down
4 changes: 1 addition & 3 deletions trdg/generators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from trdg.generators.from_dict import GeneratorFromDict
from trdg.generators.from_random import GeneratorFromRandom
from trdg.generators.from_strings import GeneratorFromStrings
from trdg.generators.from_wikipedia import (
GeneratorFromWikipedia,
)
from trdg.generators.from_wikipedia import GeneratorFromWikipedia
53 changes: 28 additions & 25 deletions trdg/generators/from_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,32 @@ def __next__(self):
def next(self):
if self.generated_count == self.count:
raise StopIteration
self.generated_count += 1
return FakeTextDataGenerator.generate(
self.generated_count,
self.generated_count += 1
return (
FakeTextDataGenerator.generate(
self.generated_count,
self.strings[(self.generated_count - 1) % len(self.strings)],
self.fonts[(self.generated_count - 1) % len(self.fonts)],
None,
self.size,
None,
self.skewing_angle,
self.random_skew,
self.blur,
self.random_blur,
self.background_type,
self.distorsion_type,
self.distorsion_orientation,
self.is_handwritten,
0,
self.width,
self.alignment,
self.text_color,
self.orientation,
self.space_width,
self.character_spacing,
self.margins,
self.fit,
),
self.strings[(self.generated_count - 1) % len(self.strings)],
self.fonts[(self.generated_count - 1) % len(self.fonts)],
None,
self.size,
None,
self.skewing_angle,
self.random_skew,
self.blur,
self.random_blur,
self.background_type,
self.distorsion_type,
self.distorsion_orientation,
self.is_handwritten,
0,
self.width,
self.alignment,
self.text_color,
self.orientation,
self.space_width,
self.character_spacing,
self.margins,
self.fit,
), self.strings[(self.generated_count - 1) % len(self.strings)]
)
2 changes: 0 additions & 2 deletions trdg/bin/trdg → trdg/run.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python

import argparse
import os, errno
import random as rnd
Expand Down

0 comments on commit 01329e0

Please sign in to comment.