2022-10-11 18:08:33 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
from subprocess import run
|
2024-01-15 12:04:47 +00:00
|
|
|
from typing import List, Optional, Tuple
|
2022-10-11 18:08:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
resolutions = [64, 128, 256, 512, 1024, 2048]
|
|
|
|
|
|
|
|
for svg in Path("public/").glob("**/*.svg"):
|
|
|
|
if "square" in str(svg):
|
|
|
|
for resolution in resolutions:
|
|
|
|
convert_to_image(
|
|
|
|
source=svg,
|
|
|
|
destination=svg.with_name(f"{svg.stem}-{resolution}.png"),
|
2024-01-15 12:04:47 +00:00
|
|
|
resolution=(resolution, resolution),
|
2022-10-11 18:08:33 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
convert_to_image(source=svg, destination=svg.with_suffix(".png"))
|
|
|
|
|
|
|
|
|
|
|
|
def convert_to_image(
|
|
|
|
source: Path,
|
|
|
|
destination: Path,
|
2024-01-15 12:04:47 +00:00
|
|
|
resolution: Optional[Tuple[int, int]] = None,
|
2022-10-11 18:08:33 +00:00
|
|
|
) -> None:
|
2024-01-15 12:04:04 +00:00
|
|
|
"""Convert an SVG to an image with `inkscape`."""
|
2022-10-11 18:08:33 +00:00
|
|
|
inkscape_args: List[str] = ["inkscape", str(source), "-o", str(destination)]
|
|
|
|
|
2024-01-15 12:04:47 +00:00
|
|
|
if resolution is not None:
|
|
|
|
inkscape_args.extend(["-w", str(resolution[0]), "-h", str(resolution[1])])
|
2022-10-11 18:08:33 +00:00
|
|
|
|
|
|
|
run(args=inkscape_args)
|
|
|
|
mat2_image(destination)
|
|
|
|
|
|
|
|
|
|
|
|
def mat2_image(image: Path) -> None:
|
2024-01-15 12:04:04 +00:00
|
|
|
"""Run `mat2` on the image path."""
|
2022-10-11 18:08:33 +00:00
|
|
|
run(args=["mat2", "--inplace", image])
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|