|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | import argparse |
| 3 | +import sys |
3 | 4 | from pathlib import Path |
4 | 5 | from PIL import Image |
5 | 6 | import cairosvg |
6 | 7 |
|
7 | 8 | # Take build directory as argument to save generated C files and PNG files. |
8 | 9 | parser = argparse.ArgumentParser(description="Convert image files to C structs.") |
9 | | -parser.add_argument("dest", help="Destination build folder for PNG files.") |
| 10 | +parser.add_argument( |
| 11 | + "--dest", default="build", help="Destination build folder for generated files." |
| 12 | +) |
| 13 | +parser.add_argument( |
| 14 | + "--list-sources", action="store_true", help="Only list sources files." |
| 15 | +) |
10 | 16 | args = parser.parse_args() |
11 | 17 |
|
| 18 | +media_dir = Path(__file__).parent.relative_to(Path.cwd()) / "media" |
| 19 | + |
| 20 | +# Collect all SVG and image files in media_dir (svg, png, bmp, jpg), including subfolders. |
| 21 | +media_images_sources = ( |
| 22 | + list(media_dir.rglob("*.png")) |
| 23 | + + list(media_dir.rglob("*.bmp")) |
| 24 | + + list(media_dir.rglob("*.jpg")) |
| 25 | +) |
| 26 | +media_svg_sources = list(media_dir.rglob("*.svg")) |
| 27 | +media_sources = media_images_sources + media_svg_sources |
| 28 | +media_sources.sort() |
| 29 | + |
| 30 | +if args.list_sources: |
| 31 | + for m in media_sources: |
| 32 | + print(m) |
| 33 | + sys.exit(0) |
| 34 | + |
12 | 35 | build_dir = Path(args.dest) |
13 | 36 | build_dir.mkdir(parents=True, exist_ok=True) |
14 | | -media_dir = Path(__file__).parent / "media" |
| 37 | + |
| 38 | +media_gen = [] |
15 | 39 |
|
16 | 40 | # Convert all SVG files in media_dir to PNG and save in build_dir if not already present. |
17 | | -svg_files = media_dir.rglob("*.svg") |
18 | | -for svg in svg_files: |
| 41 | +for svg in media_svg_sources: |
19 | 42 | png = svg.with_suffix(".png").name |
20 | 43 | png_path = build_dir / png |
| 44 | + media_gen.append(png_path) |
21 | 45 | if png_path.exists() and png_path.stat().st_mtime >= svg.stat().st_mtime: |
22 | 46 | continue |
23 | 47 | with open(svg, "rb") as svg_file: |
24 | 48 | png_bytes = cairosvg.svg2png(file_obj=svg_file) |
25 | 49 | with open(png_path, "wb") as out_png: |
26 | 50 | out_png.write(png_bytes) |
27 | 51 |
|
28 | | -# Collect all image files in media_dir (png, bmp, jpg) and build_dir (png), including subfolders. |
29 | | -media_images = ( |
30 | | - list(media_dir.rglob("*.png")) |
31 | | - + list(media_dir.rglob("*.bmp")) |
32 | | - + list(media_dir.rglob("*.jpg")) |
33 | | - + list(build_dir.rglob("*.png")) |
34 | | -) |
35 | | - |
36 | 52 |
|
37 | 53 | # Convert rgba to monochrome, treating fully transparent pixels as white. |
38 | 54 | def is_black(r, g, b, a): |
@@ -60,6 +76,7 @@ def image_to_8bit_map(img): |
60 | 76 |
|
61 | 77 |
|
62 | 78 | # Process each image. |
| 79 | +media_images = media_images_sources + media_gen |
63 | 80 | results = {} |
64 | 81 | for img_path in media_images: |
65 | 82 | with Image.open(img_path) as img: |
@@ -129,3 +146,8 @@ def image_to_8bit_map(img): |
129 | 146 | f.write( |
130 | 147 | "MP_DEFINE_CONST_DICT(pb_type_image_attributes_dict, pb_type_image_attributes_dict_table);" |
131 | 148 | ) |
| 149 | + |
| 150 | +with open(build_dir / "media_sources_gen.mk", "w") as f: |
| 151 | + f.write("MEDIA_SOURCES_GEN = \\\n ") |
| 152 | + f.write(" \\\n ".join(str(m) for m in media_sources)) |
| 153 | + f.write("\n") |
0 commit comments