This commit is contained in:
Matthias Bisping 2023-01-18 19:22:48 +01:00
parent 73d546367c
commit 48f6aebc13

View File

@ -103,7 +103,6 @@ from funcy import (
keep,
repeatedly,
mapcat,
rpartial,
)
from cv_analysis.locations import TEST_PAGE_TEXTURES_DIR
@ -189,9 +188,9 @@ def dpi(request):
@pytest.fixture(
params=[
# "brown",
"brown",
# "sepia",
"gray",
# "gray",
# "white",
# "light_red",
# "light_blue",
@ -437,12 +436,16 @@ def generate_random_plot(rectangle: Rectangle) -> ContentRectangle:
def generate_random_table(rectangle: Rectangle) -> ContentRectangle:
block = RandomTable(*rectangle.coords)
block.content = (
rectangle.content if isinstance(rectangle, (ContentRectangle, RandomContentRectangle)) else None
) # TODO: Refactor
block.generate_random_table(rectangle)
return block
def generate_recursive_random_table(rectangle: Rectangle, **kwargs) -> ContentRectangle:
block = RecursiveRandomTable(*rectangle.coords, **kwargs)
# block.content = rectangle.content if isinstance(rectangle, ContentRectangle) else None # TODO: Refactor
block.generate_random_table()
return block
@ -465,8 +468,8 @@ class Size(Enum):
# LARGE = sqrt((100 * 10) ** 2)
SMALL = 100
MEDIUM = 170
LARGE = 200
MEDIUM = 180
LARGE = 300
def get_size_class(rectangle: Rectangle):
@ -485,7 +488,7 @@ def get_size(rectangle: Rectangle):
class RecursiveRandomTable(RandomContentRectangle):
def __init__(self, x1, y1, x2, y2, seed=None, border_width=3):
def __init__(self, x1, y1, x2, y2, seed=None, border_width=2):
super().__init__(x1, y1, x2, y2, seed=seed)
self.n_columns = random.randint(1, max(self.width // 100, 1))
self.n_rows = random.randint(1, max(self.height // random.randint(17, 100), 1))
@ -494,7 +497,7 @@ class RecursiveRandomTable(RandomContentRectangle):
self.content = Image.new("RGBA", (self.width, self.height), (255, 255, 255, 255))
self.background_color = tuple([random.randint(0, 100) for _ in range(4)])
self.cell_border_color = (*map(lambda x: int(x * 0.8), self.background_color[:3]), 255)
self.draw_single_cell_borders(self, border_width, fill=self.background_color)
self.draw_single_cell_borders(self, border_width, fill=(0, 0, 0, 0))
def generate_random_table(self):
cells = list(self.generate_cells_with_content())
@ -503,11 +506,12 @@ class RecursiveRandomTable(RandomContentRectangle):
def generate_cells_with_content(self):
for cell in self.generate_table():
self.draw_single_cell_borders(cell, fill=self.background_color, width=2)
self.draw_single_cell_borders(cell, fill=(0, 0, 0, 0), width=2)
def inner(cell):
inner_region = shrink_rectangle(cell, 0.2)
inner_region = shrink_rectangle(cell, 0.4)
choice = random.choice(["text", "plot", "recurse", "plain_table", "blank"])
size = get_size(inner_region)
@ -516,8 +520,11 @@ class RecursiveRandomTable(RandomContentRectangle):
return generate_text_block(cell, " ".join(words))
elif size <= Size.MEDIUM.value:
choice = random.choice(["plot", "plain_table"])
if choice == "plain_table":
return generate_random_table(inner_region)
return generate_random_table(cell)
# cell.content = generate_random_table(inner_region).content
# return cell
elif choice == "plot" and is_square_like(cell):
@ -526,16 +533,15 @@ class RecursiveRandomTable(RandomContentRectangle):
return generate_text_block(cell, f"{choice} {size:.0f} {get_size_class(cell).name}")
elif size <= Size.LARGE.value:
choice = random.choice(["plot", "recurse"])
logger.debug(f"Generating {choice} {size:.0f} {get_size_class(cell).name}")
if choice == "plot" and is_square_like(cell):
return generate_random_plot(cell)
# elif choice == "plain_table":
# return generate_random_table(cell)
elif choice == "blank":
return cell
else:
logger.debug(f"recurse {size:.0f} {get_size_class(cell).name}")
return generate_recursive_random_table(inner_region, border_width=5)
return generate_recursive_random_table(inner_region, border_width=4)
else:
return generate_text_block(cell, f"{choice} {size:.0f} {get_size_class(cell).name}")
@ -662,7 +668,7 @@ class RandomTable(RandomContentRectangle):
image = write_lines_to_image(table_lines, rectangle)
self.join_lines(image)
self.content = image
self.content = image if not self.content else superimpose_texture_with_transparency(self.content, image)
def generate_random_dataframe(self, rectangle: Rectangle):
"""Generates a random dataframe that has as many rows and columns as to fit the given rectangle."""
@ -735,9 +741,11 @@ class RandomFontPicker:
fonts = itertools.compress(self.fonts, mask)
fonts = keep(map(self.load_font, fonts))
fonts = filter(self.font_is_renderable, fonts) # FIXME: this does not work
fonts = (font for font in fonts if font.getname()[1].lower() not in ["italic"])
fonts = (font for font in fonts if font.getname()[1].lower() in ["bold"])
font = first(fonts)
logger.trace(f"Using font: {font}")
logger.info(f"Using font: {font.getname()}")
return font
def shuffle_fonts(self):
@ -945,7 +953,7 @@ class TextBlock(ContentRectangle):
image = Image.new("RGBA", (text_width, text_height), (0, 255, 255, 0))
if width_delta > 0 or height_delta > 0:
image = image.resize((rectangle.width, text_height))
image = image.resize((int(rectangle.width * 0.9), text_height))
draw = ImageDraw.Draw(image)
draw.text((0, 0), text, font=self.font, fill=(0, 0, 0, 255))