[WIP] random text segments

This commit is contained in:
Matthias Bisping 2023-01-16 17:41:30 +01:00
parent e831ab1382
commit 4dfdd579a2

View File

@ -1,5 +1,4 @@
import random
import string
import textwrap
from typing import Tuple, Union, Iterable, List
@ -11,7 +10,7 @@ from PIL import Image, ImageOps, ImageFont, ImageDraw
from PIL.Image import Transpose
from faker import Faker
from cv_analysis.utils.conversion import rectangle_to_box
from cv_analysis.utils import star
Image_t = Union[Image.Image, np.ndarray]
#
@ -70,7 +69,7 @@ Image_t = Union[Image.Image, np.ndarray]
# ],
# p=0.5,
# )
from funcy import juxt, compose, identity, lflatten, lmap, first
from funcy import juxt, compose, identity, lflatten, lmap, first, iterate, take, last
from cv_analysis.locations import TEST_PAGE_TEXTURES_DIR
@ -361,25 +360,24 @@ class RandomTextBlock(ContentRectangle):
wrapped_text = textwrap.wrap(text, width=image.width, break_long_words=False)
text_size = draw.textsize(first(wrapped_text), font=self.font)[1]
last_full = True
for i, line in enumerate(wrapped_text):
if random.random() < self.blank_line_percentage and last_full:
line = self.truncate_line(line)
last_full = False
else:
last_full = True
lines = last(take(len(wrapped_text), iterate(star(self.f), (True, wrapped_text))))[1]
for i, line in enumerate(lines):
draw.text((0, i * text_size), line, font=self.font, fill=(0, 0, 0, 200))
self.content = image
def f(self, last_full, line):
def f(self, last_full, lines):
line = lines[0]
if random.random() < self.blank_line_percentage and last_full:
line = self.truncate_line(line)
last_full = False
else:
last_full = True
return last_full, line
return last_full, [*lines[1:], line]
def format_line(self, line, full=True):
line = self.truncate_line(line) if not full else line