[WIP] More table / cell edge fiddling and issue fixing

This commit is contained in:
Matthias Bisping 2023-01-24 13:53:59 +01:00
parent cee5e69a4b
commit 7676a8148e

View File

@ -31,6 +31,7 @@ from cv_analysis.utils.postprocessing import remove_overlapping, remove_included
from cv_analysis.utils.spacial import area
random_seed = random.randint(0, 2**32 - 1)
random_seed = 3896311122
rnd = random.Random(random_seed)
logger.info(f"Random seed: {random_seed}")
@ -347,8 +348,12 @@ def size(dpi, orientation):
return size
def superimpose_texture_with_transparency(page: Image, texture: Image) -> Image:
def superimpose_texture_with_transparency(page: Image, texture: Image, autocrop=True) -> Image:
"""Superimposes a noise image with transparency onto a page image."""
if autocrop:
texture = texture.crop(texture.getbbox())
if page.size != texture.size:
logger.trace(f"Padding image before pasting to fit size {page.size}")
texture = pad_image_to_size(texture, page.size)
@ -550,8 +555,12 @@ class RecursiveRandomTable(RandomContentRectangle):
self.content = Image.new("RGBA", (self.width, self.height), (255, 255, 255, 0))
self.background_color = tuple([rnd.randint(0, 100) for _ in range(4)])
self.cell_border_color = (*map(lambda x: int(x * 0.8), self.background_color[:3]), 255)
self.background_color = tuple([random.randint(0, 100) for _ in range(4)])
self.cell_border_color = (0, 0, 0, 255) # (*map(lambda x: int(x * 0.8), self.background_color[:3]), 255)
self.layout = rnd.choice(["closed", "horizontal", "vertical", "open"])
self.layout = "closed"
# Overwrite the layout choice in some cases
if self.n_columns == 1 and self.n_rows == 1:
@ -571,7 +580,7 @@ class RecursiveRandomTable(RandomContentRectangle):
def generate_cells_with_content(self):
for cell in self.generate_table():
self.draw_single_cell_borders(cell, fill=(0, 0, 0, 0), width=1)
self.draw_single_cell_borders(cell, width=1)
def inner(cell):
@ -624,22 +633,20 @@ class RecursiveRandomTable(RandomContentRectangle):
self.draw_single_cell_borders(cell, fill=self.background_color)
def draw_single_cell_borders(self, cell: ContentRectangle, width=1, fill=None):
fill = (0, 0, 0, 0) if fill is None else fill
# fill = (0, 0, 0, 0) if fill is None else fill
image = cell.content or Image.new("RGBA", (cell.width, cell.height), (255, 255, 255, 0))
assert image.mode == "RGBA"
draw = ImageDraw.Draw(image)
# TODO: Refactor
if self.layout == "closed":
draw.rectangle(
(0, 0, cell.width - 1, cell.height - 1), fill=fill, outline=self.cell_border_color, width=width
)
draw.rectangle((0, 0, cell.width - 1, cell.height - 1), outline=self.cell_border_color, width=width)
elif self.layout == "vertical":
draw.line((0, 0, 0, cell.height - 1), fill=self.cell_border_color, width=width)
draw.line((cell.width - 1, 0, cell.width - 1, cell.height - 1), fill=self.cell_border_color, width=width)
draw.line((0, 0, 0, cell.height - 1), width=width, fill=self.cell_border_color)
draw.line((cell.width - 1, 0, cell.width - 1, cell.height - 1), width=width, fill=self.cell_border_color)
elif self.layout == "horizontal":
draw.line((0, 0, cell.width - 1, 0), fill=self.cell_border_color, width=width)
draw.line((0, cell.height - 1, cell.width - 1, cell.height - 1), fill=self.cell_border_color, width=width)
draw.line((0, 0, cell.width - 1, 0), width=width, fill=self.cell_border_color)
draw.line((0, cell.height - 1, cell.width - 1, cell.height - 1), width=width, fill=self.cell_border_color)
elif self.layout == "open":
pass
else:
@ -653,15 +660,15 @@ class RecursiveRandomTable(RandomContentRectangle):
def generate_column(self, column_index) -> Iterable[ContentRectangle]:
logger.trace(f"Generating column {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))
generate_cells_for_row = partial(self.generate_cell, column_index)
yield from map(generate_cells_for_row, range(self.n_rows))
def generate_cell(self, column_index, row_index) -> ContentRectangle:
w, h = self.cell_size
x1, y1 = (column_index * w), (row_index * h)
x2, y2 = x1 + w, y1 + h
logger.trace(f"Generating cell ({row_index}, {column_index}) at ({x1}, {y1}, {x2}, {y2}).")
return ContentRectangle(x1, y1, x2, y2)
return BlankImageRectangle(x1, y1, x2, y2, self.background_color)
def generate_column_names(self):
column_names = repeatedly(self.generate_column_name, self.n_columns)
@ -672,6 +679,25 @@ class RecursiveRandomTable(RandomContentRectangle):
return column_name
class BlankImageRectangle(ContentRectangle):
def __init__(self, x1, y1, x2, y2, color):
super().__init__(x1, y1, x2, y2)
# image = Image.fromarray(np.random.uniform(0, 255, size=(self.height, self.width, 4)).astype(np.uint8))
# self.content = image.convert("RGBA")
# self.content = Image.new("RGBA", (self.width, self.height), (255, 255, 255, 0))
# self.content = Image.new(
# "RGBA",
# (self.width, self.height),
# color=tuple(random.randint(0, 255) * np.ones(3).astype(np.uint8)),
# )
print(color)
self.content = Image.new("RGBA", (self.width, self.height), color=color[:3])
def generate_random_words(n_min, n_max):
column_name = Faker().words(rnd.randint(n_min, n_max))
return column_name
@ -966,6 +992,7 @@ class RandomPlot(RandomContentRectangle):
maybe() and (ax.spines["top"].set_visible(False) or ax.spines["right"].set_visible(False))
image = dump_plt_to_image(rectangle)
assert image.mode == "RGBA"
self.content = image if not self.content else superimpose_texture_with_transparency(self.content, image)
@ -1302,5 +1329,5 @@ def drop_small_boxes(boxes: Iterable[Rectangle], page_width, page_height, min_pe
def draw_boxes(page: Image, boxes: Iterable[Rectangle]):
page = draw_rectangles(page, boxes, filled=False, annotate=True)
# page = draw_rectangles(page, boxes, filled=False, annotate=True)
show_image(page)