Skip to content

Commit

Permalink
Change our area-resize mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
bghira committed Sep 21, 2023
1 parent 887d8b4 commit 53f1a55
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions helpers/multiaspect/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,16 @@ def resize_by_pixel_edge(input_image: Image, resolution: int) -> Image:
return MultiaspectImage._resize_image(input_image, W, H)

@staticmethod
def resize_by_pixel_area(input_image: Image, area: float) -> Image:
def resize_by_pixel_area(input_image: Image, megapixels: float) -> Image:
W, H = input_image.size
aspect_ratio = W / H
W = MultiaspectImage._round_to_nearest_multiple(sqrt(area * aspect_ratio), 8)
H = MultiaspectImage._round_to_nearest_multiple(sqrt(area / aspect_ratio), 8)
return MultiaspectImage._resize_image(input_image, W, H)
total_pixels = megapixels * 1e6 # Convert megapixels to pixels

W_new = int(round(sqrt(total_pixels * aspect_ratio)))
H_new = int(round(sqrt(total_pixels / aspect_ratio)))

# Ensure they are divisible by 8
W_new = MultiaspectImage._round_to_nearest_multiple(W_new, 8)
H_new = MultiaspectImage._round_to_nearest_multiple(H_new, 8)

return MultiaspectImage._resize_image(input_image, W_new, H_new)

0 comments on commit 53f1a55

Please sign in to comment.