Refactoring

This commit is contained in:
Matthias Bisping 2023-01-30 14:10:46 +01:00
parent 25d35e2349
commit a1ccda4ea9
2 changed files with 54 additions and 21 deletions

View File

@ -298,13 +298,18 @@ def overlay(images, mode=np.sum):
@pytest.fixture
def texture(base_texture, color, color_intensity):
color_image = Image.new("RGBA", base_texture.size, color)
color_image.putalpha(color_intensity)
texture = superimpose_texture_with_transparency(base_texture, color_image)
def texture(blank_page, base_texture):
texture = superimpose_texture_with_transparency(base_texture, blank_page)
return texture
@pytest.fixture
def blank_page(size, color, color_intensity):
color_image = Image.new("RGBA", size, color)
color_image.putalpha(color_intensity)
return color_image
def tint_image(src, color="#FFFFFF"):
src.load()
r, g, b, alpha = src.split()
@ -382,10 +387,9 @@ def compute_pasting_coordinates(smaller: Image, larger: Image.Image):
return abs(larger.width - smaller.width) // 2, abs(larger.height - smaller.height) // 2
# TODO: Rename: No longer blank
@pytest.fixture
def blank_page(texture, texture_fn) -> np.ndarray:
"""Creates a blank page with a given orientation and dpi."""
def page_with_content(texture, texture_fn) -> np.ndarray:
"""Creates a page with content"""
page = random_flip(texture)
page = texture_fn(page)
page_partitioner = rnd.choice(
@ -404,6 +408,19 @@ def blank_page(texture, texture_fn) -> np.ndarray:
return page
def blend(a, b):
"""Reference: https://stackoverflow.com/a/52143032"""
a = a.astype(float) / 255
b = b.astype(float) / 255 # make float on range 0-1
mask = a >= 0.5 # generate boolean mask of everywhere a > 0.5
ab = np.zeros_like(a) # generate an output container for the blended image
# now do the blending
ab[~mask] = (2 * a * b)[~mask] # 2ab everywhere a<0.5
ab[mask] = (1 - 2 * (1 - a) * (1 - b))[mask] # else this
class ContentRectangle(Rectangle):
def __init__(self, x1, y1, x2, y2, content=None):
super().__init__(x1, y1, x2, y2)
@ -474,7 +491,7 @@ def generate_random_plot_with_caption(rectangle: Rectangle):
# TODO: deduplicate with generate_random_table_with_caption
def generate_recursive_random_table_with_caption(rectangle: Rectangle):
table_box, caption_box = split_into_figure_and_caption(rectangle)
table_box = generate_recursive_random_table(table_box, double_border=probably())
table_box = generate_recursive_random_table(table_box, double_rule=probably())
caption_box = generate_random_table_caption(caption_box)
return table_box, caption_box
@ -556,6 +573,8 @@ def get_size(rectangle: Rectangle):
def get_random_color_complementing_color_map(colormap):
def color_complement(r, g, b):
"""Reference: https://stackoverflow.com/a/40234924"""
def hilo(a, b, c):
if c < b:
b, c = c, b
@ -580,15 +599,29 @@ def get_random_background_color():
class RecursiveRandomTable(RandomContentRectangle):
def __init__(self, x1, y1, x2, y2, seed=None, border_width=1, layout=None, double_border=False):
super().__init__(x1, y1, x2, y2, seed=seed)
def __init__(self, x1, y1, x2, y2, border_width=1, layout: str = None, double_rule=False):
"""A table with a random number of rows and columns, and random content in each cell.
self.double_border = double_border
self.double_border_width = (3 * border_width) if self.double_border else 0
Args:
x1: x-coordinate of the top-left corner
y1: y-coordinate of the top-left corner
x2: x-coordinate of the bottom-right corner
y2: y-coordinate of the bottom-right corner
border_width: width of the table border
layout: layout of the table, either "horizontal", "vertical", "closed", or "open"
double_rule: whether to use double rules as the top and bottom rules
"""
assert layout in [None, "horizontal", "vertical", "closed", "open"]
super().__init__(x1, y1, x2, y2)
self.double_rule = double_rule
self.double_rule_width = (3 * border_width) if self.double_rule else 0
self.n_columns = rnd.randint(1, max(self.width // 100, 1))
self.n_rows = rnd.randint(1, max((self.height - 2 * self.double_border_width) // rnd.randint(17, 100), 1))
self.cell_size = (self.width / self.n_columns, (self.height - 2 * self.double_border_width) / self.n_rows)
self.n_rows = rnd.randint(1, max((self.height - 2 * self.double_rule_width) // rnd.randint(17, 100), 1))
self.cell_size = (self.width / self.n_columns, (self.height - 2 * self.double_rule_width) / self.n_rows)
self.content = Image.new("RGBA", (self.width, self.height), (255, 255, 255, 0))
@ -646,7 +679,7 @@ class RecursiveRandomTable(RandomContentRectangle):
cell,
border_width=1,
layout=random.choice(["open", "horizontal", "vertical"]),
double_border=False,
double_rule=False,
)
else:
@ -667,7 +700,7 @@ class RecursiveRandomTable(RandomContentRectangle):
cell,
border_width=1,
layout=random.choice(["open", "horizontal", "vertical"]),
double_border=False,
double_rule=False,
)
else:
return generate_text_block(cell, f"{choice} {size:.0f} {get_size_class(cell).name}")
@ -704,7 +737,7 @@ class RecursiveRandomTable(RandomContentRectangle):
yield self
# TODO: Refactor
if self.double_border:
if self.double_rule:
c1 = Cell(*self.coords)
c1.draw_top_border(width=1)
c1.draw_bottom_border(width=1)
@ -712,7 +745,7 @@ class RecursiveRandomTable(RandomContentRectangle):
# self.content = superimpose_texture_with_transparency(c.content, self.content)
x1, y1, x2, y2 = self.coords
c2 = Cell(x1, y1 + self.double_border_width, x2, y2 - self.double_border_width)
c2 = Cell(x1, y1 + self.double_rule_width, x2, y2 - self.double_rule_width)
c2.draw_top_border(width=1)
c2.draw_bottom_border(width=1)
@ -732,7 +765,7 @@ class RecursiveRandomTable(RandomContentRectangle):
def generate_cell(self, column_index, row_index) -> ContentRectangle:
w, h = self.cell_size
x1, y1 = (column_index * w), (row_index * h) + self.double_border_width
x1, y1 = (column_index * w), (row_index * h) + self.double_rule_width
x2, y2 = x1 + w, y1 + h
logger.trace(f"Generating cell ({row_index}, {column_index}) at ({x1}, {y1}, {x2}, {y2}).")
return Cell(x1, y1, x2, y2, self.background_color)

View File

@ -1,6 +1,6 @@
from cv_analysis.utils.display import show_image
def test_blank_page(blank_page):
def test_blank_page(page_with_content):
pass
show_image(blank_page)
# show_image(blank_page)