Skip to content

Commit

Permalink
Add character_spacing argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Belval committed Nov 25, 2019
1 parent b1db01c commit a900e82
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ docker run -v /output/path/:/app/out/ -t belval/trdg:latest trdg [args]
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!)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
packages=find_packages(exclude=["contrib", "docs", "tests"]),
include_package_data=True,
install_requires=[
"pillow>=5.1.0",
"pillow==5.1.0",
"numpy>=1.15.1,<1.17",
"requests>=2.20.0",
"opencv-python>=4.0.0.21",
Expand Down
15 changes: 15 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def test_generate_data_with_format(self):
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -180,6 +181,7 @@ def test_generate_data_with_extension(self):
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -213,6 +215,7 @@ def test_generate_data_with_skew_angle(self):
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -246,6 +249,7 @@ def test_generate_data_with_blur(self):
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -279,6 +283,7 @@ def test_generate_data_with_sine_distorsion(self):
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -312,6 +317,7 @@ def test_generate_data_with_cosine_distorsion(self):
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -345,6 +351,7 @@ def test_generate_data_with_left_alignment(self):
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -378,6 +385,7 @@ def test_generate_data_with_center_alignment(self):
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -411,6 +419,7 @@ def test_generate_data_with_right_alignment(self):
"#010101",
0,
1,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -445,6 +454,7 @@ def test_raise_if_handwritten_and_vertical(self):
"#010101",
1,
1,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -474,6 +484,7 @@ def test_generate_vertical_text(self):
"#010101",
1,
1,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -507,6 +518,7 @@ def test_generate_horizontal_text_with_variable_space(self):
"#010101",
0,
4,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -540,6 +552,7 @@ def test_generate_vertical_text_with_variable_space(self):
"#010101",
1,
2,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -574,6 +587,7 @@ def test_generate_text_with_unknown_orientation(self):
"#010101",
100,
2,
0,
(5, 5, 5, 5),
0,
)
Expand Down Expand Up @@ -603,6 +617,7 @@ def test_generate_data_with_fit(self):
"#010101",
0,
1,
0,
(0, 0, 0, 0),
1,
)
Expand Down
9 changes: 9 additions & 0 deletions trdg/bin/trdg
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ def parse_arguments():
help="Define the width of the spaces between words. 2.0 means twice the normal space width",
default=1.0,
)
parser.add_argument(
"-cs",
"--character_spacing",
type=int,
nargs="?",
help="Define the width of the spaces between characters. 2 means two pixels",
default=0,
)
parser.add_argument(
"-m",
"--margins",
Expand Down Expand Up @@ -357,6 +365,7 @@ def main():
[args.text_color] * string_count,
[args.orientation] * string_count,
[args.space_width] * string_count,
[args.character_spacing] * string_count,
[args.margins] * string_count,
[args.fit] * string_count,
),
Expand Down
32 changes: 17 additions & 15 deletions trdg/computer_text_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@
from PIL import Image, ImageColor, ImageFont, ImageDraw, ImageFilter


def generate(text, font, text_color, font_size, orientation, space_width, 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, fit
text, font, text_color, font_size, space_width, character_spacing, fit
)
elif orientation == 1:
return _generate_vertical_text(
text, font, text_color, font_size, space_width, fit
text, font, text_color, font_size, space_width, character_spacing, fit
)
else:
raise ValueError("Unknown orientation " + str(orientation))


def _generate_horizontal_text(text, font, text_color, font_size, space_width, 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)
words = text.split(" ")
space_width = image_font.getsize(" ")[0] * space_width

words_width = [image_font.getsize(w)[0] for w in words]
text_width = sum(words_width) + int(space_width) * (len(words) - 1)
text_height = max([image_font.getsize(w)[1] for w in words])
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
]
text_width = sum(char_widths) + character_spacing * (len(text) - 1)
text_height = max([image_font.getsize(c)[1] for c in text])

txt_img = Image.new("RGBA", (text_width, text_height), (0, 0, 0, 0))

Expand All @@ -38,10 +40,10 @@ def _generate_horizontal_text(text, font, text_color, font_size, space_width, fi
rnd.randint(min(c1[2], c2[2]), max(c1[2], c2[2])),
)

for i, w in enumerate(words):
for i, c in enumerate(text):
txt_draw.text(
(sum(words_width[0:i]) + i * int(space_width), 0),
w,
(sum(char_widths[0:i]) + i * character_spacing, 0),
c,
fill=fill,
font=image_font,
)
Expand All @@ -52,7 +54,7 @@ def _generate_horizontal_text(text, font, text_color, font_size, space_width, fi
return txt_img


def _generate_vertical_text(text, font, text_color, font_size, space_width, 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 @@ -61,7 +63,7 @@ def _generate_vertical_text(text, font, text_color, font_size, space_width, fit)
image_font.getsize(c)[1] if c != " " else space_height for c in text
]
text_width = max([image_font.getsize(c)[0] for c in text])
text_height = sum(char_heights)
text_height = sum(char_heights) + character_spacing * len(text)

txt_img = Image.new("RGBA", (text_width, text_height), (0, 0, 0, 0))

Expand All @@ -77,7 +79,7 @@ def _generate_vertical_text(text, font, text_color, font_size, space_width, fit)
)

for i, c in enumerate(text):
txt_draw.text((0, sum(char_heights[0:i])), 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
3 changes: 2 additions & 1 deletion trdg/data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def generate(
text_color,
orientation,
space_width,
character_spacing,
margins,
fit,
):
Expand All @@ -65,7 +66,7 @@ def generate(
image = handwritten_text_generator.generate(text, text_color)
else:
image = computer_text_generator.generate(
text, font, text_color, size, orientation, space_width, fit
text, font, text_color, size, orientation, space_width, character_spacing, fit
)

random_angle = rnd.randint(0 - skewing_angle, skewing_angle)
Expand Down
2 changes: 2 additions & 0 deletions trdg/generators/from_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(
text_color="#282828",
orientation=0,
space_width=1.0,
character_spacing=0,
margins=(5, 5, 5, 5),
fit=False,
):
Expand All @@ -54,6 +55,7 @@ def __init__(
text_color,
orientation,
space_width,
character_spacing,
margins,
fit,
)
Expand Down
2 changes: 2 additions & 0 deletions trdg/generators/from_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(
text_color="#282828",
orientation=0,
space_width=1.0,
character_spacing=0,
margins=(5, 5, 5, 5),
fit=False,
):
Expand Down Expand Up @@ -68,6 +69,7 @@ def __init__(
text_color,
orientation,
space_width,
character_spacing,
margins,
fit,
)
Expand Down
3 changes: 3 additions & 0 deletions trdg/generators/from_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(
text_color="#282828",
orientation=0,
space_width=1.0,
character_spacing=0,
margins=(5, 5, 5, 5),
fit=False,
):
Expand All @@ -48,6 +49,7 @@ def __init__(
self.text_color = text_color
self.orientation = orientation
self.space_width = space_width
self.character_spacing = character_spacing
self.margins = margins
self.fit = fit
self.generated_count = 0
Expand Down Expand Up @@ -83,6 +85,7 @@ def next(self):
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: 2 additions & 0 deletions trdg/generators/from_wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(
text_color="#282828",
orientation=0,
space_width=1.0,
character_spacing=0,
margins=(5, 5, 5, 5),
fit=False,
):
Expand All @@ -52,6 +53,7 @@ def __init__(
text_color,
orientation,
space_width,
character_spacing,
margins,
fit,
)
Expand Down

0 comments on commit a900e82

Please sign in to comment.