Refactoring: Move
Move rectangle shrinking logic into new morphing module
This commit is contained in:
parent
b77951d4fe
commit
9c401a977c
38
cv_analysis/utils/morphing.py
Normal file
38
cv_analysis/utils/morphing.py
Normal file
@ -0,0 +1,38 @@
|
||||
from typing import Tuple
|
||||
|
||||
from PIL import Image
|
||||
from loguru import logger
|
||||
|
||||
from cv_analysis.utils.image_operations import compute_pasting_coordinates
|
||||
from cv_analysis.utils.rectangle import Rectangle
|
||||
from synthesis.segment.content_rectangle import ContentRectangle
|
||||
|
||||
|
||||
def shrink_rectangle(rectangle: Rectangle, factor: float) -> Rectangle:
|
||||
x1, y1, x2, y2 = compute_scaled_coordinates(rectangle, (1 - factor))
|
||||
|
||||
logger.trace(f"Shrinking {rectangle} by {factor} to ({x1}, {y1}, {x2}, {y2}).")
|
||||
|
||||
assert x1 >= rectangle.x1
|
||||
assert y1 >= rectangle.y1
|
||||
assert x2 <= rectangle.x2
|
||||
assert y2 <= rectangle.y2
|
||||
|
||||
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]:
|
||||
# FIXME: Refactor: Using image to compute coordinates is not clean
|
||||
image = Image.new("RGBA", (rectangle.width, rectangle.height))
|
||||
scaled = image.resize((int(rectangle.width * factor), int(rectangle.height * factor)))
|
||||
|
||||
x1, y1 = compute_pasting_coordinates(scaled, image)
|
||||
x1 = rectangle.x1 + x1
|
||||
y1 = rectangle.y1 + y1
|
||||
x2, y2 = x1 + scaled.width, y1 + scaled.height
|
||||
return x1, y1, x2, y2
|
||||
32
test/fixtures/page_generation/page.py
vendored
32
test/fixtures/page_generation/page.py
vendored
@ -10,7 +10,7 @@ from PIL.Image import Transpose
|
||||
from loguru import logger
|
||||
|
||||
from cv_analysis.utils.conversion import normalize_image_format_to_array, normalize_image_format_to_pil
|
||||
from cv_analysis.utils.image_operations import blur, sharpen, overlay, superimpose, compute_pasting_coordinates
|
||||
from cv_analysis.utils.image_operations import blur, sharpen, overlay, superimpose
|
||||
from cv_analysis.utils.merging import merge_related_rectangles
|
||||
from cv_analysis.utils.postprocessing import remove_overlapping, remove_included
|
||||
from synthesis.partitioner.two_column import TwoColumnPagePartitioner
|
||||
@ -253,36 +253,6 @@ def split_into_figure_and_caption(rectangle: Rectangle):
|
||||
return figure_box, caption_box
|
||||
|
||||
|
||||
def shrink_rectangle(rectangle: Rectangle, factor: float) -> Rectangle:
|
||||
x1, y1, x2, y2 = compute_scaled_coordinates(rectangle, (1 - factor))
|
||||
|
||||
logger.trace(f"Shrinking {rectangle} by {factor} to ({x1}, {y1}, {x2}, {y2}).")
|
||||
|
||||
assert x1 >= rectangle.x1
|
||||
assert y1 >= rectangle.y1
|
||||
assert x2 <= rectangle.x2
|
||||
assert y2 <= rectangle.y2
|
||||
|
||||
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]:
|
||||
# TODO: Refactor: Using image to compute coordinates is not clean
|
||||
image = Image.new("RGBA", (rectangle.width, rectangle.height))
|
||||
scaled = image.resize((int(rectangle.width * factor), int(rectangle.height * factor)))
|
||||
|
||||
x1, y1 = compute_pasting_coordinates(scaled, image)
|
||||
x1 = rectangle.x1 + x1
|
||||
y1 = rectangle.y1 + y1
|
||||
x2, y2 = x1 + scaled.width, y1 + scaled.height
|
||||
return x1, y1, x2, y2
|
||||
|
||||
|
||||
def generate_random_text_block(rectangle: Rectangle, n_sentences=3000) -> ContentRectangle:
|
||||
block = TextBlock(
|
||||
*rectangle.coords,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user