Convert all SVG images to PNGs.
(images_dir: Path, converted_image_dir: Path)
| 23 | |
| 24 | |
| 25 | def convert_images(images_dir: Path, converted_image_dir: Path) -> None: |
| 26 | """Convert all SVG images to PNGs.""" |
| 27 | |
| 28 | if not converted_image_dir.exists(): |
| 29 | converted_image_dir.mkdir() |
| 30 | |
| 31 | for source_file in images_dir.glob("*"): |
| 32 | if source_file.suffix == ".svg": |
| 33 | dest_file = converted_image_dir / source_file.with_suffix(".png").name |
| 34 | |
| 35 | try: |
| 36 | subprocess.check_output( |
| 37 | [ |
| 38 | "inkscape", |
| 39 | f"--export-filename={dest_file.as_posix()}", |
| 40 | source_file.as_posix(), |
| 41 | ], |
| 42 | stderr=subprocess.STDOUT, |
| 43 | ) |
| 44 | except FileNotFoundError: |
| 45 | raise RuntimeError( |
| 46 | f"failed to convert {source_file.name} to {dest_file.name}: " |
| 47 | "inkscape not installed" |
| 48 | ) |
| 49 | except CalledProcessError as e: |
| 50 | raise RuntimeError( |
| 51 | f"failed to convert {source_file.name} to {dest_file.name}: " |
| 52 | f"inkscape failed: {e.output.decode()}" |
| 53 | ) |
| 54 | else: |
| 55 | shutil.copy(source_file, converted_image_dir / source_file.name) |
| 56 | |
| 57 | return converted_image_dir |
| 58 | |
| 59 | |
| 60 | @dataclass |