-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.py
More file actions
36 lines (26 loc) · 1.25 KB
/
helpers.py
File metadata and controls
36 lines (26 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import webp
from PIL import Image
def convert_image_to_webp(image, quality: int = 70):
config = webp.WebPConfig.new(preset=webp.WebPPreset.PHOTO, quality=quality)
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