[WIP] random text segments
This commit is contained in:
parent
e831ab1382
commit
4dfdd579a2
22
test/fixtures/page_generation/page.py
vendored
22
test/fixtures/page_generation/page.py
vendored
@ -1,5 +1,4 @@
|
|||||||
import random
|
import random
|
||||||
import string
|
|
||||||
import textwrap
|
import textwrap
|
||||||
from typing import Tuple, Union, Iterable, List
|
from typing import Tuple, Union, Iterable, List
|
||||||
|
|
||||||
@ -11,7 +10,7 @@ from PIL import Image, ImageOps, ImageFont, ImageDraw
|
|||||||
from PIL.Image import Transpose
|
from PIL.Image import Transpose
|
||||||
from faker import Faker
|
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]
|
Image_t = Union[Image.Image, np.ndarray]
|
||||||
#
|
#
|
||||||
@ -70,7 +69,7 @@ Image_t = Union[Image.Image, np.ndarray]
|
|||||||
# ],
|
# ],
|
||||||
# p=0.5,
|
# 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
|
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)
|
wrapped_text = textwrap.wrap(text, width=image.width, break_long_words=False)
|
||||||
text_size = draw.textsize(first(wrapped_text), font=self.font)[1]
|
text_size = draw.textsize(first(wrapped_text), font=self.font)[1]
|
||||||
|
|
||||||
last_full = True
|
lines = last(take(len(wrapped_text), iterate(star(self.f), (True, wrapped_text))))[1]
|
||||||
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
|
|
||||||
|
|
||||||
|
for i, line in enumerate(lines):
|
||||||
draw.text((0, i * text_size), line, font=self.font, fill=(0, 0, 0, 200))
|
draw.text((0, i * text_size), line, font=self.font, fill=(0, 0, 0, 200))
|
||||||
|
|
||||||
self.content = image
|
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:
|
if random.random() < self.blank_line_percentage and last_full:
|
||||||
line = self.truncate_line(line)
|
line = self.truncate_line(line)
|
||||||
last_full = False
|
last_full = False
|
||||||
else:
|
else:
|
||||||
last_full = True
|
last_full = True
|
||||||
return last_full, line
|
|
||||||
|
return last_full, [*lines[1:], line]
|
||||||
|
|
||||||
def format_line(self, line, full=True):
|
def format_line(self, line, full=True):
|
||||||
line = self.truncate_line(line) if not full else line
|
line = self.truncate_line(line) if not full else line
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user