Skip to content

Commit a900e82

Browse files
authored
Add character_spacing argument
1 parent b1db01c commit a900e82

File tree

10 files changed

+54
-17
lines changed

10 files changed

+54
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ docker run -v /output/path/:/app/out/ -t belval/trdg:latest trdg [args]
3131
The path (`/output/path/`) must be absolute.
3232

3333
## New
34+
- Add `--character_spacing` to control space between characters (in pixels)
3435
- Add python module
3536
- Move `run.py` to an executable python file ([`trdg/bin/trdg`](trdg/bin/trdg))
3637
- Add `--font` to use only one font for all the generated images (Thank you @JulienCoutault!)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
packages=find_packages(exclude=["contrib", "docs", "tests"]),
4242
include_package_data=True,
4343
install_requires=[
44-
"pillow>=5.1.0",
44+
"pillow==5.1.0",
4545
"numpy>=1.15.1,<1.17",
4646
"requests>=2.20.0",
4747
"opencv-python>=4.0.0.21",

tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def test_generate_data_with_format(self):
147147
"#010101",
148148
0,
149149
1,
150+
0,
150151
(5, 5, 5, 5),
151152
0,
152153
)
@@ -180,6 +181,7 @@ def test_generate_data_with_extension(self):
180181
"#010101",
181182
0,
182183
1,
184+
0,
183185
(5, 5, 5, 5),
184186
0,
185187
)
@@ -213,6 +215,7 @@ def test_generate_data_with_skew_angle(self):
213215
"#010101",
214216
0,
215217
1,
218+
0,
216219
(5, 5, 5, 5),
217220
0,
218221
)
@@ -246,6 +249,7 @@ def test_generate_data_with_blur(self):
246249
"#010101",
247250
0,
248251
1,
252+
0,
249253
(5, 5, 5, 5),
250254
0,
251255
)
@@ -279,6 +283,7 @@ def test_generate_data_with_sine_distorsion(self):
279283
"#010101",
280284
0,
281285
1,
286+
0,
282287
(5, 5, 5, 5),
283288
0,
284289
)
@@ -312,6 +317,7 @@ def test_generate_data_with_cosine_distorsion(self):
312317
"#010101",
313318
0,
314319
1,
320+
0,
315321
(5, 5, 5, 5),
316322
0,
317323
)
@@ -345,6 +351,7 @@ def test_generate_data_with_left_alignment(self):
345351
"#010101",
346352
0,
347353
1,
354+
0,
348355
(5, 5, 5, 5),
349356
0,
350357
)
@@ -378,6 +385,7 @@ def test_generate_data_with_center_alignment(self):
378385
"#010101",
379386
0,
380387
1,
388+
0,
381389
(5, 5, 5, 5),
382390
0,
383391
)
@@ -411,6 +419,7 @@ def test_generate_data_with_right_alignment(self):
411419
"#010101",
412420
0,
413421
1,
422+
0,
414423
(5, 5, 5, 5),
415424
0,
416425
)
@@ -445,6 +454,7 @@ def test_raise_if_handwritten_and_vertical(self):
445454
"#010101",
446455
1,
447456
1,
457+
0,
448458
(5, 5, 5, 5),
449459
0,
450460
)
@@ -474,6 +484,7 @@ def test_generate_vertical_text(self):
474484
"#010101",
475485
1,
476486
1,
487+
0,
477488
(5, 5, 5, 5),
478489
0,
479490
)
@@ -507,6 +518,7 @@ def test_generate_horizontal_text_with_variable_space(self):
507518
"#010101",
508519
0,
509520
4,
521+
0,
510522
(5, 5, 5, 5),
511523
0,
512524
)
@@ -540,6 +552,7 @@ def test_generate_vertical_text_with_variable_space(self):
540552
"#010101",
541553
1,
542554
2,
555+
0,
543556
(5, 5, 5, 5),
544557
0,
545558
)
@@ -574,6 +587,7 @@ def test_generate_text_with_unknown_orientation(self):
574587
"#010101",
575588
100,
576589
2,
590+
0,
577591
(5, 5, 5, 5),
578592
0,
579593
)
@@ -603,6 +617,7 @@ def test_generate_data_with_fit(self):
603617
"#010101",
604618
0,
605619
1,
620+
0,
606621
(0, 0, 0, 0),
607622
1,
608623
)

trdg/bin/trdg

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,14 @@ def parse_arguments():
241241
help="Define the width of the spaces between words. 2.0 means twice the normal space width",
242242
default=1.0,
243243
)
244+
parser.add_argument(
245+
"-cs",
246+
"--character_spacing",
247+
type=int,
248+
nargs="?",
249+
help="Define the width of the spaces between characters. 2 means two pixels",
250+
default=0,
251+
)
244252
parser.add_argument(
245253
"-m",
246254
"--margins",
@@ -357,6 +365,7 @@ def main():
357365
[args.text_color] * string_count,
358366
[args.orientation] * string_count,
359367
[args.space_width] * string_count,
368+
[args.character_spacing] * string_count,
360369
[args.margins] * string_count,
361370
[args.fit] * string_count,
362371
),

trdg/computer_text_generator.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,29 @@
33
from PIL import Image, ImageColor, ImageFont, ImageDraw, ImageFilter
44

55

