From 30e63508813b66f48ab341b071f8f3610a2f14fc Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Wed, 18 Jan 2023 14:49:00 +0100 Subject: [PATCH] [WIP] recursive random table Add padding between cell content and cell border --- test/fixtures/page_generation/page.py | 53 +++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/test/fixtures/page_generation/page.py b/test/fixtures/page_generation/page.py index 296dcb4..a18fbc7 100644 --- a/test/fixtures/page_generation/page.py +++ b/test/fixtures/page_generation/page.py @@ -340,13 +340,33 @@ def size(dpi, orientation): def superimpose_texture_with_transparency(page: Image, texture: Image) -> Image: """Superimposes a noise image with transparency onto a page image.""" - # assert page.mode == "RGB" - assert texture.mode == "RGBA" + if page.size != texture.size: + logger.debug(f"Padding image before pasting to fit size {page.size}") + texture = pad_image_to_size(texture, page.size) assert page.size == texture.size + assert texture.mode == "RGBA" page.paste(texture, (0, 0), texture) return page +def pad_image_to_size(image: Image, size: Tuple[int, int]) -> Image: + """Pads an image to a given size.""" + if image.size == size: + return image + if image.size[0] > size[0] or image.size[1] > size[1]: + raise ValueError(f"Image size {image.size} is larger than target size {size}.") + padded = Image.new(image.mode, size, color=255) + pasting_coords = compute_pasting_coordinates(image, padded) + assert image.mode == "RGBA" + padded.paste(image, pasting_coords, image) + return padded + + +def compute_pasting_coordinates(smaller: Image, larger: Image.Image): + """Computes the coordinates for centrally pasting a smaller image onto a larger image.""" + return abs(larger.width - smaller.width) // 2, abs(larger.height - smaller.height) // 2 + + @pytest.fixture def blank_page(texture, texture_fn) -> np.ndarray: """Creates a blank page with a given orientation and dpi.""" @@ -444,7 +464,7 @@ class RecursiveRandomTable(RandomContentRectangle): self.content = Image.new("RGBA", (self.width, self.height), (255, 255, 255, 255)) self.background_color = tuple([random.randint(100, 200) for _ in range(4)]) - self.cell_border_color = tuple(map(lambda x: int(x * 0.8), self.background_color[:3])) + self.cell_border_color = (*map(lambda x: int(x * 0.8), self.background_color[:3]), 255) def generate_random_table(self): self.draw_single_cell_borders(self, 3) @@ -455,9 +475,8 @@ class RecursiveRandomTable(RandomContentRectangle): def generate_cells_with_content(self): for cell in self.generate_table(): self.draw_single_cell_borders(cell) - # cell.content.show() - # exit(0) - cell.content = generate_random_text_block(cell, n_sentences=1).content + inner_region = shrink_rectangle(cell, 0.1) + cell.content = generate_random_text_block(inner_region, n_sentences=1).content assert cell.content.mode == "RGBA" yield cell @@ -499,6 +518,24 @@ class RecursiveRandomTable(RandomContentRectangle): return column_name +def shrink_rectangle(rectangle: Rectangle, factor: float) -> Rectangle: + x1, y1, x2, y2 = compute_scaled_coordinates(rectangle, factor) + shrunk_rectangle = Rectangle(x1, y1, x2, y2) + if isinstance(rectangle, ContentRectangle): # TODO: Refactor + shrunk_rectangle = ContentRectangle(*shrunk_rectangle.coords, rectangle.content) + return shrunk_rectangle + + +def compute_scaled_coordinates(rectangle: Rectangle, factor: float) -> Tuple[int, int, int, int]: + width = rectangle.width * (1 - factor) + height = rectangle.height * (1 - factor) + x1 = rectangle.x1 + (rectangle.width - width) / 2 + y1 = rectangle.y1 + (rectangle.height - height) / 2 + x2 = x1 + width + y2 = y1 + height + return tuple(lmap(int, (x1, y1, x2, y2))) + + class RandomTable(RandomContentRectangle): def __init__(self, x1, y1, x2, y2, seed=None): super().__init__(x1, y1, x2, y2, seed=seed) @@ -758,7 +795,7 @@ def generate_random_text_block(rectangle: Rectangle, n_sentences=3000) -> Conten def write_lines_to_image(lines: List[str], rectangle: Rectangle, font=None) -> Image.Image: def write_line(line, line_number): - draw.text((0, line_number * text_size), line, font=font, fill=(0, 0, 0, 200)) + draw.text((0, line_number * text_size), line, font=font, fill=(0, 0, 0, 255)) font = font or pick_random_mono_space_font_available_on_system() @@ -776,7 +813,7 @@ class RandomTextBlock(ContentRectangle): def __init__(self, x1, y1, x2, y2): super().__init__(x1, y1, x2, y2) self.blank_line_percentage = random.uniform(0, 0.5) - self.font = pick_random_font_available_on_system() + self.font = ImageFont.load_default() # pick_random_font_available_on_system() def __call__(self, *args, **kwargs): pass