-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_jpeg_to_webp.py
More file actions
executable file
·83 lines (67 loc) · 2.45 KB
/
convert_jpeg_to_webp.py
File metadata and controls
executable file
·83 lines (67 loc) · 2.45 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
#!/usr/bin/env python3
"""Convert all .jpg/.jpeg images under a directory to .webp using Pillow."""
from __future__ import annotations
import argparse
from pathlib import Path
from PIL import Image
def convert_jpegs(root: Path, quality: int, delete_original: bool, skip_existing: bool) -> tuple[int, int, int]:
converted = 0
skipped = 0
deleted = 0
print(delete_original, skip_existing)
for jpg_path in root.rglob("*"):
if not jpg_path.is_file():
continue
if jpg_path.suffix.lower() not in {".jpg", ".jpeg"}:
continue
webp_path = jpg_path.with_suffix(".webp")
if skip_existing and webp_path.exists():
skipped += 1
#print(f"Skipping existing file: {webp_path}")
else:
with Image.open(jpg_path) as img:
# Ensure consistent mode for WebP
if img.mode not in ("RGB", "RGBA"):
img = img.convert("RGB")
img.save(webp_path, format="WEBP", quality=quality)
converted += 1
if delete_original:
#print(f"Deleting original file: {jpg_path}")
jpg_path.unlink()
deleted += 1
return converted, skipped, deleted
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Convert .jpg/.jpeg images to .webp in a directory tree.")
parser.add_argument(
"root",
nargs="?",
help="Root directory to traverse",
)
parser.add_argument("--quality", type=int, default=80, help="WebP quality (default: 80)")
parser.add_argument(
"--delete-original",
action="store_true",
help="Delete .jpg/.jpeg files after successful conversion",
)
parser.add_argument(
"--overwrite",
action="store_true",
help="Overwrite existing .webp files (default: skip existing)",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
root = Path(args.root)
if not root.exists() or not root.is_dir():
raise SystemExit(f"Root directory not found: {root}")
converted, skipped, deleted = convert_jpegs(
root=root,
quality=args.quality,
delete_original=args.delete_original,
skip_existing=not args.overwrite,
)
print(f"Converted: {converted}")
print(f"Skipped (existing .webp): {skipped}")
print(f"Deleted original .jpg/.jpeg: {deleted}")
if __name__ == "__main__":
main()