From c7b224a98a355f93653a0d576a10fbd2507ed1d8 Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Wed, 1 Feb 2023 18:14:54 +0100 Subject: [PATCH] Refactoring: Move Move cell class into its own module --- synthesis/segment/segments.py | 0 synthesis/segment/table/cell.py | 81 ++++++++++++++++++++++++++++++++ synthesis/segment/table/table.py | 80 +------------------------------ 3 files changed, 83 insertions(+), 78 deletions(-) create mode 100644 synthesis/segment/segments.py diff --git a/synthesis/segment/segments.py b/synthesis/segment/segments.py new file mode 100644 index 0000000..e69de29 diff --git a/synthesis/segment/table/cell.py b/synthesis/segment/table/cell.py index e69de29..6114594 100644 --- a/synthesis/segment/table/cell.py +++ b/synthesis/segment/table/cell.py @@ -0,0 +1,81 @@ +from PIL import Image, ImageDraw + +from cv_analysis.utils.image_operations import superimpose +from synthesis.segment.content_rectangle import ContentRectangle + + +class Cell(ContentRectangle): + def __init__(self, x1, y1, x2, y2, color=None): + super().__init__(x1, y1, x2, y2) + + self.background_color = color or (255, 255, 255, 0) + + # to debug use random border color: tuple([random.randint(100, 200) for _ in range(3)] + [255]) + self.cell_border_color = (0, 0, 0, 255) + + self.border_width = 1 + self.inset = 1 + + self.content = Image.new("RGBA", (self.width, self.height)) + self.fill() + + def draw_top_border(self, width=None): + self.draw_line((0, 0, self.width - self.inset, 0), width=width) + return self + + def draw_bottom_border(self, width=None): + self.draw_line((0, self.height - self.inset, self.width - self.inset, self.height - self.inset), width=width) + return self + + def draw_left_border(self, width=None): + self.draw_line((0, 0, 0, self.height), width=width) + return self + + def draw_right_border(self, width=None): + self.draw_line((self.width - self.inset, 0, self.width - self.inset, self.height), width=width) + return self + + def draw_line(self, points, width=None): + width = width or self.border_width + draw = ImageDraw.Draw(self.content) + draw.line(points, width=width, fill=self.cell_border_color) + return self + + def draw(self, width=None): + self.draw_top_border(width=width) + self.draw_bottom_border(width=width) + self.draw_left_border(width=width) + self.draw_right_border(width=width) + return self + + def draw_top_left_corner(self, width=None): + self.draw_line((0, 0, 0, 0), width=width) + self.draw_line((0, 0, 0, 0), width=width) + return self + + def draw_top_right_corner(self, width=None): + self.draw_line((self.width - self.inset, 0, self.width - self.inset, 0), width=width) + self.draw_line((self.width - self.inset, 0, self.width - self.inset, 0), width=width) + return self + + def draw_bottom_left_corner(self, width=None): + self.draw_line((0, self.height - self.inset, 0, self.height - self.inset), width=width) + self.draw_line((0, self.height - self.inset, 0, self.height - self.inset), width=width) + return self + + def draw_bottom_right_corner(self, width=None): + self.draw_line( + (self.width - self.inset, self.height - self.inset, self.width - self.inset, self.height - self.inset), + width=width, + ) + self.draw_line( + (self.width - self.inset, self.height - self.inset, self.width - self.inset, self.height - self.inset), + width=width, + ) + return self + + def fill(self, color=None): + color = color or self.background_color + image = Image.new("RGBA", (self.width, self.height), color=color) + self.content = superimpose(image, self.content) + return self diff --git a/synthesis/segment/table/table.py b/synthesis/segment/table/table.py index f257e65..ce763e9 100644 --- a/synthesis/segment/table/table.py +++ b/synthesis/segment/table/table.py @@ -5,7 +5,7 @@ from functools import lru_cache, partial from math import sqrt from typing import List, Iterable -from PIL import Image, ImageDraw +from PIL import Image from funcy import chunks, mapcat, repeatedly from loguru import logger @@ -18,6 +18,7 @@ from synthesis.random import rnd, possibly from synthesis.segment.content_rectangle import ContentRectangle from synthesis.segment.plot import RandomPlot, pick_colormap from synthesis.segment.random_content_rectangle import RandomContentRectangle +from synthesis.segment.table.cell import Cell from synthesis.segment.text_block import TextBlock from synthesis.text.font import pick_random_font_available_on_system from synthesis.text.text import generate_random_words, generate_random_number @@ -282,83 +283,6 @@ class RecursiveRandomTable(RandomContentRectangle): return column_name -class Cell(ContentRectangle): - def __init__(self, x1, y1, x2, y2, color=None): - super().__init__(x1, y1, x2, y2) - - self.background_color = color or (255, 255, 255, 0) - - # to debug use random border color: tuple([random.randint(100, 200) for _ in range(3)] + [255]) - self.cell_border_color = (0, 0, 0, 255) - - self.border_width = 1 - self.inset = 1 - - self.content = Image.new("RGBA", (self.width, self.height)) - self.fill() - - def draw_top_border(self, width=None): - self.draw_line((0, 0, self.width - self.inset, 0), width=width) - return self - - def draw_bottom_border(self, width=None): - self.draw_line((0, self.height - self.inset, self.width - self.inset, self.height - self.inset), width=width) - return self - - def draw_left_border(self, width=None): - self.draw_line((0, 0, 0, self.height), width=width) - return self - - def draw_right_border(self, width=None): - self.draw_line((self.width - self.inset, 0, self.width - self.inset, self.height), width=width) - return self - - def draw_line(self, points, width=None): - width = width or self.border_width - draw = ImageDraw.Draw(self.content) - draw.line(points, width=width, fill=self.cell_border_color) - return self - - def draw(self, width=None): - self.draw_top_border(width=width) - self.draw_bottom_border(width=width) - self.draw_left_border(width=width) - self.draw_right_border(width=width) - return self - - def draw_top_left_corner(self, width=None): - self.draw_line((0, 0, 0, 0), width=width) - self.draw_line((0, 0, 0, 0), width=width) - return self - - def draw_top_right_corner(self, width=None): - self.draw_line((self.width - self.inset, 0, self.width - self.inset, 0), width=width) - self.draw_line((self.width - self.inset, 0, self.width - self.inset, 0), width=width) - return self - - def draw_bottom_left_corner(self, width=None): - self.draw_line((0, self.height - self.inset, 0, self.height - self.inset), width=width) - self.draw_line((0, self.height - self.inset, 0, self.height - self.inset), width=width) - return self - - def draw_bottom_right_corner(self, width=None): - self.draw_line( - (self.width - self.inset, self.height - self.inset, self.width - self.inset, self.height - self.inset), - width=width, - ) - self.draw_line( - (self.width - self.inset, self.height - self.inset, self.width - self.inset, self.height - self.inset), - width=width, - ) - return self - - def fill(self, color=None): - color = color or self.background_color - image = Image.new("RGBA", (self.width, self.height), color=color) - self.content = superimpose(image, self.content) - return self - - def generate_text_block(rectangle: Rectangle, text) -> ContentRectangle: block = TextBlock( *rectangle.coords,