awacke1 commited on
Commit
a09d912
1 Parent(s): dc59a70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -15
app.py CHANGED
@@ -4,7 +4,6 @@ from PIL import Image
4
  import random
5
  from io import BytesIO
6
  import datetime
7
- import base64
8
 
9
  # Adjust Streamlit layout to wide mode
10
  st.set_page_config(layout="wide")
@@ -12,10 +11,11 @@ st.set_page_config(layout="wide")
12
  # Function to arrange images dynamically
13
  def arrange_images(image_files, canvas_size=(3000, 3000)):
14
  if not image_files:
15
- return None
16
 
17
  positions = [] # Keeps track of image positions (x1, y1, x2, y2)
18
  canvas = Image.new("RGBA", canvas_size, "white")
 
19
 
20
  def get_center(pos):
21
  """Calculate center of a bounding box (x1, y1, x2, y2)."""
@@ -42,9 +42,10 @@ def arrange_images(image_files, canvas_size=(3000, 3000)):
42
  x2, y2 = x1 + width, y1 + height
43
  positions.append((x1, y1, x2, y2))
44
  canvas.paste(img, (x1, y1))
 
45
 
46
  # Place remaining images
47
- for img_file in image_files[1:]:
48
  placed = False
49
  img_path = os.path.join(map_dir, img_file)
50
 
@@ -74,21 +75,16 @@ def arrange_images(image_files, canvas_size=(3000, 3000)):
74
  if not does_overlap((x1, y1, x2, y2), positions):
75
  positions.append((x1, y1, x2, y2))
76
  canvas.paste(img, (x1, y1))
 
77
  placed = True
78
 
79
  buffer = BytesIO()
80
  canvas.save(buffer, format="PNG")
81
  buffer.seek(0)
82
- return buffer, canvas
83
 
84
- # Function to create a base64 download link
85
- def create_download_link(image_bytes, filename):
86
- b64 = base64.b64encode(image_bytes.getvalue()).decode()
87
- href = f'<a href="data:image/png;base64,{b64}" download="{filename}">📥 Download {filename}</a>'
88
- return href
89
-
90
- # Streamlit App
91
- st.title("Dynamic Dungeon Map Generator")
92
 
93
  # Directory for images
94
  map_dir = "."
@@ -99,29 +95,38 @@ if "layout_image" not in st.session_state:
99
  st.session_state["layout_image"] = None
100
  if "canvas" not in st.session_state:
101
  st.session_state["canvas"] = None
 
 
102
 
103
  # Generate map if layout_image is empty
104
  if st.session_state["layout_image"] is None:
105
  image_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
106
  if image_files:
107
- layout_image, canvas = arrange_images(image_files, canvas_size=(canvas_size, canvas_size))
108
  st.session_state["layout_image"] = layout_image
109
  st.session_state["canvas"] = canvas
 
110
 
111
  # Sidebar Controls
112
- st.sidebar.title("Map Controls")
113
  if st.sidebar.button("💾 Save Map"):
114
  now = datetime.datetime.now()
115
  filename = f"dungeon_map_{now.strftime('%Y%m%d_%H%M%S')}.png"
116
  st.session_state["canvas"].save(filename)
117
  st.sidebar.success(f"Map saved as {filename}")
118
 
 
 
 
 
 
 
119
  if st.sidebar.button("🗺️ Regenerate Map"):
120
  image_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
121
  if image_files:
122
- layout_image, canvas = arrange_images(image_files, canvas_size=(canvas_size, canvas_size))
123
  st.session_state["layout_image"] = layout_image
124
  st.session_state["canvas"] = canvas
 
125
  st.rerun()
126
 
127
  # Zoom Controls
 
4
  import random
5
  from io import BytesIO
6
  import datetime
 
7
 
8
  # Adjust Streamlit layout to wide mode
9
  st.set_page_config(layout="wide")
 
11
  # Function to arrange images dynamically
12
  def arrange_images(image_files, canvas_size=(3000, 3000)):
13
  if not image_files:
14
+ return None, []
15
 
16
  positions = [] # Keeps track of image positions (x1, y1, x2, y2)
17
  canvas = Image.new("RGBA", canvas_size, "white")
18
+ room_details = [] # To store room layout details
19
 
20
  def get_center(pos):
21
  """Calculate center of a bounding box (x1, y1, x2, y2)."""
 
42
  x2, y2 = x1 + width, y1 + height
43
  positions.append((x1, y1, x2, y2))
44
  canvas.paste(img, (x1, y1))
45
+ room_details.append(f"Room 1: {image_files[0]} at center")
46
 
47
  # Place remaining images
48
+ for idx, img_file in enumerate(image_files[1:], start=2):
49
  placed = False
50
  img_path = os.path.join(map_dir, img_file)
51
 
 
75
  if not does_overlap((x1, y1, x2, y2), positions):
76
  positions.append((x1, y1, x2, y2))
77
  canvas.paste(img, (x1, y1))
78
+ room_details.append(f"Room {idx}: {img_file} at ({x1}, {y1})")
79
  placed = True
80
 
81
  buffer = BytesIO()
82
  canvas.save(buffer, format="PNG")
83
  buffer.seek(0)
84
+ return buffer, canvas, room_details
85
 
86
+ # Sidebar Title
87
+ st.sidebar.markdown("#### 🏰 Dynamic Dungeon Map Generator")
 
 
 
 
 
 
88
 
89
  # Directory for images
90
  map_dir = "."
 
95
  st.session_state["layout_image"] = None
96
  if "canvas" not in st.session_state:
97
  st.session_state["canvas"] = None
98
+ if "room_details" not in st.session_state:
99
+ st.session_state["room_details"] = []
100
 
101
  # Generate map if layout_image is empty
102
  if st.session_state["layout_image"] is None:
103
  image_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
104
  if image_files:
105
+ layout_image, canvas, room_details = arrange_images(image_files, canvas_size=(canvas_size, canvas_size))
106
  st.session_state["layout_image"] = layout_image
107
  st.session_state["canvas"] = canvas
108
+ st.session_state["room_details"] = room_details
109
 
110
  # Sidebar Controls
 
111
  if st.sidebar.button("💾 Save Map"):
112
  now = datetime.datetime.now()
113
  filename = f"dungeon_map_{now.strftime('%Y%m%d_%H%M%S')}.png"
114
  st.session_state["canvas"].save(filename)
115
  st.sidebar.success(f"Map saved as {filename}")
116
 
117
+ # Save room details as text
118
+ layout_filename = f"room_layout_{now.strftime('%Y%m%d_%H%M%S')}.txt"
119
+ with open(layout_filename, "w") as f:
120
+ f.write("\n".join(st.session_state["room_details"]))
121
+ st.sidebar.success(f"Room layout saved as {layout_filename}")
122
+
123
  if st.sidebar.button("🗺️ Regenerate Map"):
124
  image_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
125
  if image_files:
126
+ layout_image, canvas, room_details = arrange_images(image_files, canvas_size=(canvas_size, canvas_size))
127
  st.session_state["layout_image"] = layout_image
128
  st.session_state["canvas"] = canvas
129
+ st.session_state["room_details"] = room_details
130
  st.rerun()
131
 
132
  # Zoom Controls