Spaces:
Paused
Paused
sachin
commited on
Commit
·
57cd301
1
Parent(s):
7ba3751
add-auto generated mask
Browse files
runway.py
CHANGED
@@ -29,8 +29,8 @@ async def root():
|
|
29 |
def prepare_guided_image(original_image: Image, reference_image: Image, mask_image: Image) -> Image:
|
30 |
"""
|
31 |
Prepare an initial image by softly blending the reference image into the masked area.
|
32 |
-
-
|
33 |
-
-
|
34 |
|
35 |
Args:
|
36 |
original_image (Image): The original image (RGB).
|
@@ -43,13 +43,13 @@ def prepare_guided_image(original_image: Image, reference_image: Image, mask_ima
|
|
43 |
# Convert images to numpy arrays
|
44 |
original_array = np.array(original_image)
|
45 |
reference_array = np.array(reference_image)
|
46 |
-
mask_array = np.array(mask_image) / 255.0 # Normalize to [0, 1]
|
47 |
|
48 |
# Expand mask to RGB channels
|
49 |
mask_array = mask_array[:, :, np.newaxis]
|
50 |
|
51 |
-
#
|
52 |
-
blended_array = original_array * mask_array + reference_array *
|
53 |
blended_array = blended_array.astype(np.uint8)
|
54 |
|
55 |
return Image.fromarray(blended_array)
|
@@ -71,7 +71,7 @@ def soften_mask(mask_image: Image, softness: int = 5) -> Image:
|
|
71 |
def generate_rectangular_mask(image_size: tuple, x1: int = 100, y1: int = 100, x2: int = 200, y2: int = 200) -> Image:
|
72 |
"""
|
73 |
Generate a rectangular mask matching the image dimensions.
|
74 |
-
-
|
75 |
|
76 |
Args:
|
77 |
image_size (tuple): Tuple of (width, height) of the original image.
|
@@ -81,12 +81,12 @@ def generate_rectangular_mask(image_size: tuple, x1: int = 100, y1: int = 100, x
|
|
81 |
Returns:
|
82 |
Image: The generated mask in grayscale (L mode).
|
83 |
"""
|
84 |
-
# Create a blank
|
85 |
-
mask = Image.new("L", image_size,
|
86 |
draw = ImageDraw.Draw(mask)
|
87 |
|
88 |
-
# Draw a
|
89 |
-
draw.rectangle([x1, y1, x2, y2], fill=
|
90 |
|
91 |
return mask
|
92 |
|
|
|
29 |
def prepare_guided_image(original_image: Image, reference_image: Image, mask_image: Image) -> Image:
|
30 |
"""
|
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).
|
|
|
43 |
# Convert images to numpy arrays
|
44 |
original_array = np.array(original_image)
|
45 |
reference_array = np.array(reference_image)
|
46 |
+
mask_array = np.array(mask_image) / 255.0 # Normalize to [0, 1]
|
47 |
|
48 |
# Expand mask to RGB channels
|
49 |
mask_array = mask_array[:, :, np.newaxis]
|
50 |
|
51 |
+
# Blend: use original where mask=0 (black), reference where mask=1 (white)
|
52 |
+
blended_array = original_array * (1 - mask_array) + reference_array * mask_array
|
53 |
blended_array = blended_array.astype(np.uint8)
|
54 |
|
55 |
return Image.fromarray(blended_array)
|
|
|
71 |
def generate_rectangular_mask(image_size: tuple, x1: int = 100, y1: int = 100, x2: int = 200, y2: int = 200) -> Image:
|
72 |
"""
|
73 |
Generate a rectangular mask matching the image dimensions.
|
74 |
+
- Black (0) for areas to keep, white (255) for areas to inpaint.
|
75 |
|
76 |
Args:
|
77 |
image_size (tuple): Tuple of (width, height) of the original image.
|
|
|
81 |
Returns:
|
82 |
Image: The generated mask in grayscale (L mode).
|
83 |
"""
|
84 |
+
# Create a blank black mask (0 = keep)
|
85 |
+
mask = Image.new("L", image_size, 0)
|
86 |
draw = ImageDraw.Draw(mask)
|
87 |
|
88 |
+
# Draw a white rectangle (255 = inpaint)
|
89 |
+
draw.rectangle([x1, y1, x2, y2], fill=255)
|
90 |
|
91 |
return mask
|
92 |
|