-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.py
More file actions
41 lines (33 loc) · 1.47 KB
/
processor.py
File metadata and controls
41 lines (33 loc) · 1.47 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
from PIL import Image
class ImageProcessor:
def optimize_without_quality_loss(self, input_path, output_path):
try:
with Image.open(input_path) as img:
img = self._convert_to_rgb(img)
if input_path.lower().endswith(('.png', '.bmp')):
img.save(output_path, optimize=True)
else:
img.save(output_path, quality=95, optimize=True, progressive=True)
return True
except Exception as e:
print(f"Error optimizing {input_path}: {e}")
return False
def compress_with_quality_reduction(self, input_path, output_path, quality):
try:
with Image.open(input_path) as img:
img = self._convert_to_rgb(img)
img.save(output_path, quality=quality, optimize=True, progressive=True)
return True
except Exception as e:
print(f"Error compressing {input_path}: {e}")
return False
def _convert_to_rgb(self, img):
if img.mode in ('RGBA', 'LA', 'P'):
if img.mode == 'P':
img = img.convert('RGBA')
background = Image.new('RGB', img.size, (255, 255, 255))
background.paste(img, mask=img.split()[-1] if img.mode == 'RGBA' else None)
return background
elif img.mode != 'RGB':
return img.convert('RGB')
return img