Skip to content

Commit

Permalink
Improve conversion of focus points into focal areas (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
ababic authored Apr 25, 2024
1 parent 8facc2d commit 015512d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 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
8 changes: 4 additions & 4 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ def test_update_from_asset_data(self):
self.assertEqual(self.obj.is_public, self.asset_data["isPublic"] == 1)
self.assertEqual(self.obj.focal_point_x, 13)
self.assertEqual(self.obj.focal_point_y, 13)
self.assertEqual(self.obj.focal_point_height, 26)
self.assertEqual(self.obj.focal_point_width, 26)
self.assertEqual(self.obj.focal_point_height, 20)
self.assertEqual(self.obj.focal_point_width, 20)
self.assertTrue(self.obj._focal_point_changed)

def test_update_from_asset_data_with_focal_point_change(self):
Expand Down Expand Up @@ -246,8 +246,8 @@ def test_update_from_asset_data_without_focal_point_change(self):
# Set the focal point values to reflect the typical test outcome
self.obj.focal_point_x = 13
self.obj.focal_point_y = 13
self.obj.focal_point_height = 26
self.obj.focal_point_width = 26
self.obj.focal_point_height = 20
self.obj.focal_point_width = 20

current_focal_point = self.obj.get_focal_point()

Expand Down

0 comments on commit 015512d

Please sign in to comment.