-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathconvert_heic_to_jpeg.py
More file actions
162 lines (125 loc) · 4.45 KB
/
Copy pathconvert_heic_to_jpeg.py
File metadata and controls
162 lines (125 loc) · 4.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "pillow>=11.0.0",
# "pillow-heif>=0.22.0",
# ]
# ///
"""Convert HEIC and HEIF images to JPEG format."""
from __future__ import annotations
import argparse
from dataclasses import dataclass
from pathlib import Path
from PIL import Image
import pillow_heif
# Register HEIF support with Pillow before opening files.
pillow_heif.register_heif_opener()
SUPPORTED_EXTENSIONS = {".heic", ".heif"}
@dataclass(frozen=True)
class ConversionResult:
"""Capture the outcome for a single source image."""
source_path: Path
output_path: Path
status: str
def is_heif_file(path: Path) -> bool:
"""Return whether the path has a supported HEIF extension."""
return path.suffix.lower() in SUPPORTED_EXTENSIONS
def build_output_path(
source_path: Path, input_root: Path, output_dir: Path | None
) -> Path:
"""Build the destination JPEG path for a source HEIF image."""
if output_dir is None:
return source_path.with_suffix(".jpg")
if input_root.is_file():
return output_dir / f"{source_path.stem}.jpg"
relative_path = source_path.relative_to(input_root).with_suffix(".jpg")
return output_dir / relative_path
def save_as_jpeg(source_path: Path, output_path: Path, quality: int) -> None:
"""Open a HEIF image and save it as JPEG."""
output_path.parent.mkdir(parents=True, exist_ok=True)
with Image.open(source_path) as image:
if image.mode not in ("RGB", "L"):
image = image.convert("RGB")
image.save(output_path, "JPEG", quality=quality)
def convert_heic_to_jpeg(
source_path: Path,
input_root: Path,
output_dir: Path | None = None,
quality: int = 90,
overwrite: bool = False,
) -> ConversionResult:
"""Convert one HEIF image to JPEG."""
output_path = build_output_path(source_path, input_root, output_dir)
if output_path.exists() and not overwrite:
return ConversionResult(
source_path=source_path, output_path=output_path, status="skipped"
)
save_as_jpeg(source_path, output_path, quality)
return ConversionResult(
source_path=source_path, output_path=output_path, status="converted"
)
def gather_source_files(input_path: Path) -> list[Path]:
"""Return all HEIF images under the given file or directory."""
if input_path.is_file():
if not is_heif_file(input_path):
raise ValueError(f"Unsupported input file: {input_path}")
return [input_path]
return sorted(
path for path in input_path.rglob("*") if path.is_file() and is_heif_file(path)
)
def parse_args() -> argparse.Namespace:
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(description="Convert HEIC or HEIF images to JPEG")
parser.add_argument("input", type=Path, help="Input HEIC/HEIF file or directory")
parser.add_argument(
"-o",
"--output-dir",
type=Path,
default=None,
help="Optional output directory for JPEG files",
)
parser.add_argument(
"-q",
"--quality",
type=int,
default=90,
help="JPEG quality from 1 to 100 (default: 90)",
)
parser.add_argument(
"--overwrite",
action="store_true",
help="Replace existing JPEG files instead of skipping them",
)
return parser.parse_args()
def main() -> int:
"""Run the CLI conversion workflow."""
args = parse_args()
if not args.input.exists():
raise FileNotFoundError(f"Input path does not exist: {args.input}")
if not 1 <= args.quality <= 100:
raise ValueError("Quality must be between 1 and 100")
source_files = gather_source_files(args.input)
if not source_files:
print("No HEIC or HEIF files found.")
return 0
converted_count = 0
skipped_count = 0
for source_path in source_files:
result = convert_heic_to_jpeg(
source_path=source_path,
input_root=args.input,
output_dir=args.output_dir,
quality=args.quality,
overwrite=args.overwrite,
)
print(f"{result.status:9} {result.source_path} -> {result.output_path}")
if result.status == "converted":
converted_count += 1
else:
skipped_count += 1
print(
f"Summary: converted={converted_count} skipped={skipped_count} total={len(source_files)}"
)
return 0
if __name__ == "__main__":
raise SystemExit(main())