rootglitch commited on
Commit
cdf05c6
·
1 Parent(s): 53d9ab7

Resize the input image to square

Browse files
Files changed (1) hide show
  1. app.py +23 -0
app.py CHANGED
@@ -433,6 +433,27 @@ def resize_image(image, scaling_factor=1):
433
  int(image.height * scaling_factor)))
434
  return image
435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436
  def encode_image(image):
437
  buffer = BytesIO()
438
  image.save(buffer, format="PNG")
@@ -527,6 +548,8 @@ def blend_details(input_image, relit_image, masked_image, scaling_factor=1):
527
  @spaces.GPU
528
  def generate_image(input_img, prompt):
529
 
 
 
530
  ai_gen_image = generate_ai_bg(input_img, prompt)
531
 
532
  mask_input_image = run_grounded_sam(input_img)
 
433
  int(image.height * scaling_factor)))
434
  return image
435
 
436
+ def resize_to_square(image, size=1024):
437
+
438
+ # img = Image.open(image_path).convert("RGBA")
439
+ img = image
440
+
441
+ # Resize while maintaining aspect ratio
442
+ img.thumbnail((size, size), Image.LANCZOS)
443
+
444
+ # Create a transparent square canvas
445
+ square_img = Image.new("RGBA", (size, size), (0, 0, 0, 0))
446
+
447
+ # Calculate the position to paste the resized image (centered)
448
+ x_offset = (size - img.width) // 2
449
+ y_offset = (size - img.height) // 2
450
+
451
+ # Paste the resized image onto the square canvas
452
+ square_img.paste(img, (x_offset, y_offset), img)
453
+
454
+ return square_img
455
+
456
+
457
  def encode_image(image):
458
  buffer = BytesIO()
459
  image.save(buffer, format="PNG")
 
548
  @spaces.GPU
549
  def generate_image(input_img, prompt):
550
 
551
+ input_img = resize_to_square(input_img)
552
+
553
  ai_gen_image = generate_ai_bg(input_img, prompt)
554
 
555
  mask_input_image = run_grounded_sam(input_img)