02202e4d3d
- Added `image_clean.png` to the output directory for the clean image representation. - Added `image_clean_preview.png` for the preview of the clean image. - Introduced `image_svg_clean.png` for the SVG clean image representation.
49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
parser = argparse.ArgumentParser(
|
|
description="Convert an image file into a JSON matrix of RGBA pixels."
|
|
)
|
|
parser.add_argument("input_image", type=Path, help="Source image path")
|
|
parser.add_argument("output_json", type=Path, help="Destination JSON path")
|
|
return parser.parse_args()
|
|
|
|
|
|
def image_to_matrix(image_path: Path) -> dict:
|
|
with Image.open(image_path) as image:
|
|
rgba_image = image.convert("RGBA")
|
|
width, height = rgba_image.size
|
|
pixel_map = rgba_image.load()
|
|
rows = [
|
|
[list(pixel_map[x, y]) for x in range(width)]
|
|
for y in range(height)
|
|
]
|
|
|
|
return {
|
|
"source": image_path.name,
|
|
"width": width,
|
|
"height": height,
|
|
"mode": "RGBA",
|
|
"pixels": rows,
|
|
}
|
|
|
|
|
|
def main() -> None:
|
|
args = parse_args()
|
|
data = image_to_matrix(args.input_image)
|
|
args.output_json.parent.mkdir(parents=True, exist_ok=True)
|
|
args.output_json.write_text(json.dumps(data, indent=2), encoding="utf-8")
|
|
print(
|
|
f"Wrote {args.output_json} with {data['width']}x{data['height']} RGBA pixels"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |