Add new PNG images for clean and preview outputs
- 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.
@@ -0,0 +1,49 @@
|
||||
#!/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()
|
||||
@@ -0,0 +1,53 @@
|
||||
#!/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 a JSON RGBA matrix back into a PNG image."
|
||||
)
|
||||
parser.add_argument("input_json", type=Path, help="Source JSON path")
|
||||
parser.add_argument("output_png", type=Path, help="Destination PNG path")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def load_matrix(json_path: Path) -> tuple[int, int, list[tuple[int, int, int, int]]]:
|
||||
data = json.loads(json_path.read_text(encoding="utf-8"))
|
||||
width = int(data["width"])
|
||||
height = int(data["height"])
|
||||
rows = data["pixels"]
|
||||
|
||||
if len(rows) != height:
|
||||
raise ValueError(f"Expected {height} rows, found {len(rows)}")
|
||||
|
||||
flat_pixels: list[tuple[int, int, int, int]] = []
|
||||
for row_index, row in enumerate(rows):
|
||||
if len(row) != width:
|
||||
raise ValueError(
|
||||
f"Expected row {row_index} to contain {width} pixels, found {len(row)}"
|
||||
)
|
||||
for pixel in row:
|
||||
if len(pixel) != 4:
|
||||
raise ValueError("Each pixel must contain 4 RGBA channels")
|
||||
flat_pixels.append(tuple(int(channel) for channel in pixel))
|
||||
|
||||
return width, height, flat_pixels
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = parse_args()
|
||||
width, height, pixels = load_matrix(args.input_json)
|
||||
image = Image.new("RGBA", (width, height))
|
||||
image.putdata(pixels)
|
||||
args.output_png.parent.mkdir(parents=True, exist_ok=True)
|
||||
image.save(args.output_png, format="PNG")
|
||||
print(f"Wrote {args.output_png} with {width}x{height} RGBA pixels")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
After Width: | Height: | Size: 421 B |
|
After Width: | Height: | Size: 804 B |
|
After Width: | Height: | Size: 815 B |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 885 B |
|
After Width: | Height: | Size: 256 KiB |
|
After Width: | Height: | Size: 62 KiB |
|
After Width: | Height: | Size: 781 B |
|
After Width: | Height: | Size: 2.8 KiB |
|
After Width: | Height: | Size: 4.2 KiB |