Spaces:
Running
on
Zero
Running
on
Zero
fixes resize alignment with vertical images
Browse files
app.py
CHANGED
|
@@ -58,19 +58,15 @@ def infer(image, width, height, overlap_width, num_inference_steps, resize_optio
|
|
| 58 |
target_size = (width, height)
|
| 59 |
overlap = overlap_width
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
new_height = int(source.height * scale_factor)
|
| 66 |
-
source = source.resize((new_width, new_height), Image.LANCZOS)
|
| 67 |
-
|
| 68 |
-
if source.width > target_size[0] or source.height > target_size[1]:
|
| 69 |
-
scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)
|
| 70 |
-
new_width = int(source.width * scale_factor)
|
| 71 |
-
new_height = int(source.height * scale_factor)
|
| 72 |
-
source = source.resize((new_width, new_height), Image.LANCZOS)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
if resize_option == "Full":
|
| 75 |
resize_size = max(source.width, source.height)
|
| 76 |
elif resize_option == "1/2":
|
|
@@ -87,29 +83,28 @@ def infer(image, width, height, overlap_width, num_inference_steps, resize_optio
|
|
| 87 |
new_height = int(resize_size * aspect_ratio)
|
| 88 |
source = source.resize((new_width, new_height), Image.LANCZOS)
|
| 89 |
|
| 90 |
-
if not can_expand(source.width, source.height, target_size[0], target_size[1], alignment):
|
| 91 |
-
alignment = "Middle"
|
| 92 |
-
|
| 93 |
# Calculate margins based on alignment
|
| 94 |
if alignment == "Middle":
|
| 95 |
-
margin_x = (target_size[0] -
|
| 96 |
-
margin_y = (target_size[1] -
|
| 97 |
elif alignment == "Left":
|
| 98 |
margin_x = 0
|
| 99 |
-
margin_y = (target_size[1] -
|
| 100 |
elif alignment == "Right":
|
| 101 |
-
margin_x = target_size[0] -
|
| 102 |
-
margin_y = (target_size[1] -
|
| 103 |
elif alignment == "Top":
|
| 104 |
-
margin_x = (target_size[0] -
|
| 105 |
margin_y = 0
|
| 106 |
elif alignment == "Bottom":
|
| 107 |
-
margin_x = (target_size[0] -
|
| 108 |
-
margin_y = target_size[1] -
|
| 109 |
|
|
|
|
| 110 |
background = Image.new('RGB', target_size, (255, 255, 255))
|
| 111 |
background.paste(source, (margin_x, margin_y))
|
| 112 |
|
|
|
|
| 113 |
mask = Image.new('L', target_size, 255)
|
| 114 |
mask_draw = ImageDraw.Draw(mask)
|
| 115 |
|
|
@@ -117,29 +112,32 @@ def infer(image, width, height, overlap_width, num_inference_steps, resize_optio
|
|
| 117 |
if alignment == "Middle":
|
| 118 |
mask_draw.rectangle([
|
| 119 |
(margin_x + overlap, margin_y + overlap),
|
| 120 |
-
(margin_x +
|
| 121 |
], fill=0)
|
| 122 |
elif alignment == "Left":
|
| 123 |
mask_draw.rectangle([
|
| 124 |
(margin_x, margin_y),
|
| 125 |
-
(margin_x +
|
| 126 |
], fill=0)
|
| 127 |
elif alignment == "Right":
|
| 128 |
mask_draw.rectangle([
|
| 129 |
(margin_x + overlap, margin_y),
|
| 130 |
-
(margin_x +
|
| 131 |
], fill=0)
|
| 132 |
elif alignment == "Top":
|
| 133 |
mask_draw.rectangle([
|
| 134 |
(margin_x, margin_y),
|
| 135 |
-
(margin_x +
|
| 136 |
], fill=0)
|
| 137 |
elif alignment == "Bottom":
|
| 138 |
mask_draw.rectangle([
|
| 139 |
(margin_x, margin_y + overlap),
|
| 140 |
-
(margin_x +
|
| 141 |
], fill=0)
|
| 142 |
|
|
|
|
|
|
|
|
|
|
| 143 |
cnet_image = background.copy()
|
| 144 |
cnet_image.paste(0, (0, 0), mask)
|
| 145 |
|
|
|
|
| 58 |
target_size = (width, height)
|
| 59 |
overlap = overlap_width
|
| 60 |
|
| 61 |
+
# Calculate the scaling factor to fit the image within the target size
|
| 62 |
+
scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)
|
| 63 |
+
new_width = int(source.width * scale_factor)
|
| 64 |
+
new_height = int(source.height * scale_factor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# Resize the source image
|
| 67 |
+
source = source.resize((new_width, new_height), Image.LANCZOS)
|
| 68 |
+
|
| 69 |
+
# Apply resize option
|
| 70 |
if resize_option == "Full":
|
| 71 |
resize_size = max(source.width, source.height)
|
| 72 |
elif resize_option == "1/2":
|
|
|
|
| 83 |
new_height = int(resize_size * aspect_ratio)
|
| 84 |
source = source.resize((new_width, new_height), Image.LANCZOS)
|
| 85 |
|
|
|
|
|
|
|
|
|
|
| 86 |
# Calculate margins based on alignment
|
| 87 |
if alignment == "Middle":
|
| 88 |
+
margin_x = (target_size[0] - new_width) // 2
|
| 89 |
+
margin_y = (target_size[1] - new_height) // 2
|
| 90 |
elif alignment == "Left":
|
| 91 |
margin_x = 0
|
| 92 |
+
margin_y = (target_size[1] - new_height) // 2
|
| 93 |
elif alignment == "Right":
|
| 94 |
+
margin_x = target_size[0] - new_width
|
| 95 |
+
margin_y = (target_size[1] - new_height) // 2
|
| 96 |
elif alignment == "Top":
|
| 97 |
+
margin_x = (target_size[0] - new_width) // 2
|
| 98 |
margin_y = 0
|
| 99 |
elif alignment == "Bottom":
|
| 100 |
+
margin_x = (target_size[0] - new_width) // 2
|
| 101 |
+
margin_y = target_size[1] - new_height
|
| 102 |
|
| 103 |
+
# Create a new background image and paste the resized source image
|
| 104 |
background = Image.new('RGB', target_size, (255, 255, 255))
|
| 105 |
background.paste(source, (margin_x, margin_y))
|
| 106 |
|
| 107 |
+
# Create the mask
|
| 108 |
mask = Image.new('L', target_size, 255)
|
| 109 |
mask_draw = ImageDraw.Draw(mask)
|
| 110 |
|
|
|
|
| 112 |
if alignment == "Middle":
|
| 113 |
mask_draw.rectangle([
|
| 114 |
(margin_x + overlap, margin_y + overlap),
|
| 115 |
+
(margin_x + new_width - overlap, margin_y + new_height - overlap)
|
| 116 |
], fill=0)
|
| 117 |
elif alignment == "Left":
|
| 118 |
mask_draw.rectangle([
|
| 119 |
(margin_x, margin_y),
|
| 120 |
+
(margin_x + new_width - overlap, margin_y + new_height)
|
| 121 |
], fill=0)
|
| 122 |
elif alignment == "Right":
|
| 123 |
mask_draw.rectangle([
|
| 124 |
(margin_x + overlap, margin_y),
|
| 125 |
+
(margin_x + new_width, margin_y + new_height)
|
| 126 |
], fill=0)
|
| 127 |
elif alignment == "Top":
|
| 128 |
mask_draw.rectangle([
|
| 129 |
(margin_x, margin_y),
|
| 130 |
+
(margin_x + new_width, margin_y + new_height - overlap)
|
| 131 |
], fill=0)
|
| 132 |
elif alignment == "Bottom":
|
| 133 |
mask_draw.rectangle([
|
| 134 |
(margin_x, margin_y + overlap),
|
| 135 |
+
(margin_x + new_width, margin_y + new_height)
|
| 136 |
], fill=0)
|
| 137 |
|
| 138 |
+
if not can_expand(source.width, source.height, target_size[0], target_size[1], alignment):
|
| 139 |
+
alignment = "Middle"
|
| 140 |
+
|
| 141 |
cnet_image = background.copy()
|
| 142 |
cnet_image.paste(0, (0, 0), mask)
|
| 143 |
|