-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrupi_processor.py
More file actions
40 lines (34 loc) · 1.39 KB
/
rupi_processor.py
File metadata and controls
40 lines (34 loc) · 1.39 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
from __future__ import annotations
from pathlib import Path
class RupiProcessor:
def __init__(self, scale_percent=50):
self.scale_percent = scale_percent
def build_output_path(self, local_path):
return Path(local_path).with_suffix(".png")
def _require_image(self):
try:
from PIL import Image
except ImportError as exc:
raise RuntimeError("Pillow가 필요합니다. `pip install pillow`로 설치하세요.") from exc
return Image
def process(self, local_path):
Image = self._require_image()
output_path = self.build_output_path(local_path)
output_path.parent.mkdir(parents=True, exist_ok=True)
try:
with Image.open(local_path) as image:
width, height = image.size
new_size = (
max(1, round(width * self.scale_percent / 100)),
max(1, round(height * self.scale_percent / 100)),
)
resized = image.resize(new_size)
resized.save(output_path, format="PNG")
print(
f"[IMAGE] {local_path.name} / size={image.size} / mode={image.mode} "
f"-> output={output_path} / resized={new_size}"
)
except Exception:
output_path.unlink(missing_ok=True)
raise
return output_path