Spaces:
Running
on
Zero
Running
on
Zero
Alexander Bagus
commited on
Commit
·
395a295
1
Parent(s):
2308582
22
Browse files- app.py +6 -4
- utils/image_utils.py +14 -0
app.py
CHANGED
|
@@ -95,13 +95,14 @@ print("pipe ready.")
|
|
| 95 |
|
| 96 |
def prepare(edit_dict):
|
| 97 |
# return edit_dict['background']
|
| 98 |
-
return edit_dict['
|
| 99 |
|
| 100 |
@spaces.GPU
|
| 101 |
def inference(
|
| 102 |
prompt,
|
| 103 |
negative_prompt,
|
| 104 |
edit_dict,
|
|
|
|
| 105 |
# image_scale=1.0,
|
| 106 |
# control_mode='Canny',
|
| 107 |
control_context_scale = 0.75,
|
|
@@ -148,7 +149,7 @@ def inference(
|
|
| 148 |
# control_image = control_image.resize((width, height))
|
| 149 |
# control_image_torch = get_image_latent(control_image, sample_size=sample_size)[:, :, 0]
|
| 150 |
|
| 151 |
-
mask_image = edit_dict['layers'][0]
|
| 152 |
# mask_image = edit_dict['composite']
|
| 153 |
if mask_image is not None:
|
| 154 |
mask_image = get_image_latent(mask_image, sample_size=sample_size)[:, :1, 0]
|
|
@@ -208,7 +209,7 @@ with gr.Blocks() as demo:
|
|
| 208 |
height=600,
|
| 209 |
sources=['upload', 'clipboard'],
|
| 210 |
type="pil",
|
| 211 |
-
label="
|
| 212 |
)
|
| 213 |
|
| 214 |
prompt = gr.Textbox(
|
|
@@ -270,7 +271,7 @@ with gr.Blocks() as demo:
|
|
| 270 |
# polished_prompt = gr.Textbox(label="Polished prompt", interactive=False)
|
| 271 |
|
| 272 |
with gr.Accordion("Preprocessor data", open=True):
|
| 273 |
-
mask_image = gr.Image(label="Mask image"
|
| 274 |
|
| 275 |
gr.Examples(examples=examples, inputs=[edit_dict, prompt])
|
| 276 |
gr.Markdown(read_file("static/footer.md"))
|
|
@@ -286,6 +287,7 @@ with gr.Blocks() as demo:
|
|
| 286 |
prompt,
|
| 287 |
negative_prompt,
|
| 288 |
edit_dict,
|
|
|
|
| 289 |
# image_scale,
|
| 290 |
# control_mode,
|
| 291 |
control_context_scale,
|
|
|
|
| 95 |
|
| 96 |
def prepare(edit_dict):
|
| 97 |
# return edit_dict['background']
|
| 98 |
+
return image_utils.convert_transparent_to_white(edit_dict['layers'][0])
|
| 99 |
|
| 100 |
@spaces.GPU
|
| 101 |
def inference(
|
| 102 |
prompt,
|
| 103 |
negative_prompt,
|
| 104 |
edit_dict,
|
| 105 |
+
mask_image,
|
| 106 |
# image_scale=1.0,
|
| 107 |
# control_mode='Canny',
|
| 108 |
control_context_scale = 0.75,
|
|
|
|
| 149 |
# control_image = control_image.resize((width, height))
|
| 150 |
# control_image_torch = get_image_latent(control_image, sample_size=sample_size)[:, :, 0]
|
| 151 |
|
| 152 |
+
# mask_image = edit_dict['layers'][0]
|
| 153 |
# mask_image = edit_dict['composite']
|
| 154 |
if mask_image is not None:
|
| 155 |
mask_image = get_image_latent(mask_image, sample_size=sample_size)[:, :1, 0]
|
|
|
|
| 209 |
height=600,
|
| 210 |
sources=['upload', 'clipboard'],
|
| 211 |
type="pil",
|
| 212 |
+
label="Edit Image"
|
| 213 |
)
|
| 214 |
|
| 215 |
prompt = gr.Textbox(
|
|
|
|
| 271 |
# polished_prompt = gr.Textbox(label="Polished prompt", interactive=False)
|
| 272 |
|
| 273 |
with gr.Accordion("Preprocessor data", open=True):
|
| 274 |
+
mask_image = gr.Image(label="Mask image")
|
| 275 |
|
| 276 |
gr.Examples(examples=examples, inputs=[edit_dict, prompt])
|
| 277 |
gr.Markdown(read_file("static/footer.md"))
|
|
|
|
| 287 |
prompt,
|
| 288 |
negative_prompt,
|
| 289 |
edit_dict,
|
| 290 |
+
mask_image,
|
| 291 |
# image_scale,
|
| 292 |
# control_mode,
|
| 293 |
control_context_scale,
|
utils/image_utils.py
CHANGED
|
@@ -19,6 +19,20 @@ def rescale_image(img, scale, nearest=32, max_size=1280):
|
|
| 19 |
|
| 20 |
return img.resize((new_w, new_h), Image.LANCZOS), new_w, new_h
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def padding_image(images, new_width, new_height):
|
| 23 |
new_image = Image.new('RGB', (new_width, new_height), (255, 255, 255))
|
| 24 |
|
|
|
|
| 19 |
|
| 20 |
return img.resize((new_w, new_h), Image.LANCZOS), new_w, new_h
|
| 21 |
|
| 22 |
+
def convert_transparent_to_white(input_image):
|
| 23 |
+
"""
|
| 24 |
+
Converts transparent pixels in a PIL Image to white.
|
| 25 |
+
"""
|
| 26 |
+
if input_image.mode in ('RGBA', 'LA') or \
|
| 27 |
+
(input_image.mode == 'P' and 'transparency' in input_image.info):
|
| 28 |
+
alpha = input_image.convert('RGBA').split()[-1]
|
| 29 |
+
bg = Image.new("RGB", input_image.size, (255, 255, 255))
|
| 30 |
+
converted_image = Image.composite(input_image.convert("RGB"), bg, alpha)
|
| 31 |
+
return converted_image
|
| 32 |
+
else:
|
| 33 |
+
# If the image doesn't have an alpha channel, return it as is
|
| 34 |
+
return input_image
|
| 35 |
+
|
| 36 |
def padding_image(images, new_width, new_height):
|
| 37 |
new_image = Image.new('RGB', (new_width, new_height), (255, 255, 255))
|
| 38 |
|