awacke1 commited on
Commit
becda8d
1 Parent(s): fb8aa70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -12
app.py CHANGED
@@ -1,18 +1,17 @@
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)."""
@@ -79,12 +78,11 @@ def arrange_images(image_files, output_path="dungeon_layout.png"):
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")
@@ -93,15 +91,38 @@ st.write("Automatically generates dungeon maps by arranging PNG images dynamical
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:
 
1
  import streamlit as st
2
  import os
3
+ from PIL import Image
4
  import random
5
+ from io import BytesIO
6
 
7
  # Function to arrange images dynamically
8
+ def arrange_images(image_files, canvas_size=(3000, 3000)):
9
  if not image_files:
10
  return None
11
 
12
  # Initialize layout
13
  positions = [] # Keeps track of image positions (x1, y1, x2, y2)
 
14
  canvas = Image.new("RGBA", canvas_size, "white")
 
15
 
16
  def get_center(pos):
17
  """Calculate center of a bounding box (x1, y1, x2, y2)."""
 
78
  canvas.paste(img, (x1, y1))
79
  placed = True
80
 
81
+ # Save canvas to memory
82
+ buffer = BytesIO()
83
+ canvas.save(buffer, format="PNG")
84
+ buffer.seek(0)
85
+ return buffer
 
86
 
87
  # Streamlit App
88
  st.title("Dynamic Dungeon Map Generator")
 
91
  # Directory for images
92
  map_dir = "." # Top-level directory
93
 
94
+ # Canvas size and scroll offsets
95
+ canvas_size = 3000
96
+ scroll_offset = {"x": 0, "y": 0}
97
+
98
+ # WASD Controls for scrolling
99
+ st.sidebar.title("Scroll Map")
100
+ scroll_buttons = st.sidebar.columns(3)
101
+ if scroll_buttons[0].button("⬅️"):
102
+ scroll_offset["x"] -= 100
103
+ if scroll_buttons[1].button("⬆️"):
104
+ scroll_offset["y"] -= 100
105
+ if scroll_buttons[2].button("➡️"):
106
+ scroll_offset["x"] += 100
107
+ if st.sidebar.button("⬇️"):
108
+ scroll_offset["y"] += 100
109
+
110
  # Scan the directory for .png files
111
  image_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
112
 
113
  if image_files:
114
  # Add "Create Map" button
115
  if st.button("🗺️ Create Map"):
116
+ scroll_offset = {"x": 0, "y": 0} # Reset scroll offset
117
+ layout_image = arrange_images(image_files, canvas_size=(canvas_size, canvas_size))
118
  if layout_image:
119
+ st.image(
120
+ layout_image,
121
+ caption="Generated Dungeon Map Layout",
122
+ use_column_width=True,
123
+ output_format="PNG",
124
+ clamp=True
125
+ )
126
  else:
127
  st.write("Failed to generate a map. Please check the images in the directory.")
128
  else: