Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions files/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,30 @@ def convert_image_to_webp(image, quality: int = 70):
pil_image = Image.open(image.file)
webp_image = webp.WebPPicture.from_pil(pil_image)
return webp_image.encode(config)


def resize_image(image, size=(512, 512)):
pil_image = Image.open(image.file)

# Подгонка параметров для горизонтальных изображений
if pil_image.height < pil_image.width:
width, height = pil_image.width, pil_image.width
x, y = 0, int((pil_image.height - height) // 2)

# Подгонка параметров для вертикальных изображений
elif pil_image.height > pil_image.width:
width, height = pil_image.height, pil_image.height
x, y = int((pil_image.width - width) // 2), 0

# Подгонка параметров для квадратных изображений
else:
width, height = pil_image.width, pil_image.height
x, y = 0, 0

# Итоговые размеры до ресайза
area = (x, y, x + width, y + height)

pil_image = pil_image.crop(area)
pil_image = pil_image.resize(size)

return pil_image