Datasets:
v2: parser_v5 IR (regions, spike dirs, on/off footprints, entity names), legend v2 (boss door D), boss-entity mmm_boss config, +48K levels, official author-disjoint splits, ~25 new columns
c875799 verified Download mmm_decode.py from elliottash/megaman-maker-levels: direct link, hf CLI and curl.
- Browser
- Download file 3.05 kB
-
https://huggingface.co/datasets/elliottash/megaman-maker-levels/resolve/main/mmm_decode.py
- Command line
-
hf download hf://datasets/elliottash/megaman-maker-levels/mmm_decode.py
-
curl -L -o mmm_decode.py https://huggingface.co/datasets/elliottash/megaman-maker-levels/resolve/main/mmm_decode.py
3.05 kB
| """Tiny helper for the Mega Man Maker HuggingFace dataset. | |
| This file is copied into the exported dataset repo so users can do: | |
| from datasets import load_dataset | |
| from mmm_decode import load_ir, ascii_png | |
| row = load_dataset("your-org/mega-man-maker-levels", split="train")[0] | |
| print("\\n".join(row["level_ascii"])) | |
| ir = load_ir(row) | |
| ascii_png(row).save("level.png") | |
| The dataset row already carries a ready-to-train `level_ascii` list of | |
| strings, so most users do not need to understand the original `.mmlv` | |
| format or the full IR schema. | |
| """ | |
| from __future__ import annotations | |
| import gzip | |
| import json | |
| from io import BytesIO | |
| from typing import Any | |
| from PIL import Image, ImageDraw | |
| PALETTE: dict[str, tuple[int, int, int]] = { | |
| "-": (248, 248, 248), # empty/passable | |
| "X": (76, 80, 92), # solid | |
| "=": (120, 103, 65), # semisolid | |
| "H": (214, 155, 64), # ladder | |
| "W": (74, 143, 210), # water | |
| "*": (210, 68, 57), # hazard | |
| "?": (236, 189, 62), # item block | |
| "o": (255, 230, 92), # pickup | |
| "E": (196, 72, 117), # enemy | |
| "B": (126, 58, 170), # boss | |
| "D": (158, 108, 196), # boss door (legend v2) | |
| "G": (76, 175, 80), # goal | |
| "P": (66, 135, 245), # spawn | |
| "g": (119, 119, 119), # gizmo | |
| "~": (166, 216, 232), # slippery solid (legend v2) | |
| ".": (180, 180, 180), # unknown | |
| } | |
| def load_ir(row: dict[str, Any]) -> dict[str, Any]: | |
| """Return the full parsed IR JSON from a dataset row.""" | |
| blob = row.get("ir_json_gz") | |
| if blob is None: | |
| raise ValueError("row does not contain ir_json_gz") | |
| return json.loads(gzip.decompress(blob)) | |
| def level_ascii(row: dict[str, Any]) -> list[str]: | |
| """Return the row's MarioGPT-style ASCII grid.""" | |
| rows = row.get("level_ascii") | |
| if not rows: | |
| raise ValueError("row does not contain level_ascii") | |
| return [str(r) for r in rows] | |
| def ascii_image(rows: list[str], *, tile_size: int = 6) -> Image.Image: | |
| """Render ASCII rows to a Pillow RGB image.""" | |
| if not rows: | |
| raise ValueError("empty ASCII level") | |
| width = max(len(r) for r in rows) | |
| height = len(rows) | |
| img = Image.new("RGB", (width * tile_size, height * tile_size), PALETTE["-"]) | |
| draw = ImageDraw.Draw(img) | |
| for y, row in enumerate(rows): | |
| for x, ch in enumerate(row): | |
| color = PALETTE.get(ch, PALETTE["."]) | |
| draw.rectangle( | |
| (x * tile_size, y * tile_size, | |
| (x + 1) * tile_size - 1, (y + 1) * tile_size - 1), | |
| fill=color, | |
| ) | |
| return img | |
| def ascii_png(row: dict[str, Any], *, tile_size: int = 6) -> Image.Image: | |
| """Render a dataset row's `level_ascii` field as a Pillow Image.""" | |
| return ascii_image(level_ascii(row), tile_size=tile_size) | |
| def ascii_png_bytes(row: dict[str, Any], *, tile_size: int = 6) -> bytes: | |
| """Render a dataset row's ASCII grid to PNG bytes.""" | |
| buf = BytesIO() | |
| ascii_png(row, tile_size=tile_size).save(buf, format="PNG", optimize=True) | |
| return buf.getvalue() | |