Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,66 +3,92 @@ import os
|
|
| 3 |
from PIL import Image, ImageDraw
|
| 4 |
import random
|
| 5 |
|
| 6 |
-
# Function to
|
| 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 randomly position images while ensuring no overlap
|
| 15 |
def arrange_images(image_files, output_path="dungeon_layout.png"):
|
| 16 |
if not image_files:
|
| 17 |
return None
|
| 18 |
-
|
| 19 |
-
# Initialize
|
| 20 |
-
positions = [] # Keeps track of image positions
|
| 21 |
-
canvas_size = (3000, 3000) #
|
| 22 |
canvas = Image.new("RGBA", canvas_size, "white")
|
| 23 |
draw = ImageDraw.Draw(canvas)
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
img_path = os.path.join(map_dir, img_file)
|
|
|
|
| 27 |
with Image.open(img_path) as img:
|
| 28 |
width, height = img.size
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
#
|
| 55 |
-
if
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
canvas.save(output_path)
|
| 61 |
return output_path
|
| 62 |
|
| 63 |
# Streamlit App
|
| 64 |
st.title("Dynamic Dungeon Map Generator")
|
| 65 |
-
st.write("Automatically generates dungeon maps by
|
| 66 |
|
| 67 |
# Directory for images
|
| 68 |
map_dir = "." # Top-level directory
|
|
|
|
| 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
|