Skip to content

Commit

Permalink
fix: Fix pad to image when resized size larger than new square canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
hh-space-invader committed Nov 25, 2024
1 parent 082cb77 commit aa6be34
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions fastembed/image/transform/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,17 @@ def pad2square(
) -> Image.Image:
height, width = image.height, image.width

# if the size is larger than the new canvas
if width > size or height > size:
left = (width - size) // 2
top = (height - size) // 2
right = left + size
bottom = top + size
image = image.crop((left, top, right, bottom))
return image

new_image = Image.new(mode="RGB", size=(size, size), color=fill_color)
left = (size - height) // 2
top = (size - width) // 2
left = (size - width) // 2
top = (size - height) // 2
new_image.paste(image, (left, top))
return new_image

0 comments on commit aa6be34

Please sign in to comment.