6-
def generate(text, font, text_color, font_size, orientation, space_width, fit):
6+
def generate(text, font, text_color, font_size, orientation, space_width, character_spacing, fit):
77
if orientation == 0:
88
return _generate_horizontal_text(
9-
text, font, text_color, font_size, space_width, fit
9+
text, font, text_color, font_size, space_width, character_spacing, fit
1010
)
1111
elif orientation == 1:
1212
return _generate_vertical_text(
13-
text, font, text_color, font_size, space_width, fit
13+
text, font, text_color, font_size, space_width, character_spacing, fit
1414
)
1515
else:
1616
raise ValueError("Unknown orientation " + str(orientation))
1717

1818

19-
def _generate_horizontal_text(text, font, text_color, font_size, space_width, fit):
19+
def _generate_horizontal_text(text, font, text_color, font_size, space_width, character_spacing, fit):
2020
image_font = ImageFont.truetype(font=font, size=font_size)
21-
words = text.split(" ")
22-
space_width = image_font.getsize(" ")[0] * space_width
2321

24-
words_width = [image_font.getsize(w)[0] for w in words]
25-
text_width = sum(words_width) + int(space_width) * (len(words) - 1)
26-
text_height = max([image_font.getsize(w)[1] for w in words])
22+
space_width = int(image_font.getsize(" ")[0] * space_width)
23+
24+
char_widths = [
25+
image_font.getsize(c)[0] if c != " " else space_width for c in text
26+
]
27+
text_width = sum(char_widths) + character_spacing * (len(text) - 1)
28+
text_height = max([image_font.getsize(c)[1] for c in text])
2729

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

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

41-
for i, w in enumerate(words):
43+
for i, c in enumerate(text):
4244
txt_draw.text(
43-
(sum(words_width[0:i]) + i * int(space_width), 0),
44-
w,
45+
(sum(char_widths[0:i]) + i * character_spacing, 0),
46+
c,
4547
fill=fill,
4648
font=image_font,
4749
)
@@ -52,7 +54,7 @@ def _generate_horizontal_text(text, font, text_color, font_size, space_width, fi
5254
return txt_img
5355

5456

55-
def _generate_vertical_text(text, font, text_color, font_size, space_width, fit):
57+
def _generate_vertical_text(text, font, text_color, font_size, space_width, character_spacing, fit):
5658
image_font = ImageFont.truetype(font=font, size=font_size)
5759

5860
space_height = int(image_font.getsize(" ")[1] * space_width)
@@ -61,7 +63,7 @@ def _generate_vertical_text(text, font, text_color, font_size, space_width, fit)
6163
image_font.getsize(c)[1] if c != " " else space_height for c in text
6264
]
6365
text_width = max([image_font.getsize(c)[0] for c in text])
64-
text_height = sum(char_heights)
66+
text_height = sum(char_heights) + character_spacing * len(text)
6567

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

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

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

8284
if fit:
8385
return txt_img.crop(txt_img.getbbox())

trdg/data_generator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def generate(
4747
text_color,
4848
orientation,
4949
space_width,
50+
character_spacing,
5051
margins,
5152
fit,
5253
):
@@ -65,7 +66,7 @@ def generate(
6566
image = handwritten_text_generator.generate(text, text_color)
6667
else:
6768
image = computer_text_generator.generate(
68-
text, font, text_color, size, orientation, space_width, fit
69+
text, font, text_color, size, orientation, space_width, character_spacing, fit
6970
)
7071

7172
random_angle = rnd.randint(0 - skewing_angle, skewing_angle)

trdg/generators/from_dict.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def __init__(
2828
text_color="#282828",
2929
orientation=0,
3030
space_width=1.0,
31+
character_spacing=0,
3132
margins=(5, 5, 5, 5),
3233
fit=False,
3334
):
@@ -54,6 +55,7 @@ def __init__(
5455
text_color,
5556
orientation,
5657
space_width,
58+
character_spacing,
5759
margins,
5860
fit,
5961
)

trdg/generators/from_random.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(
3131
text_color="#282828",
3232
orientation=0,
3333
space_width=1.0,
34+
character_spacing=0,
3435
margins=(5, 5, 5, 5),
3536
fit=False,
3637
):
@@ -68,6 +69,7 @@ def __init__(
6869
text_color,
6970
orientation,
7071
space_width,
72+
character_spacing,
7173
margins,
7274
fit,
7375
)

trdg/generators/from_strings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __init__(
2525
text_color="#282828",
2626
orientation=0,
2727
space_width=1.0,
28+
character_spacing=0,
2829
margins=(5, 5, 5, 5),
2930
fit=False,
3031
):
@@ -48,6 +49,7 @@ def __init__(
4849
self.text_color = text_color
4950
self.orientation = orientation
5051
self.space_width = space_width
52+
self.character_spacing = character_spacing
5153
self.margins = margins
5254
self.fit = fit
5355
self.generated_count = 0
@@ -83,6 +85,7 @@ def next(self):
8385
self.text_color,
8486
self.orientation,
8587
self.space_width,
88+
self.character_spacing,
8689
self.margins,
8790
self.fit,
8891
), self.strings[(self.generated_count - 1) % len(self.strings)]

trdg/generators/from_wikipedia.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(
2727
text_color="#282828",
2828
orientation=0,
2929
space_width=1.0,
30+
character_spacing=0,
3031
margins=(5, 5, 5, 5),
3132
fit=False,
3233
):
@@ -52,6 +53,7 @@ def __init__(
5253
text_color,
5354
orientation,
5455
space_width,
56+
character_spacing,
5557
margins,
5658
fit,
5759
)

0 commit comments

Comments
 (0)