Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Find all effect/layout config example at [link](https://github.com/oh-my-ocr/tex
| 21 | perspective_transform | ![perspective_transform.jpg](https://github.com/oh-my-ocr/text_renderer/raw/master/example_data/effect_layout_image/perspective_transform.jpg) |
| 22 | same_line_layout_different_font_size | ![same_line_layout_different_font_size.jpg](https://github.com/oh-my-ocr/text_renderer/raw/master/example_data/effect_layout_image/same_line_layout_different_font_size.jpg) |
| 23 | vertical_text | ![vertical_text.jpg](https://github.com/oh-my-ocr/text_renderer/raw/master/example_data/effect_layout_image/vertical_text.jpg) |
| 24 | text_stroke_width | ![text_stroke_width.jpg](https://github.com/oh-my-ocr/text_renderer/raw/master/example_data/effect_layout_image/text_stroke_width.jpg) |


## Contribution
Expand Down
8 changes: 7 additions & 1 deletion example_data/effect_layout_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,15 @@ def emboss():

return cfg

def text_stroke_width():
cfg = base_cfg(inspect.currentframe().f_code.co_name)
cfg.render_cfg.corpus.cfg.stroke_width = (0, 2)
return cfg


configs = [
# bg_and_text_mask()
emboss()
# emboss()
# vertical_text()
# extra_text_line_layout()
# char_spacing_compact(),
Expand All @@ -229,4 +234,5 @@ def emboss():
# dropout_vertical(),
# padding(),
# same_line_layout_different_font_size(),
text_stroke_width()
]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions text_renderer/corpus/corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class CorpusCfg:
horizontal : bool
generate the horizontal(default) or vertical text
Set False to generate vertical text
stroke_width: (Union[int, tuple[int, int]])
Add stroke width to text. If tuple, random choice between (min, max)
Set -1 to disable
"""
font_dir: Path
font_size: Tuple[int, int]
Expand All @@ -46,6 +49,7 @@ class CorpusCfg:
char_spacing: Union[float, Tuple[float, float]] = -1
text_color_cfg: TextColorCfg = field(default_factory=SimpleTextColorCfg)
horizontal: bool = True
stroke_width: Union[int, Tuple[int, int]] = -1

def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
Expand Down
4 changes: 2 additions & 2 deletions text_renderer/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def gen_single_corpus(self) -> Tuple[PILImage, str, PILImage, PILImage]:
text_color = self.corpus.cfg.text_color_cfg.get_color(bg)

text_mask = draw_text_on_bg(
font_text, text_color, char_spacing=self.corpus.cfg.char_spacing
font_text, text_color, char_spacing=self.corpus.cfg.char_spacing, stroke_width=self.corpus.cfg.stroke_width
)

if self.cfg.corpus_effects is not None:
Expand Down Expand Up @@ -217,7 +217,7 @@ def gen_multi_corpus(self) -> Tuple[PILImage, str, PILImage, PILImage]:
else:
_text_color = text_color
text_mask = draw_text_on_bg(
font_text, _text_color, char_spacing=self.corpus[i].cfg.char_spacing
font_text, _text_color, char_spacing=self.corpus[i].cfg.char_spacing, stroke_width=self.corpus[i].cfg.stroke_width
)

text_bbox = BBox.from_size(text_mask.size)
Expand Down
31 changes: 26 additions & 5 deletions text_renderer/utils/draw_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def draw_text_on_bg(
font_text: FontText,
text_color: Tuple[int, int, int, int] = (0, 0, 0, 255),
char_spacing: Union[float, Tuple[float, float]] = -1,
stroke_width: Union[int, Tuple[int, int]] = -1
) -> PILImage:
"""

Expand All @@ -34,17 +35,32 @@ def draw_text_on_bg(
char_spacing : Union[float, Tuple[float, float]]
Draw character with spacing. If tuple, random choice between [min, max)
Set -1 to disable

stroke_width: (Union[int, tuple[int, int]])
Add stroke width to text. If tuple, random choice between (min, max)
Set -1 to disable
Returns
-------
PILImage:
RGBA Pillow image with text on a transparent image
-------

"""
# Determine the text stroke width
if stroke_width == -1:
actual_stroke_width = 0
elif isinstance(stroke_width, int):
actual_stroke_width = max(0, stroke_width)
elif isinstance(stroke_width, Tuple) and len(stroke_width) == 2:
try:
actual_stroke_width = max(0, int(np.random.uniform(stroke_width[0], stroke_width[1])))
except (ValueError, TypeError):
actual_stroke_width = 0
else:
actual_stroke_width = 0

if char_spacing == -1:
if font_text.horizontal:
return _draw_text_on_bg(font_text, text_color)
return _draw_text_on_bg(font_text, text_color, actual_stroke_width)
else:
char_spacing = 0

Expand Down Expand Up @@ -95,12 +111,12 @@ def draw_text_on_bg(
if font_text.horizontal:
y_offset = font_text.offset[1]
for i, c in enumerate(font_text.text):
draw.text((c_x, c_y - y_offset), c, fill=text_color, font=font_text.font)
draw.text((c_x, c_y - y_offset), c, fill=text_color, font=font_text.font, stroke_width=actual_stroke_width, stroke_fill=text_color)
c_x += chars_size[i][0] + char_spacings[i]
else:
x_offset = font_text.offset[0]
for i, c in enumerate(font_text.text):
draw.text((c_x - x_offset, c_y), c, fill=text_color, font=font_text.font)
draw.text((c_x - x_offset, c_y), c, fill=text_color, font=font_text.font, stroke_width=actual_stroke_width, stroke_fill=text_color)
c_y += chars_size[i][1] + char_spacings[i]
text_mask = text_mask.rotate(90, expand=True)

Expand All @@ -110,6 +126,7 @@ def draw_text_on_bg(
def _draw_text_on_bg(
font_text: FontText,
text_color: Tuple[int, int, int, int] = (0, 0, 0, 255),
stroke_width: Union[int, Tuple[int, int]] = -1
) -> PILImage:
"""
Draw text
Expand All @@ -119,7 +136,9 @@ def _draw_text_on_bg(
font_text : FontText
text_color : RGBA
Default is black

stroke_width: (Union[int, tuple[int, int]])
Add stroke width to text. If tuple, random choice between (min, max)
Set -1 to disable
Returns
-------
PILImage:
Expand All @@ -138,6 +157,8 @@ def _draw_text_on_bg(
font=font_text.font,
fill=text_color,
anchor=None,
stroke_width=stroke_width,
stroke_fill=text_color
)

return text_mask