Spaces:
Paused
Paused
sachin
commited on
Commit
·
19259ed
1
Parent(s):
a14b836
add- fit to image
Browse files
runway.py
CHANGED
@@ -31,14 +31,6 @@ def prepare_guided_image(original_image: Image, reference_image: Image, mask_ima
|
|
31 |
Prepare an initial image by softly blending the reference image into the masked area.
|
32 |
- Areas to keep (black in mask, 0) remain fully from the original image.
|
33 |
- Areas to inpaint (white in mask, 255) take content from the reference image with soft blending.
|
34 |
-
|
35 |
-
Args:
|
36 |
-
original_image (Image): The original image (RGB).
|
37 |
-
reference_image (Image): The reference image to copy from (RGB).
|
38 |
-
mask_image (Image): The mask image (grayscale, L mode).
|
39 |
-
|
40 |
-
Returns:
|
41 |
-
Image: The blended image to guide inpainting.
|
42 |
"""
|
43 |
original_array = np.array(original_image)
|
44 |
reference_array = np.array(reference_image)
|
@@ -80,6 +72,10 @@ def fit_image_to_mask(original_image: Image, reference_image: Image, mask_x1: in
|
|
80 |
mask_width = mask_x2 - mask_x1
|
81 |
mask_height = mask_y2 - mask_y1
|
82 |
|
|
|
|
|
|
|
|
|
83 |
# Resize reference image to fit the mask while preserving aspect ratio
|
84 |
ref_width, ref_height = reference_image.size
|
85 |
aspect_ratio = ref_width / ref_height
|
@@ -195,13 +191,6 @@ async def fit_image_to_mask(
|
|
195 |
):
|
196 |
"""
|
197 |
Endpoint for fitting a reference image into a masked region of the original image, refined to look natural.
|
198 |
-
- `image`: Original image file (PNG/JPG), e.g., a table.
|
199 |
-
- `reference_image`: Image to fit into the masked region (PNG/JPG), e.g., a cat.
|
200 |
-
- `prompt`: Text prompt for inpainting refinement.
|
201 |
-
- `mask_x1, mask_y1, mask_x2, mask_y2`: Coordinates for the rectangular mask (default: 100,100 to 200,200).
|
202 |
-
|
203 |
-
Returns:
|
204 |
-
- The resulting image as a PNG file.
|
205 |
"""
|
206 |
try:
|
207 |
# Load the uploaded images
|
@@ -211,7 +200,10 @@ async def fit_image_to_mask(
|
|
211 |
reference_image = Image.open(io.BytesIO(reference_bytes)).convert("RGB")
|
212 |
|
213 |
# Fit the reference image into the masked region
|
214 |
-
|
|
|
|
|
|
|
215 |
|
216 |
# Soften the mask for smoother transitions
|
217 |
softened_mask = soften_mask(mask_image, softness=5)
|
@@ -234,8 +226,10 @@ async def fit_image_to_mask(
|
|
234 |
media_type="image/png",
|
235 |
headers={"Content-Disposition": "attachment; filename=fitted_image.png"}
|
236 |
)
|
|
|
|
|
237 |
except Exception as e:
|
238 |
-
raise HTTPException(status_code=500, detail=f"Error during fitting and inpainting: {e}")
|
239 |
|
240 |
if __name__ == "__main__":
|
241 |
import uvicorn
|
|
|
31 |
Prepare an initial image by softly blending the reference image into the masked area.
|
32 |
- Areas to keep (black in mask, 0) remain fully from the original image.
|
33 |
- Areas to inpaint (white in mask, 255) take content from the reference image with soft blending.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
"""
|
35 |
original_array = np.array(original_image)
|
36 |
reference_array = np.array(reference_image)
|
|
|
72 |
mask_width = mask_x2 - mask_x1
|
73 |
mask_height = mask_y2 - mask_y1
|
74 |
|
75 |
+
# Ensure mask dimensions are positive
|
76 |
+
if mask_width <= 0 or mask_height <= 0:
|
77 |
+
raise ValueError("Mask dimensions must be positive")
|
78 |
+
|
79 |
# Resize reference image to fit the mask while preserving aspect ratio
|
80 |
ref_width, ref_height = reference_image.size
|
81 |
aspect_ratio = ref_width / ref_height
|
|
|
191 |
):
|
192 |
"""
|
193 |
Endpoint for fitting a reference image into a masked region of the original image, refined to look natural.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
"""
|
195 |
try:
|
196 |
# Load the uploaded images
|
|
|
200 |
reference_image = Image.open(io.BytesIO(reference_bytes)).convert("RGB")
|
201 |
|
202 |
# Fit the reference image into the masked region
|
203 |
+
result = fit_image_to_mask(original_image, reference_image, mask_x1, mask_y1, mask_x2, mask_y2)
|
204 |
+
if not isinstance(result, tuple) or len(result) != 2:
|
205 |
+
raise ValueError(f"Expected tuple of (guided_image, mask_image), got {type(result)}")
|
206 |
+
guided_image, mask_image = result
|
207 |
|
208 |
# Soften the mask for smoother transitions
|
209 |
softened_mask = soften_mask(mask_image, softness=5)
|
|
|
226 |
media_type="image/png",
|
227 |
headers={"Content-Disposition": "attachment; filename=fitted_image.png"}
|
228 |
)
|
229 |
+
except ValueError as ve:
|
230 |
+
raise HTTPException(status_code=500, detail=f"ValueError in processing: {str(ve)}")
|
231 |
except Exception as e:
|
232 |
+
raise HTTPException(status_code=500, detail=f"Error during fitting and inpainting: {str(e)}")
|
233 |
|
234 |
if __name__ == "__main__":
|
235 |
import uvicorn
|