Repository navigation
Expand file tree
/
Copy path_image_utils.py
More file actions
105 lines (83 loc) · 2.17 KB
/
Copy path_image_utils.py
File metadata and controls
105 lines (83 loc) · 2.17 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
from PIL import Image
from mtlogger import logger
from _settings import (
LONG_STRIP_ASPECT_RATIO,
MAX_HEIGHT,
MAX_WIDTH,
OUTPUT_FORMAT,
OUTPUT_EXTENSION,
OUTPUT_QUALITY,
WEBP_METHOD,
)
JPEG_EXTENSION = '.jpeg'
JPG_EXTENSION = '.jpg'
PNG_EXTENSION = '.png'
WEBP_EXTENSION = '.webp'
FILE_EXCLUSIONS = ['folder.jpg', 'cover.jpg']
def is_image_file(
filename: str,
):
name = filename.lower()
return (
name.endswith(
(JPG_EXTENSION, JPEG_EXTENSION, PNG_EXTENSION, WEBP_EXTENSION)
)
and name not in FILE_EXCLUSIONS
)
def is_image_uncompressed(
filename: str,
):
return filename.lower().endswith(PNG_EXTENSION)
def is_image_optimally_compressed(
filename: str,
):
return filename.lower().endswith(WEBP_EXTENSION)
def get_max_dimensions(
img: Image.Image,
):
width, height = img.size
aspect_ratio = width / height
height_ratio = height / MAX_HEIGHT
width_ratio = width / MAX_WIDTH
is_portrait = height > width
is_long_strip = aspect_ratio <= LONG_STRIP_ASPECT_RATIO
too_wide = width > MAX_WIDTH and is_portrait
too_tall = height > MAX_HEIGHT and not is_long_strip
if too_wide and width_ratio > height_ratio:
width = MAX_WIDTH
height = int(width / aspect_ratio)
elif too_tall:
height = MAX_HEIGHT
width = int(height * aspect_ratio)
else:
return None
return (width, height)
def image_needs_resizing(
img: Image.Image,
):
return get_max_dimensions(img) is not None
def resize_image(
img: Image.Image,
):
new_dimensions = get_max_dimensions(img)
if new_dimensions:
return img.resize(new_dimensions, Image.LANCZOS)
return img
def save_image_to_path(
img: Image.Image,
original_img_path: str,
keep=False,
):
try:
output_img_path = (
f'{os.path.splitext(original_img_path)[0]}.{OUTPUT_EXTENSION}'
)
save_options = {'quality': OUTPUT_QUALITY}
if OUTPUT_FORMAT.upper() == 'WEBP':
save_options['method'] = WEBP_METHOD
img.save(output_img_path, OUTPUT_FORMAT, **save_options)
if not keep and output_img_path != original_img_path:
os.remove(original_img_path)
except Exception as ex:
logger.error(f'Error processing "{original_img_path}":\n{ex}')