Png To P2d Converter

The repo is live at github.com/yourname/png2p2d. Bug reports, feature requests, and additional color-mapping presets are very welcome.

python convert.py input.png -m mapping.json -o output.p2d

Example mapping.json:


  "#ff0000":  "layer": "Spawn", "type": "point" ,
  "#0000ff":  "layer": "Walls", "type": "polygon"

In modern graphics pipelines, assets often undergo a transformation from "Authoring Formats" (PNG, PSD) to "Runtime Formats." The P2D format (Proprietary 2-Dimensional Data) is designed to store geometric hulls, sprite atlases, and animation keyframes in a binary stream, allowing for O(1) memory access times. png to p2d converter

Install Pillow:

pip install Pillow

If existing tools do not fit your workflow, you can write a custom converter in under 150 lines of Python. Below is a simplified example using Pillow and opencv-python. The repo is live at github

import cv2
import json
import numpy as np
from PIL import Image

def png_to_p2d(input_path, output_path, tolerance=1.0): # Load PNG and extract alpha img = Image.open(input_path).convert("RGBA") alpha = np.array(img)[:,:,3]

# Create binary mask (alpha > 10)
mask = (alpha > 10).astype(np.uint8) * 255
# Find contours
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Simplify polygon
epsilon = tolerance * cv2.arcLength(contours[0], True)
simplified = cv2.approxPolyDP(contours[0], epsilon, True)
# Convert to list format
vertices = simplified.reshape(-1, 2).tolist()
# Create P2D data structure
p2d_data = 
    "version": 1,
    "source": input_path,
    "width": img.width,
    "height": img.height,
    "polygons": ["type": "collision", "vertices": vertices]
# Write to file
with open(output_path, 'w') as f:
    json.dump(p2d_data, f, indent=2)
print(f"Converted input_path -> output_path with len(vertices) vertices")

The resulting hero.p2d might look something like this (JSON example): Example mapping


  "version": 1,
  "source": "hero.png",
  "width": 128,
  "height": 128,
  "polygons": [
"type": "collision",
      "vertices": [[12,0], [116,0], [128,12], [128,116], [116,128], [12,128], [0,116], [0,12]]
],
  "simplification_tolerance": 1.2

The converter utilizes a low-level image library (e.g., libpng or stb_image) to decompress the PNG into a raw byte buffer. The system isolates the Alpha channel into a 2D array.