multimodalart HF Staff commited on
Commit
b784b02
·
verified ·
1 Parent(s): 8263fc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -17
app.py CHANGED
@@ -47,31 +47,28 @@ pipe = StableDiffusionXLFillPipeline.from_pretrained(
47
 
48
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
49
 
50
- def resize_and_pad(image, target_size):
51
- aspect_ratio = image.width / image.height
52
- target_aspect_ratio = target_size[0] / target_size[1]
53
-
54
- if aspect_ratio > target_aspect_ratio:
55
- # Image is wider than the target ratio
56
- new_width = target_size[0]
57
- new_height = int(new_width / aspect_ratio)
58
- else:
59
- # Image is taller than the target ratio
60
- new_height = target_size[1]
61
- new_width = int(new_height * aspect_ratio)
62
-
63
- resized_image = image.resize((new_width, new_height), Image.LANCZOS)
64
 
 
65
  new_image = Image.new('RGB', target_size, (255, 255, 255))
66
 
67
- paste_x = (target_size[0] - new_width) // 2
 
68
  paste_y = (target_size[1] - new_height) // 2
69
 
 
70
  new_image.paste(resized_image, (paste_x, paste_y))
71
 
 
72
  mask = Image.new('L', target_size, 255)
73
  mask_draw = ImageDraw.Draw(mask)
74
- mask_draw.rectangle([paste_x, paste_y, paste_x + new_width, paste_y + new_height], fill=0)
75
 
76
  return new_image, mask
77
 
@@ -80,7 +77,7 @@ def infer(image, model_selection, width, height, overlap_width, num_inference_st
80
  target_size = (width, height)
81
 
82
  if expand_mode:
83
- background, mask = resize_and_pad(image, target_size)
84
  cnet_image = background.copy()
85
  cnet_image.paste(0, (0, 0), mask)
86
  else:
 
47
 
48
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
49
 
50
+ def resize_and_pad(image, target_size, resize_width=512):
51
+ # Calculate the new height to maintain aspect ratio
52
+ aspect_ratio = image.height / image.width
53
+ new_height = int(resize_width * aspect_ratio)
54
+
55
+ # Resize the image
56
+ resized_image = image.resize((resize_width, new_height), Image.LANCZOS)
 
 
 
 
 
 
 
57
 
58
+ # Create a new white image with the target size
59
  new_image = Image.new('RGB', target_size, (255, 255, 255))
60
 
61
+ # Calculate position to paste the resized image (center it)
62
+ paste_x = (target_size[0] - resize_width) // 2
63
  paste_y = (target_size[1] - new_height) // 2
64
 
65
+ # Paste the resized image onto the new image
66
  new_image.paste(resized_image, (paste_x, paste_y))
67
 
68
+ # Create a mask
69
  mask = Image.new('L', target_size, 255)
70
  mask_draw = ImageDraw.Draw(mask)
71
+ mask_draw.rectangle([paste_x, paste_y, paste_x + resize_width, paste_y + new_height], fill=0)
72
 
73
  return new_image, mask
74
 
 
77
  target_size = (width, height)
78
 
79
  if expand_mode:
80
+ background, mask = resize_and_pad(image, target_size, resize_width=512)
81
  cnet_image = background.copy()
82
  cnet_image.paste(0, (0, 0), mask)
83
  else: