Refactoring: Re-order

This commit is contained in:
Matthias Bisping 2023-02-01 13:22:16 +01:00
parent 04a617b9df
commit d42f053c81

View File

@ -34,6 +34,7 @@ logger.add(sys.stderr, level="INFO")
random_seed = random.randint(0, 2**32 - 1)
random_seed = 2973413116
# random_seed = 2212357755
rnd = random.Random(random_seed)
logger.info(f"Random seed: {random_seed}")
@ -324,87 +325,6 @@ def compute_pasting_coordinates(smaller: Image, larger: Image.Image):
return abs(larger.width - smaller.width) // 2, abs(larger.height - smaller.height) // 2
@pytest.fixture
def page_with_opaque_content(
blank_page,
tinted_blank_page,
texture,
texture_fn,
) -> Tuple[np.ndarray, Iterable[Rectangle]]:
"""Creates a page with content"""
page_partitioner = rnd.choice(
[
TwoColumnPagePartitioner(),
# RandomPagePartitioner(),
]
)
texture = random_flip(texture)
texture = texture_fn(texture)
boxes = page_partitioner(texture)
content_generator = ContentGenerator()
boxes = content_generator(boxes)
page = paste_contents(texture, boxes)
return page, boxes
@pytest.fixture
def page_with_translucent_content(
blank_page, tinted_blank_page, texture, texture_fn
) -> Tuple[np.ndarray, List[Rectangle]]:
"""Creates a page with content"""
page_partitioner = rnd.choice(
[
TwoColumnPagePartitioner(),
# RandomPagePartitioner(),
]
)
boxes = page_partitioner(blank_page)
content_generator = ContentGenerator()
boxes = content_generator(boxes)
page_content = paste_contents(blank_page, boxes)
texture = random_flip(texture)
texture = texture_fn(texture)
texture.putalpha(255)
page_content.putalpha(255)
factor = 1.2
enhancer = ImageEnhance.Contrast(texture)
texture = enhancer.enhance(factor)
page = blend_modes.multiply(
*map(
to_array,
(
page_content,
texture,
),
),
opacity=1,
).astype(np.uint8)
return page, boxes
@pytest.fixture
def page_with_content(
# page_with_translucent_content,
page_with_opaque_content,
) -> np.ndarray:
# page, boxes = page_with_translucent_content
page, boxes = page_with_opaque_content
draw_boxes(page, boxes)
return page
def to_array(image: Image) -> np.ndarray:
"""Converts a PIL image to a numpy array."""
return np.array(image).astype(np.float32)
@ -1505,3 +1425,84 @@ def drop_small_boxes(boxes: Iterable[Rectangle], page_width, page_height, min_pe
def draw_boxes(page: Image, boxes: Iterable[Rectangle]):
# page = draw_rectangles(page, boxes, filled=False, annotate=True)
show_image(page, backend="pil")
@pytest.fixture
def page_with_opaque_content(
blank_page,
tinted_blank_page,
texture,
texture_fn,
) -> Tuple[np.ndarray, Iterable[Rectangle]]:
"""Creates a page with content"""
page_partitioner = rnd.choice(
[
TwoColumnPagePartitioner(),
# RandomPagePartitioner(),
]
)
texture = random_flip(texture)
texture = texture_fn(texture)
boxes = page_partitioner(texture)
content_generator = ContentGenerator()
boxes = content_generator(boxes)
page = paste_contents(texture, boxes)
return page, boxes
@pytest.fixture
def page_with_translucent_content(
blank_page, tinted_blank_page, texture, texture_fn
) -> Tuple[np.ndarray, List[Rectangle]]:
"""Creates a page with content"""
page_partitioner = rnd.choice(
[
TwoColumnPagePartitioner(),
# RandomPagePartitioner(),
]
)
boxes = page_partitioner(blank_page)
content_generator = ContentGenerator()
boxes = content_generator(boxes)
page_content = paste_contents(blank_page, boxes)
texture = random_flip(texture)
texture = texture_fn(texture)
texture.putalpha(255)
page_content.putalpha(255)
factor = 1.2
enhancer = ImageEnhance.Contrast(texture)
texture = enhancer.enhance(factor)
page = blend_modes.multiply(
*map(
to_array,
(
page_content,
texture,
),
),
opacity=1,
).astype(np.uint8)
return page, boxes
@pytest.fixture
def page_with_content(
page_with_translucent_content,
# page_with_opaque_content,
) -> np.ndarray:
page, boxes = page_with_translucent_content
# page, boxes = page_with_opaque_content
draw_boxes(page, boxes)
return page