Skip to content

Instantly share code, notes, and snippets.

@perrygeo
Created January 22, 2025 23:04
Show Gist options
  • Save perrygeo/75bf585b3e2b28de7a7474b45ec6d52e to your computer and use it in GitHub Desktop.
Save perrygeo/75bf585b3e2b28de7a7474b45ec6d52e to your computer and use it in GitHub Desktop.
Serve blank tiles with the z/x/y address labelled
import io
from fastapi import FastAPI, Response
from PIL import Image, ImageDraw, ImageFont
app = FastAPI()
@app.get("/tiles/{z}/{x}/{y}.png")
async def get_debug_tile(z: int, x: int, y: int):
img = Image.new("RGB", (256, 256), "white")
draw = ImageDraw.Draw(img)
draw.rectangle([0, 0, 255, 255], outline="black", width=1)
font = ImageFont.load_default()
text = f"{z}/{x}/{y}"
# Calculate text size and position for centering
bbox = draw.textbbox((0, 0), text, font=font)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
x_pos = (256 - text_width) // 2
y_pos = (256 - text_height) // 2
# Draw it
draw.text((x_pos, y_pos), text, fill="black", font=font)
# Save to bytes
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format="PNG")
img_byte_arr.seek(0)
return Response(content=img_byte_arr.getvalue(), media_type="image/png")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8080)
@perrygeo
Copy link
Author

image image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment