From 5dc13e7137a513138a55c7e05781abd8bd3f7e5e Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Wed, 25 Jan 2023 18:16:33 +0100 Subject: [PATCH] [WIP] More table / cell edge fiddling and issue fixing Fix: The cell width and height were rounded to int in the table constructor. The imprecison of rounding would accumulate when stacking cells in a row or columns leading to gaps at the bottom and right hand edge of tables. The rounding has now been removed and left to the cell constructor. Cells are derived from the Rectangle class, which does the rounding itself. This eliminates the issue with accumulated gaps in the tables. --- test/fixtures/page_generation/page.py | 37 ++++++++++++++++++--------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/test/fixtures/page_generation/page.py b/test/fixtures/page_generation/page.py index 316d9cd..9c717e3 100644 --- a/test/fixtures/page_generation/page.py +++ b/test/fixtures/page_generation/page.py @@ -32,7 +32,8 @@ 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 +# random_seed = 3896311122 +random_seed = 1986343479 rnd = random.Random(random_seed) logger.info(f"Random seed: {random_seed}") @@ -165,10 +166,10 @@ Color = Tuple[int, int, int] @pytest.fixture( params=[ - "rough_grain", + # "rough_grain", # "plain", # "digital", - # "crumpled", + "crumpled", ] ) def base_texture(request, size): @@ -199,10 +200,10 @@ def dpi(request): @pytest.fixture( params=[ - "brown", + # "brown", # "sepia", # "gray", - # "white", + "white", # "light_red", # "light_blue", ] @@ -224,9 +225,9 @@ def texture_name(request): @pytest.fixture( params=[ - 30, + # 30, # 70, - # 150, + 150, ] ) def color_intensity(request): @@ -351,6 +352,7 @@ def superimpose_texture_with_transparency(page: Image, texture: Image, autocrop= assert page.size == texture.size assert texture.mode == "RGBA" + page.paste(texture, (0, 0), texture) return page @@ -544,7 +546,16 @@ class RecursiveRandomTable(RandomContentRectangle): super().__init__(x1, y1, x2, y2, seed=seed) self.n_columns = rnd.randint(1, max(self.width // 100, 1)) self.n_rows = rnd.randint(1, max(self.height // rnd.randint(17, 100), 1)) - self.cell_size = (self.width // self.n_columns, self.height // self.n_rows) + cell_width, cell_height = (self.width / self.n_columns, self.height / self.n_rows) + + width_delta = (self.width - cell_width * self.n_columns) / self.n_columns + height_delta = (self.height - cell_height * self.n_rows) / self.n_rows + + logger.debug(f"width_delta: {width_delta}, height_delta: {height_delta}") + + self.cell_size = cell_width, cell_height + + logger.debug(f"cell size: {self.cell_size}") self.content = Image.new("RGBA", (self.width, self.height), (255, 255, 255, 0)) self.background_color = tuple([rnd.randint(100, 200) for _ in range(3)] + [rnd.randint(180, 210)]) @@ -562,7 +573,7 @@ class RecursiveRandomTable(RandomContentRectangle): # elif self.n_rows == 1: # self.layout = rnd.choice(["horizontal", "closed"]) - self.layout = "open" # TODO: Remove this line + self.layout = "closed" # TODO: Remove this line self.layout = layout or self.layout @@ -585,10 +596,10 @@ class RecursiveRandomTable(RandomContentRectangle): def inner(cell): - inner_region = shrink_rectangle(cell, 0.11) + # inner_region = shrink_rectangle(cell, 0.11) choice = rnd.choice(["text", "plot", "recurse", "plain_table", "blank"]) - size = get_size(inner_region) + size = get_size(cell) if size <= Size.SMALL.value: words = generate_random_words(1, 3) @@ -641,6 +652,8 @@ class RecursiveRandomTable(RandomContentRectangle): if row_index < self.n_rows - 1: cell.draw_bottom_border() + # cell.draw() + columns = chunks(self.n_rows, cells) for col_idx, columns in enumerate(columns): for row_index, cell in enumerate(columns): @@ -777,7 +790,7 @@ class Cell(ContentRectangle): def fill(self, color=None): color = color or self.background_color image = Image.new("RGBA", (self.width, self.height), color=color) - self.content = image if not self.content else superimpose_texture_with_transparency(image, self.content) + self.content = superimpose_texture_with_transparency(image, self.content) return self