-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize_ss.py
More file actions
55 lines (42 loc) · 2.3 KB
/
resize_ss.py
File metadata and controls
55 lines (42 loc) · 2.3 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
from PIL import Image
import os
import sys
import glob
def resize_screenshot(input_path, output_path):
try:
# Проверяем существование файла
if not os.path.exists(input_path):
raise FileNotFoundError(f"Файл не найден: {input_path}")
# Проверяем, является ли файл изображением
if not input_path.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
raise ValueError("Файл должен быть изображением (PNG/JPG/JPEG/WEBP)")
# Открываем изображение с проверкой
try:
img = Image.open(input_path)
except Exception as e:
raise ValueError(f"Не удалось открыть изображение: {e}")
# Оптимальные параметры
max_width = 800
quality = 80
# Пропорциональное масштабирование
width_percent = max_width / float(img.size[0])
new_height = int(float(img.size[1]) * float(width_percent))
# Изменяем размер с использованием LANCZOS (высококачественное масштабирование)
resized_img = img.resize((max_width, new_height), Image.Resampling.LANCZOS)
# Создаём папку для результата, если её нет
os.makedirs(os.path.dirname(output_path), exist_ok=True)
# Сохраняем в формате JPEG (можно изменить на WEBP)
resized_img.save(output_path, 'JPEG', quality=quality, optimize=True)
print(f"✔ Успешно: {output_path} ({max_width}x{new_height}px)")
except Exception as e:
print(f"❌ Ошибка: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Использование: python resize_screenshot.py <входной_файл> <выходной_файл>")
print("Пример: python resize_screenshot.py input.jpg output.jpg")
sys.exit(1)
resize_screenshot(sys.argv[1], sys.argv[2])
for img_path in glob.glob("images/*.jpg"):
output_path = f"optimized/{os.path.basename(img_path)}"
resize_screenshot(img_path, output_path)