[WIP] recursive random table

This commit is contained in:
Matthias Bisping 2023-01-18 13:11:15 +01:00
parent 893622a73e
commit a5cd3d6ec9

View File

@ -100,6 +100,7 @@ from funcy import (
keep,
repeatedly,
mapcat,
rpartial,
)
from cv_analysis.locations import TEST_PAGE_TEXTURES_DIR
@ -386,8 +387,8 @@ class ContentGenerator:
text_boxes = merge_related_rectangles(text_boxes)
text_boxes = lmap(generate_recursive_random_table, text_boxes)
plots = lmap(generate_random_table, every_nth(2, char_boxes))
tables = lmap(generate_random_table, every_nth(2, char_boxes[1:]))
plots = lmap(generate_recursive_random_table, every_nth(2, char_boxes))
tables = lmap(generate_recursive_random_table, every_nth(2, char_boxes[1:]))
boxes = text_boxes + plots + tables
boxes = remove_included(boxes)
@ -438,10 +439,11 @@ class RecursiveRandomTable(RandomContentRectangle):
def __init__(self, x1, y1, x2, y2, seed=None):
super().__init__(x1, y1, x2, y2, seed=seed)
self.n_columns = max(self.width // 100, 1)
self.n_rows = max(self.height // 100, 1)
self.n_rows = max(self.height // 17, 1)
self.cell_size = (self.width // self.n_columns, self.height // self.n_rows)
logger.debug(f"RecursiveRandomTable({self.n_columns}, {self.n_rows}, {self.cell_size}) at {self}")
self.content = Image.new("RGB", (self.width, self.height), (255, 255, 255))
self.draw = ImageDraw.Draw(self.content)
def generate_random_table(self):
cells = list(self.generate_cells_with_content())
@ -453,36 +455,32 @@ class RecursiveRandomTable(RandomContentRectangle):
def generate_cells_with_content(self):
for cell in self.generate_table():
# self.draw_single_cell_borders(cell)
cell.content = generate_random_text_block(cell, n_sentences=1).content
assert cell.content.mode == "RGBA"
yield cell
def draw_cell_borders(self, cells):
draw = ImageDraw.Draw(self.content)
def draw_cell_borders(self, cells: List[ContentRectangle]):
for cell in cells:
draw.rectangle(cell.coords, outline=(0, 0, 0))
self.draw_single_cell_borders(cell)
def draw_single_cell_borders(self, cell):
def draw_single_cell_borders(self, cell: ContentRectangle):
draw = ImageDraw.Draw(self.content)
draw.rectangle(cell.coords, outline=(1, 1, 0))
draw.rectangle(cell.coords, fill=(0, 1, 0, 1), outline=(0, 0, 0))
def generate_table(self) -> Iterable[ContentRectangle]:
yield from mapcat(self.generate_column, range(self.n_columns))
def generate_column(self, column_index) -> Iterable[ContentRectangle]:
logger.debug(f"Generating column {column_index}.")
generate_cell_content_for_row = partial(self.generate_cell, column_index=column_index)
generate_cell_content_for_row = partial(self.generate_cell, column_index)
yield from map(generate_cell_content_for_row, range(self.n_rows))
def generate_cell(self, row_index, column_index) -> ContentRectangle:
def generate_cell(self, column_index, row_index) -> ContentRectangle:
w, h = self.cell_size
x1, y1 = self.x1 + column_index * w, self.y1 + row_index * h
x1, y1 = (column_index * w), (row_index * h)
x2, y2 = x1 + w, y1 + h
logger.debug(f"Generating cell ({row_index}, {column_index}) at ({x1}, {y1}, {x2}, {y2}).")
assert x1 >= self.x1
assert y1 >= self.y1
assert x2 <= self.x2
assert y2 <= self.y2
return ContentRectangle(x1, y1, x2, y2)
def generate_column_names(self):