Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from PIL import Image
|
4 |
+
import random
|
5 |
+
|
6 |
+
# Function to calculate aspect ratio
|
7 |
+
def calculate_aspect_ratio(image_path):
|
8 |
+
with Image.open(image_path) as img:
|
9 |
+
width, height = img.size
|
10 |
+
gcd = lambda a, b: a if not b else gcd(b, a % b)
|
11 |
+
divisor = gcd(width, height)
|
12 |
+
return width // divisor, height // divisor
|
13 |
+
|
14 |
+
# Function to classify room types based on aspect ratio
|
15 |
+
def classify_room(aspect_ratio):
|
16 |
+
if aspect_ratio == (16, 9) or aspect_ratio == (19, 6):
|
17 |
+
return "Hallway"
|
18 |
+
elif aspect_ratio[0] < 8 and aspect_ratio[1] < 8:
|
19 |
+
return "Door"
|
20 |
+
else:
|
21 |
+
return "Room"
|
22 |
+
|
23 |
+
# Function to generate a layout
|
24 |
+
def generate_dungeon_map(image_files):
|
25 |
+
dungeon = {"Hallways": [], "Doors": [], "Rooms": []}
|
26 |
+
|
27 |
+
for img_file in image_files:
|
28 |
+
img_path = os.path.join(map_dir, img_file)
|
29 |
+
aspect_ratio = calculate_aspect_ratio(img_path)
|
30 |
+
room_type = classify_room(aspect_ratio)
|
31 |
+
|
32 |
+
if room_type == "Hallway":
|
33 |
+
dungeon["Hallways"].append(img_file)
|
34 |
+
elif room_type == "Door":
|
35 |
+
dungeon["Doors"].append(img_file)
|
36 |
+
else:
|
37 |
+
dungeon["Rooms"].append(img_file)
|
38 |
+
|
39 |
+
# Add logic to connect rooms, hallways, and doors
|
40 |
+
layout = {}
|
41 |
+
if dungeon["Hallways"]:
|
42 |
+
for i, hallway in enumerate(dungeon["Hallways"]):
|
43 |
+
layout[f"Hallway {i+1}"] = {"type": "Hallway", "connected_to": []}
|
44 |
+
if dungeon["Rooms"]:
|
45 |
+
room = dungeon["Rooms"].pop(0)
|
46 |
+
layout[f"Hallway {i+1}"]["connected_to"].append(room)
|
47 |
+
if dungeon["Doors"]:
|
48 |
+
door = dungeon["Doors"].pop(0)
|
49 |
+
layout[f"Hallway {i+1}"]["connected_to"].append(door)
|
50 |
+
|
51 |
+
return layout, dungeon
|
52 |
+
|
53 |
+
# Streamlit App
|
54 |
+
st.title("Dynamic Dungeon Map Generator")
|
55 |
+
st.write("Automatically generates dungeon maps by analyzing PNG images in a directory.")
|
56 |
+
|
57 |
+
# Directory for images
|
58 |
+
map_dir = "maps" # Replace with the actual directory path
|
59 |
+
|
60 |
+
# Scan the directory for .png files
|
61 |
+
if os.path.exists(map_dir):
|
62 |
+
image_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
|
63 |
+
|
64 |
+
if image_files:
|
65 |
+
st.subheader("Classified Rooms")
|
66 |
+
layout, dungeon = generate_dungeon_map(image_files)
|
67 |
+
|
68 |
+
st.write("### Hallways")
|
69 |
+
st.write(", ".join(dungeon["Hallways"]) if dungeon["Hallways"] else "No hallways found.")
|
70 |
+
|
71 |
+
st.write("### Doors")
|
72 |
+
st.write(", ".join(dungeon["Doors"]) if dungeon["Doors"] else "No doors found.")
|
73 |
+
|
74 |
+
st.write("### Rooms")
|
75 |
+
st.write(", ".join(dungeon["Rooms"]) if dungeon["Rooms"] else "No rooms found.")
|
76 |
+
|
77 |
+
st.subheader("Dungeon Layout")
|
78 |
+
for key, value in layout.items():
|
79 |
+
st.write(f"{key}: {value}")
|
80 |
+
|
81 |
+
else:
|
82 |
+
st.write("No PNG files found in the specified directory.")
|
83 |
+
else:
|
84 |
+
st.write("The specified directory does not exist. Please check the path.")
|
85 |
+
|
86 |
+
# File upload option
|
87 |
+
st.sidebar.title("Options")
|
88 |
+
uploaded_file = st.sidebar.file_uploader("Upload a PNG file", type="png")
|
89 |
+
|
90 |
+
if uploaded_file:
|
91 |
+
# Save uploaded file to the directory
|
92 |
+
with open(os.path.join(map_dir, uploaded_file.name), "wb") as f:
|
93 |
+
f.write(uploaded_file.getbuffer())
|
94 |
+
st.sidebar.success("File uploaded successfully! Refresh the app to include it in the dungeon map.")
|
95 |
+
|
96 |
+
# Placeholder for visualization
|
97 |
+
st.subheader("Visual Representation")
|
98 |
+
st.write("A feature to visualize the map will be added soon.")
|