|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +from pathlib import Path |
| 4 | +from PIL import Image |
| 5 | +import cairosvg |
| 6 | + |
| 7 | +# Take build directory as argument to save generated C files and PNG files. |
| 8 | +parser = argparse.ArgumentParser(description="Convert SVG files to PNG.") |
| 9 | +parser.add_argument("dest", help="Destination build folder for PNG files.") |
| 10 | +args = parser.parse_args() |
| 11 | + |
| 12 | +build_dir = Path(args.dest) |
| 13 | +build_dir.mkdir(parents=True, exist_ok=True) |
| 14 | +media_dir = Path(__file__).parent / "media" |
| 15 | + |
| 16 | +# 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: |
| 19 | + png = svg.with_suffix(".png").name |
| 20 | + png_path = build_dir / png |
| 21 | + if png_path.exists(): |
| 22 | + continue |
| 23 | + with open(svg, "rb") as svg_file: |
| 24 | + png_bytes = cairosvg.svg2png(file_obj=svg_file) |
| 25 | + with open(png_path, "wb") as out_png: |
| 26 | + out_png.write(png_bytes) |
| 27 | + |
| 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 | + |
| 37 | +# Convert rgba to monochrome, treating fully transparent pixels as white. |
| 38 | +def is_black(r, g, b, a): |
| 39 | + if a == 0: |
| 40 | + return 0 |
| 41 | + return 1 if (r + g + b) < (128 * 3) else 0 |
| 42 | + |
| 43 | + |
| 44 | +def image_to_8bit_map(img): |
| 45 | + img = img.convert("RGBA") |
| 46 | + width, height = img.size |
| 47 | + pixels = img.load() |
| 48 | + mono = [is_black(*pixels[x, y]) for y in range(height) for x in range(width)] |
| 49 | + |
| 50 | + # go in chunks of 8 pixels and pack into a byte |
| 51 | + data = [] |
| 52 | + for i in range(0, len(mono), 8): |
| 53 | + byte = 0 |
| 54 | + for j in range(8): |
| 55 | + if i + j < len(mono): |
| 56 | + byte |= mono[i + j] << (7 - j) |
| 57 | + data.append(byte) |
| 58 | + |
| 59 | + return width, height, bytes(data) |
| 60 | + |
| 61 | + |
| 62 | +# Process each image. |
| 63 | +results = {} |
| 64 | +for img_path in media_images: |
| 65 | + with Image.open(img_path) as img: |
| 66 | + name = Path(img_path.name).stem |
| 67 | + width, height, bin_data = image_to_8bit_map(img) |
| 68 | + results[name] = (width, height, bin_data) |
| 69 | + |
| 70 | + |
| 71 | +externs = "" |
| 72 | +structs = "" |
| 73 | +qstrtab = "" |
| 74 | + |
| 75 | +for name in sorted(results): |
| 76 | + width, height, bin_data = results[name] |
| 77 | + |
| 78 | + # Parse bytes for printing. |
| 79 | + bytes_per_line = 12 |
| 80 | + lines = [] |
| 81 | + for i in range(0, len(bin_data), bytes_per_line): |
| 82 | + chunk = bin_data[i : i + bytes_per_line] |
| 83 | + line = " " + ", ".join(f"0x{val:02x}" for val in chunk) |
| 84 | + lines.append(line) |
| 85 | + data_literal = ",\n".join(lines) + "," |
| 86 | + |
| 87 | + # Printed C structs. |
| 88 | + structs += f"static const uint8_t {name}_data[] = {{\n{data_literal}\n}};\n\n" |
| 89 | + structs += ( |
| 90 | + f"const pbio_image_monochrome_t pbio_image_media_{name} = {{\n" |
| 91 | + f" .width = {width},\n" |
| 92 | + f" .height = {height},\n" |
| 93 | + f" .data = {name}_data,\n" |
| 94 | + f"}};\n" |
| 95 | + ) |
| 96 | + |
| 97 | + # Printed header and QSTR table entries. |
| 98 | + externs += f"extern const pbio_image_monochrome_t pbio_image_media_{name};\n\n" |
| 99 | + qstrtab += f" {{ MP_ROM_QSTR(MP_QSTR_{name.upper()}), MP_ROM_PTR(&pbio_image_media_{name}) }},\n" |
| 100 | + |
| 101 | + |
| 102 | +HEADER = """// SPDX-License-Identifier: MIT |
| 103 | +//Copyright (c) 2025 The Pybricks Authors |
| 104 | +
|
| 105 | +#include <pbio/image.h> |
| 106 | +""" |
| 107 | + |
| 108 | +with open(build_dir / "pbio_image_media.c", "w") as f: |
| 109 | + f.write(HEADER) |
| 110 | + f.write('#include "pbio_image_media.h"\n\n') |
| 111 | + f.write(structs) |
| 112 | + |
| 113 | +with open(build_dir / "pbio_image_media.h", "w") as f: |
| 114 | + f.write(HEADER) |
| 115 | + f.write("#ifndef _PBIO_IMAGE_MEDIA_H_\n") |
| 116 | + f.write("#define _PBIO_IMAGE_MEDIA_H_\n\n") |
| 117 | + f.write(externs) |
| 118 | + f.write("#endif // _PBIO_IMAGE_MEDIA_H_\n") |
| 119 | + |
| 120 | +with open(build_dir / "pb_type_image_attributes.c", "w") as f: |
| 121 | + f.write(HEADER) |
| 122 | + f.write('#include "pbio_image_media.h"\n\n') |
| 123 | + f.write("#include <py/obj.h>\n\n") |
| 124 | + f.write( |
| 125 | + "static const mp_rom_map_elem_t pb_type_image_attributes_dict_table[] = {\n" |
| 126 | + ) |
| 127 | + f.write(qstrtab) |
| 128 | + f.write("};\n") |
| 129 | + f.write( |
| 130 | + "MP_DEFINE_CONST_DICT(pb_type_image_attributes_dict, pb_type_image_attributes_dict_table);" |
| 131 | + ) |
0 commit comments