akh1r0ck commited on
Commit
eb815ed
·
1 Parent(s): 6642fea

update collage size

Browse files
Files changed (1) hide show
  1. app.py +52 -10
app.py CHANGED
@@ -4,13 +4,27 @@ import numpy as np
4
  import random
5
  from PIL import Image
6
 
7
- def resize_with_fixed_aspect_ratio(image, new_height):
8
- width, height = image.shape[:2]
9
- aspect_ratio = width / height
10
- new_width = int(new_height * aspect_ratio)
 
 
 
 
 
 
 
 
 
 
 
11
  new_size = (new_height, new_width)
 
 
12
 
13
  resized_image = cv2.resize(image, dsize=new_size, interpolation=cv2.INTER_CUBIC)
 
14
 
15
  return resized_image
16
 
@@ -19,11 +33,32 @@ def show_image(obj_image, bg_image):
19
 
20
 
21
  bg_height, bg_width = bg_image.shape[:2]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- obj_vs_bg = random.randint(2, 5)
24
 
25
 
26
- obj_image = resize_with_fixed_aspect_ratio(obj_image, int(bg_height/obj_vs_bg))
 
 
27
 
28
 
29
  height, width = obj_image.shape[:2]
@@ -33,11 +68,18 @@ def show_image(obj_image, bg_image):
33
  max_x = bg_width-width
34
  max_y = bg_height-height
35
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- move_x = random.randint(0, max_x)
38
- move_y = random.randint(0, max_y)
39
-
40
- result_image[move_y:move_y+height, move_x:move_x+width, :] = obj_image
41
 
42
  result_image = np.where(result_image>0, result_image, bg_image)
43
 
 
4
  import random
5
  from PIL import Image
6
 
7
+ def resize_with_fixed_aspect_ratio(image, new_metrics, size_metrics="height"):
8
+ height, width = image.shape[:2]
9
+ # print("new_metrics in functions", new_metrics)
10
+ # print("before:", image.shape)
11
+ if size_metrics=="height":
12
+ aspect_ratio = width / height
13
+ # print("aspect_ratio: ", aspect_ratio)
14
+ new_height = new_metrics
15
+ new_width = int(new_height * aspect_ratio)
16
+
17
+ elif size_metrics=="width":
18
+ aspect_ratio = height / width
19
+ new_width = new_metrics
20
+ new_height = int(new_width / aspect_ratio)
21
+
22
  new_size = (new_height, new_width)
23
+ new_size = (new_width, new_height)
24
+ # print("new_size: ", new_size)
25
 
26
  resized_image = cv2.resize(image, dsize=new_size, interpolation=cv2.INTER_CUBIC)
27
+ # print("after:", resized_image.shape)
28
 
29
  return resized_image
30
 
 
33
 
34
 
35
  bg_height, bg_width = bg_image.shape[:2]
36
+ obj_height, obj_width = obj_image.shape[:2]
37
+
38
+ if obj_height>obj_width:
39
+ size_metrics="height"
40
+ if bg_height>obj_height:
41
+ resize_min_rate = bg_height/obj_height
42
+ new_metrics = bg_height
43
+ else:
44
+ resize_min_rate = obj_height/bg_height
45
+ new_metrics = obj_height
46
+
47
+
48
+ elif obj_height<obj_width:
49
+ size_metrics="width"
50
+ if bg_height>obj_height:
51
+ resize_min_rate = bg_width/obj_width
52
+ new_metrics = bg_width
53
+ else:
54
+ resize_min_rate = obj_width/bg_width
55
+ new_metrics = obj_width
56
 
 
57
 
58
 
59
+ obj_vs_bg = random.uniform(resize_min_rate, 5)
60
+
61
+ obj_image = resize_with_fixed_aspect_ratio(obj_image, int(new_metrics/obj_vs_bg), size_metrics)
62
 
63
 
64
  height, width = obj_image.shape[:2]
 
68
  max_x = bg_width-width
69
  max_y = bg_height-height
70
 
71
+ try:
72
+ move_x = random.randint(0, max_x)
73
+ move_y = random.randint(0, max_y)
74
+ result_image[move_y:move_y+height, move_x:move_x+width, :] = obj_image
75
+ except:
76
+ print("bg_width.shape: ", bg_image.shape)
77
+ print("obj_image.shape: ", obj_image.shape)
78
+ print("max_y: ", max_y)
79
+ print("max_x: ", max_x)
80
+ print("obj_vs_bg", obj_vs_bg)
81
+ print("resize_min_rate", resize_min_rate)
82
 
 
 
 
 
83
 
84
  result_image = np.where(result_image>0, result_image, bg_image)
85