Skip to content

Instantly share code, notes, and snippets.

@jsoctocat
Last active March 9, 2026 07:21
Show Gist options
  • Select an option

  • Save jsoctocat/7ce0eecf0f7615bcc14497d3262a0e63 to your computer and use it in GitHub Desktop.

Select an option

Save jsoctocat/7ce0eecf0f7615bcc14497d3262a0e63 to your computer and use it in GitHub Desktop.
import os
from PIL import Image
# Configuration
tile_size = 128
output_size = 256
input_dir = "./minimap_data_processed"
output_dir = f"./stitched_{input_dir.strip('./').strip("_processed")}"
os.makedirs(output_dir, exist_ok=True)
# Maximum tile coordinates
min_x, max_x = 0,0 # x in range(min_x, max_x, 2)
min_y, max_y = 0,0 # y in range(min_y, max_y, 2)
for file in os.listdir(input_dir):
base = file.rsplit(".", 1)[0] # remove extension
_, x, y = base.split("_")
x, y = int(x), int(y)
min_x = min(min_x, x)
max_x = max(max_x, x)
min_y = min(min_y, y)
max_y = max(max_y, y)
x_steps = len(range(min_x, max_x + 2, 2))
y_steps = len(range(min_y, max_y + 2, 2))
total_steps = x_steps * y_steps
done = 0
saved = 0
# Process in 2x2 steps (0, 2, 4...)
for x in range(min_x, max_x + 2, 2):
for y in range(min_y, max_y + 2, 2):
# Change the coordinates to start from 0, 0
col = (x - min_x)
row_raw = (y - min_y)
max_row = (max_y - min_y) # Number of rows (0-based top = max_y)
row = max_row - row_raw # Flip: high y -> low row index (top of image)
# Create a blank 256x256 canvas (RGBA for transparency)
canvas = Image.new("RGBA", (output_size, output_size), (0, 0, 0, 0))
has_any_tile = False
# Check the 4 tiles for this 256x256 block
offsets = [(0, 0), (1, 0), (0, 1), (1, 1)]
for dx, dy in offsets:
tile_name = f"rader_{x + dx}_{y + dy}.png"
tile_path = os.path.join(input_dir, tile_name)
if os.path.exists(tile_path):
img = Image.open(tile_path).convert("RGBA")
# dy = 0 -> bottom half (lower y = more south)
# dy = 1 -> top half (higher y = north)
paste_y = (1-dy) * tile_size
canvas.paste(img, (dx * tile_size, paste_y))
has_any_tile = True
done += 1
# Save only if at least one tile existed
if has_any_tile:
out_col = col // 2
out_row = row // 2
canvas.save(f"{output_dir}/tile_{out_col}_{out_row}.webp", "WEBP", quality=82, method=6)
saved +=1
print(f"\rProgress: {done}/{total_steps} ({done / total_steps * 100:.1f}%)", end="", flush=True)
print()
print("Batch processing complete.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment