awacke1 commited on
Commit
55b2419
·
verified ·
1 Parent(s): 377a3b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -46
app.py CHANGED
@@ -3,66 +3,92 @@ import os
3
  from PIL import Image, ImageDraw
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 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 variables for layout
20
- positions = [] # Keeps track of image positions
21
- canvas_size = (3000, 3000) # Adjust based on your needs
22
  canvas = Image.new("RGBA", canvas_size, "white")
23
  draw = ImageDraw.Draw(canvas)
24
-
25
- for img_file in image_files:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # Randomly decide horizontal or vertical placement
31
- if random.choice(["horizontal", "vertical"]) == "horizontal":
32
- x_center = random.randint(width // 2, canvas_size[0] - width // 2)
33
- y_center = canvas_size[1] // 2
34
- else:
35
- x_center = canvas_size[0] // 2
36
- y_center = random.randint(height // 2, canvas_size[1] - height // 2)
37
-
38
- # Calculate position
39
- x1 = x_center - width // 2
40
- y1 = y_center - height // 2
41
- x2 = x1 + width
42
- y2 = y1 + height
43
-
44
- # Ensure no overlap with existing images
45
- overlap = any(
46
- x1 < pos[2] and x2 > pos[0] and y1 < pos[3] and y2 > pos[1]
47
- for pos in positions
48
- )
49
-
50
- if not overlap:
51
- positions.append((x1, y1, x2, y2))
52
- canvas.paste(img, (x1, y1))
53
-
54
- # Draw connection line
55
- if len(positions) > 1:
56
- prev_x, prev_y = (positions[-2][0] + positions[-2][2]) // 2, (positions[-2][1] + positions[-2][3]) // 2
57
- curr_x, curr_y = (x1 + x2) // 2, (y1 + y2) // 2
58
- draw.line([(prev_x, prev_y), (curr_x, curr_y)], fill="black", width=5)
 
 
 
 
 
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 analyzing PNG images in a top-level directory.")
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