Spaces:
Sleeping
Sleeping
Update backup1.app.py
Browse files- backup1.app.py +109 -59
backup1.app.py
CHANGED
|
@@ -1,68 +1,118 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
|
|
|
| 3 |
import random
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
else:
|
| 30 |
-
st.write("
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
st.write("""
|
| 36 |
-
- **V20**: Dining Hall
|
| 37 |
-
- **V21**: Storage Room
|
| 38 |
-
- **V22**: Hallway
|
| 39 |
-
- **V23**: Guard Room
|
| 40 |
-
- **V24**: Training Room
|
| 41 |
-
- **V25**: Treasury
|
| 42 |
-
- **V26**: Throne Room
|
| 43 |
-
- **V27**: Passageway
|
| 44 |
-
- **V28**: Crypt
|
| 45 |
-
- **V29**: Prison
|
| 46 |
-
- **V30**: Exit Hall
|
| 47 |
-
- **V31**: Barracks
|
| 48 |
-
- **V32**: Workshop
|
| 49 |
-
- **V33**: Common Room
|
| 50 |
-
- **V34**: Secret Chamber
|
| 51 |
-
- **V35**: Armory
|
| 52 |
-
- **V36**: Ritual Room
|
| 53 |
-
""")
|
| 54 |
-
|
| 55 |
-
# Add an option to dynamically generate maps
|
| 56 |
-
st.subheader("Generate a New Dungeon")
|
| 57 |
-
if st.button("Create New Map"):
|
| 58 |
-
st.write("**Feature coming soon!** Dynamically generating new maps using procedural algorithms.")
|
| 59 |
-
|
| 60 |
-
# Sidebar for user options
|
| 61 |
st.sidebar.title("Options")
|
| 62 |
-
st.sidebar.
|
| 63 |
-
uploaded_file = st.sidebar.file_uploader("Upload a .png map file", type="png")
|
| 64 |
|
| 65 |
if uploaded_file:
|
|
|
|
| 66 |
with open(os.path.join(map_dir, uploaded_file.name), "wb") as f:
|
| 67 |
f.write(uploaded_file.getbuffer())
|
| 68 |
-
st.sidebar.success("File uploaded successfully!")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
import random
|
| 5 |
+
|
| 6 |
+
# Function to arrange images dynamically
|
| 7 |
+
def arrange_images(image_files, output_path="dungeon_layout.png"):
|
| 8 |
+
if not image_files:
|
| 9 |
+
return None
|
| 10 |
+
|
| 11 |
+
# Initialize layout
|
| 12 |
+
positions = [] # Keeps track of image positions (x1, y1, x2, y2)
|
| 13 |
+
canvas_size = (3000, 3000) # Large canvas for positioning
|
| 14 |
+
canvas = Image.new("RGBA", canvas_size, "white")
|
| 15 |
+
draw = ImageDraw.Draw(canvas)
|
| 16 |
+
|
| 17 |
+
def get_center(pos):
|
| 18 |
+
"""Calculate center of a bounding box (x1, y1, x2, y2)."""
|
| 19 |
+
return ((pos[0] + pos[2]) // 2, (pos[1] + pos[3]) // 2)
|
| 20 |
+
|
| 21 |
+
def does_overlap(new_box, existing_boxes):
|
| 22 |
+
"""Check if a new bounding box overlaps any existing boxes."""
|
| 23 |
+
for box in existing_boxes:
|
| 24 |
+
if (
|
| 25 |
+
new_box[0] < box[2]
|
| 26 |
+
and new_box[2] > box[0]
|
| 27 |
+
and new_box[1] < box[3]
|
| 28 |
+
and new_box[3] > box[1]
|
| 29 |
+
):
|
| 30 |
+
return True
|
| 31 |
+
return False
|
| 32 |
+
|
| 33 |
+
# Place the first image at the center of the canvas
|
| 34 |
+
first_img_path = os.path.join(map_dir, image_files[0])
|
| 35 |
+
with Image.open(first_img_path) as img:
|
| 36 |
+
width, height = img.size
|
| 37 |
+
x1 = (canvas_size[0] - width) // 2
|
| 38 |
+
y1 = (canvas_size[1] - height) // 2
|
| 39 |
+
x2, y2 = x1 + width, y1 + height
|
| 40 |
+
positions.append((x1, y1, x2, y2))
|
| 41 |
+
canvas.paste(img, (x1, y1))
|
| 42 |
+
|
| 43 |
+
# Place remaining images
|
| 44 |
+
for img_file in image_files[1:]:
|
| 45 |
+
placed = False
|
| 46 |
+
img_path = os.path.join(map_dir, img_file)
|
| 47 |
+
|
| 48 |
+
with Image.open(img_path) as img:
|
| 49 |
+
width, height = img.size
|
| 50 |
+
|
| 51 |
+
while not placed:
|
| 52 |
+
# Choose a random existing image to connect to
|
| 53 |
+
target_box = random.choice(positions)
|
| 54 |
+
target_center = get_center(target_box)
|
| 55 |
+
|
| 56 |
+
# Randomly choose a side of the target image
|
| 57 |
+
side = random.choice(["top", "bottom", "left", "right"])
|
| 58 |
+
|
| 59 |
+
# Calculate the new image's position based on the chosen side
|
| 60 |
+
if side == "top":
|
| 61 |
+
x1 = target_center[0] - width // 2
|
| 62 |
+
y1 = target_box[1] - height
|
| 63 |
+
elif side == "bottom":
|
| 64 |
+
x1 = target_center[0] - width // 2
|
| 65 |
+
y1 = target_box[3]
|
| 66 |
+
elif side == "left":
|
| 67 |
+
x1 = target_box[0] - width
|
| 68 |
+
y1 = target_center[1] - height // 2
|
| 69 |
+
elif side == "right":
|
| 70 |
+
x1 = target_box[2]
|
| 71 |
+
y1 = target_center[1] - height // 2
|
| 72 |
+
|
| 73 |
+
x2, y2 = x1 + width, y1 + height
|
| 74 |
+
|
| 75 |
+
# Check for overlap
|
| 76 |
+
if not does_overlap((x1, y1, x2, y2), positions):
|
| 77 |
+
# Place the image
|
| 78 |
+
positions.append((x1, y1, x2, y2))
|
| 79 |
+
canvas.paste(img, (x1, y1))
|
| 80 |
+
placed = True
|
| 81 |
+
|
| 82 |
+
# Draw a connecting line
|
| 83 |
+
new_center = get_center((x1, y1, x2, y2))
|
| 84 |
+
draw.line([target_center, new_center], fill="black", width=5)
|
| 85 |
+
|
| 86 |
+
canvas.save(output_path)
|
| 87 |
+
return output_path
|
| 88 |
+
|
| 89 |
+
# Streamlit App
|
| 90 |
+
st.title("Dynamic Dungeon Map Generator")
|
| 91 |
+
st.write("Automatically generates dungeon maps by arranging PNG images dynamically.")
|
| 92 |
+
|
| 93 |
+
# Directory for images
|
| 94 |
+
map_dir = "." # Top-level directory
|
| 95 |
+
|
| 96 |
+
# Scan the directory for .png files
|
| 97 |
+
image_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
|
| 98 |
+
|
| 99 |
+
if image_files:
|
| 100 |
+
# Add "Create Map" button
|
| 101 |
+
if st.button("🗺️ Create Map"):
|
| 102 |
+
layout_image = arrange_images(image_files)
|
| 103 |
+
if layout_image:
|
| 104 |
+
st.image(layout_image, caption="Generated Dungeon Map Layout")
|
| 105 |
else:
|
| 106 |
+
st.write("Failed to generate a map. Please check the images in the directory.")
|
| 107 |
+
else:
|
| 108 |
+
st.write("No PNG files found in the top-level directory.")
|
| 109 |
+
|
| 110 |
+
# Sidebar for file upload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
st.sidebar.title("Options")
|
| 112 |
+
uploaded_file = st.sidebar.file_uploader("Upload a PNG file", type="png")
|
|
|
|
| 113 |
|
| 114 |
if uploaded_file:
|
| 115 |
+
# Save uploaded file to the directory
|
| 116 |
with open(os.path.join(map_dir, uploaded_file.name), "wb") as f:
|
| 117 |
f.write(uploaded_file.getbuffer())
|
| 118 |
+
st.sidebar.success("File uploaded successfully! Refresh the app to include it in the dungeon map.")
|