| 1 | """Convert HEIC to PNG via pillow-heif.""" |
| 2 | from __future__ import annotations |
| 3 | import sys |
| 4 | from pathlib import Path |
| 5 | |
| 6 | from PIL import Image |
| 7 | import pillow_heif |
| 8 | |
| 9 | pillow_heif.register_heif_opener() |
| 10 | |
| 11 | def main() -> int: |
| 12 | if len(sys.argv) < 2: |
| 13 | print("usage: heic_convert.py <dir or file>...") |
| 14 | return 2 |
| 15 | for arg in sys.argv[1:]: |
| 16 | p = Path(arg) |
| 17 | files = [p] if p.is_file() else sorted(p.glob("*.HEIC")) + sorted(p.glob("*.heic")) |
| 18 | for f in files: |
| 19 | out = f.with_suffix(".png") |
| 20 | img = Image.open(f) |
| 21 | img.save(out, "PNG") |
| 22 | print(f"{f.name} -> {out.name} ({img.size[0]}x{img.size[1]})") |
| 23 | return 0 |
| 24 | |
| 25 | if __name__ == "__main__": |
| 26 | sys.exit(main()) |