Skip to content

Commit

Permalink
Draw square focal areas around focus points, and restrict area dimens…
Browse files Browse the repository at this point in the history
…ions to a maximum of 40% of the total image width or height
  • Loading branch information
ababic committed Apr 25, 2024
1 parent 8facc2d commit 41cf396
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/wagtail_bynder/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def set_focal_area_from_focus_point(
self, x: int, y: int, original_height: int, original_width: int
) -> None:
"""
Using the provided center-point coordinates, generate a
Using the provided focus point coordinates, generate a
2D focal area for the downloaded image.
"""
if x < 0 or y < 0 or x > original_width or y > original_height:
Expand All @@ -261,11 +261,21 @@ def set_focal_area_from_focus_point(
self.focal_point_x = x
self.focal_point_y = y

# Draw a rectangle around the centre point
# For the width, span outwards until we hit the left or right bounds
self.focal_point_width = min(x, self.width - x) * 2
rect_width = min(x, self.width - x) * 2
# Restrict rectangle width to 40% of the image height
rect_width = min(rect_width, math.floor(self.width * 0.4))

# For the height, span outwards until we hit the top or bottom bounds
self.focal_point_height = min(y, self.height - y) * 2
rect_height = min(y, self.height - y) * 2
# Restrict rectangle height to 40% of the image height
rect_height = min(rect_height, math.floor(self.height * 0.4))

# Use the shortest side to make a square
width = min(rect_width, rect_height)
self.focal_point_width = width
self.focal_point_height = width

@staticmethod
def extract_file_source(asset_data: dict[str, Any]) -> str:
Expand Down

0 comments on commit 41cf396

Please sign in to comment.