comdoleger commited on
Commit
dc396dd
1 Parent(s): b650cc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -3
app.py CHANGED
@@ -28,7 +28,29 @@ def image_to_base64(image):
28
  # Encode the binary data to a base64 string
29
  base64_image = base64.b64encode(binary_image_data).decode("utf-8")
30
  return base64_image
31
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  def process(data, api, api_key):
34
 
@@ -91,8 +113,9 @@ def resize_to_fit(max_size, original_size):
91
  return new_width, new_height
92
 
93
 
94
- def process_generate(fore, prompt, intensity, mode, refprompt, bg):
95
-
 
96
  size = fore.size
97
  image_width = size[0]
98
  image_height = size[1]
 
28
  # Encode the binary data to a base64 string
29
  base64_image = base64.b64encode(binary_image_data).decode("utf-8")
30
  return base64_image
31
+
32
+ def create_square_image(image):
33
+ """
34
+ Create a new square image with the side length equal to the largest dimension
35
+ of the original image and paste the original image at the center on a transparent canvas.
36
+
37
+ :param image: A PIL image.
38
+ :return: A new square PIL image.
39
+ """
40
+ original_width, original_height = image.size
41
+ new_side_length = max(original_width, original_height)
42
+
43
+ # Create a new square image with a transparent background
44
+ new_image = Image.new("RGBA", (new_side_length, new_side_length), (255, 255, 255, 0))
45
+
46
+ # Calculate the position to paste the original image on the new square canvas
47
+ paste_x = (new_side_length - original_width) // 2
48
+ paste_y = (new_side_length - original_height) // 2
49
+
50
+ # Paste the original image onto the new square canvas using the alpha channel as a mask
51
+ new_image.paste(image, (paste_x, paste_y), image)
52
+
53
+ return new_image
54
 
55
  def process(data, api, api_key):
56
 
 
113
  return new_width, new_height
114
 
115
 
116
+ def process_generate(fore, prompt, intensity, mode, refprompt, bg):
117
+
118
+ fore = create_square_image(fore)
119
  size = fore.size
120
  image_width = size[0]
121
  image_height = size[1]