awacke1's picture
Create app.py
389d620 verified
raw
history blame
3.44 kB
import streamlit as st
import os
from PIL import Image
import random
# Function to calculate aspect ratio
def calculate_aspect_ratio(image_path):
with Image.open(image_path) as img:
width, height = img.size
gcd = lambda a, b: a if not b else gcd(b, a % b)
divisor = gcd(width, height)
return width // divisor, height // divisor
# Function to classify room types based on aspect ratio
def classify_room(aspect_ratio):
if aspect_ratio == (16, 9) or aspect_ratio == (19, 6):
return "Hallway"
elif aspect_ratio[0] < 8 and aspect_ratio[1] < 8:
return "Door"
else:
return "Room"
# Function to generate a layout
def generate_dungeon_map(image_files):
dungeon = {"Hallways": [], "Doors": [], "Rooms": []}
for img_file in image_files:
img_path = os.path.join(map_dir, img_file)
aspect_ratio = calculate_aspect_ratio(img_path)
room_type = classify_room(aspect_ratio)
if room_type == "Hallway":
dungeon["Hallways"].append(img_file)
elif room_type == "Door":
dungeon["Doors"].append(img_file)
else:
dungeon["Rooms"].append(img_file)
# Add logic to connect rooms, hallways, and doors
layout = {}
if dungeon["Hallways"]:
for i, hallway in enumerate(dungeon["Hallways"]):
layout[f"Hallway {i+1}"] = {"type": "Hallway", "connected_to": []}
if dungeon["Rooms"]:
room = dungeon["Rooms"].pop(0)
layout[f"Hallway {i+1}"]["connected_to"].append(room)
if dungeon["Doors"]:
door = dungeon["Doors"].pop(0)
layout[f"Hallway {i+1}"]["connected_to"].append(door)
return layout, dungeon
# Streamlit App
st.title("Dynamic Dungeon Map Generator")
st.write("Automatically generates dungeon maps by analyzing PNG images in a directory.")
# Directory for images
map_dir = "maps" # Replace with the actual directory path
# Scan the directory for .png files
if os.path.exists(map_dir):
image_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
if image_files:
st.subheader("Classified Rooms")
layout, dungeon = generate_dungeon_map(image_files)
st.write("### Hallways")
st.write(", ".join(dungeon["Hallways"]) if dungeon["Hallways"] else "No hallways found.")
st.write("### Doors")
st.write(", ".join(dungeon["Doors"]) if dungeon["Doors"] else "No doors found.")
st.write("### Rooms")
st.write(", ".join(dungeon["Rooms"]) if dungeon["Rooms"] else "No rooms found.")
st.subheader("Dungeon Layout")
for key, value in layout.items():
st.write(f"{key}: {value}")
else:
st.write("No PNG files found in the specified directory.")
else:
st.write("The specified directory does not exist. Please check the path.")
# File upload option
st.sidebar.title("Options")
uploaded_file = st.sidebar.file_uploader("Upload a PNG file", type="png")
if uploaded_file:
# Save uploaded file to the directory
with open(os.path.join(map_dir, uploaded_file.name), "wb") as f:
f.write(uploaded_file.getbuffer())
st.sidebar.success("File uploaded successfully! Refresh the app to include it in the dungeon map.")
# Placeholder for visualization
st.subheader("Visual Representation")
st.write("A feature to visualize the map will be added soon.")