Spaces:
Runtime error
Runtime error
File size: 19,388 Bytes
4f5118b 03d6362 4f5118b 9df6753 4f5118b ae9aaa7 4f5118b 2516d07 4f5118b b366d01 7afda38 b366d01 8cdca89 b366d01 7afda38 b366d01 ee50442 7afda38 96e1ded 8cdca89 96e1ded ee50442 7afda38 ee50442 8cdca89 ee50442 03d6362 4f5118b d68eb2f 96e1ded d68eb2f 96e1ded d68eb2f 96e1ded d68eb2f 96e1ded d68eb2f 96e1ded f4b4b46 96e1ded 6e6ac5d 8cdca89 4f5118b a8fcff6 4f5118b d68eb2f 4f5118b d68eb2f 4f5118b d68eb2f 4f5118b 8cdca89 96e1ded 4f5118b c7e6221 4f5118b 14ad6a7 c7e6221 4f5118b c7e6221 4f5118b 14ad6a7 c7e6221 14ad6a7 8cdca89 14ad6a7 c7e6221 4d28740 03d6362 a5a6e4f 2516d07 a8fcff6 2516d07 4f5118b 1d54d76 56112c7 9b938b8 14ad6a7 56112c7 a5a6e4f 56112c7 ee50442 9df6753 a5a6e4f 56112c7 7afda38 316c2e2 56112c7 267d2a8 56112c7 845cc97 56112c7 316c2e2 56112c7 845cc97 56112c7 316c2e2 56112c7 7afda38 56112c7 316c2e2 56112c7 845cc97 56112c7 316c2e2 56112c7 845cc97 56112c7 845cc97 b218c32 8cdca89 56112c7 1885170 56112c7 f0606b9 56112c7 8cdca89 58a5b39 56112c7 bd39fc7 8cdca89 845cc97 8cdca89 56112c7 845cc97 56112c7 845cc97 1d54d76 845cc97 1d54d76 845cc97 1d54d76 845cc97 ee50442 7afda38 b366d01 8cdca89 b366d01 6e6ac5d 7afda38 56112c7 96e1ded 56112c7 8cdca89 56112c7 b366d01 7afda38 b366d01 8cdca89 b366d01 14ad6a7 1885170 c7e6221 1885170 14ad6a7 f0606b9 14ad6a7 8cdca89 14ad6a7 1885170 c7e6221 1885170 14ad6a7 f0606b9 14ad6a7 8cdca89 14ad6a7 c7e6221 1885170 b366d01 f0606b9 b366d01 8cdca89 b366d01 56112c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
from diffusers import StableDiffusionXLInpaintPipeline
import gradio as gr
import numpy as np
import time
import math
import random
import imageio
from PIL import Image, ImageFilter
import torch
max_64_bit_int = 2**63 - 1
device = "cuda" if torch.cuda.is_available() else "cpu"
floatType = torch.float16 if torch.cuda.is_available() else torch.float32
pipe = StableDiffusionXLInpaintPipeline.from_pretrained("diffusers/stable-diffusion-xl-1.0-inpainting-0.1", torch_dtype = floatType)
pipe = pipe.to(device)
def noise_color(color, noise):
return color + random.randint(- noise, noise)
def check(
input_image,
enlarge_top,
enlarge_right,
enlarge_bottom,
enlarge_left,
prompt,
negative_prompt,
smooth_border,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
strength,
randomize_seed,
seed,
debug_mode,
progress = gr.Progress()):
if input_image is None:
raise gr.Error("Please provide an image.")
if prompt is None or prompt == "":
raise gr.Error("Please provide a prompt input.")
if (not (enlarge_top is None)) and enlarge_top < 0:
raise gr.Error("Please provide positive top margin.")
if (not (enlarge_right is None)) and enlarge_right < 0:
raise gr.Error("Please provide positive right margin.")
if (not (enlarge_bottom is None)) and enlarge_bottom < 0:
raise gr.Error("Please provide positive bottom margin.")
if (not (enlarge_left is None)) and enlarge_left < 0:
raise gr.Error("Please provide positive left margin.")
if (
(enlarge_top is None or enlarge_top == 0)
and (enlarge_right is None or enlarge_right == 0)
and (enlarge_bottom is None or enlarge_bottom == 0)
and (enlarge_left is None or enlarge_left == 0)
):
raise gr.Error("At least one border must be enlarged.")
def uncrop(
input_image,
enlarge_top,
enlarge_right,
enlarge_bottom,
enlarge_left,
prompt,
negative_prompt,
smooth_border,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
strength,
randomize_seed,
seed,
debug_mode,
progress = gr.Progress()):
check(
input_image,
enlarge_top,
enlarge_right,
enlarge_bottom,
enlarge_left,
prompt,
negative_prompt,
smooth_border,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
strength,
randomize_seed,
seed,
debug_mode
)
start = time.time()
progress(0, desc = "Preparing data...")
if enlarge_top is None or enlarge_top == "":
enlarge_top = 0
if enlarge_right is None or enlarge_right == "":
enlarge_right = 0
if enlarge_bottom is None or enlarge_bottom == "":
enlarge_bottom = 0
if enlarge_left is None or enlarge_left == "":
enlarge_left = 0
if negative_prompt is None:
negative_prompt = ""
if smooth_border is None:
smooth_border = 0
if denoising_steps is None:
denoising_steps = 1000
if num_inference_steps is None:
num_inference_steps = 50
if guidance_scale is None:
guidance_scale = 7
if image_guidance_scale is None:
image_guidance_scale = 1.5
if strength is None:
strength = 0.99
if randomize_seed:
seed = random.randint(0, max_64_bit_int)
random.seed(seed)
#pipe = pipe.manual_seed(seed)
original_height, original_width, original_channel = np.array(input_image).shape
output_width = enlarge_left + original_width + enlarge_right
output_height = enlarge_top + original_height + enlarge_bottom
# Enlarged image
enlarged_image = Image.new(mode = input_image.mode, size = (original_width, original_height), color = "black")
enlarged_image.paste(input_image, (0, 0))
enlarged_image = enlarged_image.resize((output_width, output_height))
enlarged_image = enlarged_image.filter(ImageFilter.BoxBlur(20))
enlarged_image.paste(input_image, (enlarge_left, enlarge_top))
horizontally_mirrored_input_image = input_image.transpose(Image.FLIP_LEFT_RIGHT).resize((original_width * 2, original_height))
enlarged_image.paste(horizontally_mirrored_input_image, (enlarge_left - (original_width * 2), enlarge_top))
enlarged_image.paste(horizontally_mirrored_input_image, (enlarge_left + original_width, enlarge_top))
vertically_mirrored_input_image = input_image.transpose(Image.FLIP_TOP_BOTTOM).resize((original_width, original_height * 2))
enlarged_image.paste(vertically_mirrored_input_image, (enlarge_left, enlarge_top - (original_height * 2)))
enlarged_image.paste(vertically_mirrored_input_image, (enlarge_left, enlarge_top + original_height))
returned_input_image = input_image.transpose(Image.ROTATE_180).resize((original_width * 2, original_height * 2))
enlarged_image.paste(returned_input_image, (enlarge_left - (original_width * 2), enlarge_top - (original_height * 2)))
enlarged_image.paste(returned_input_image, (enlarge_left - (original_width * 2), enlarge_top + original_height))
enlarged_image.paste(returned_input_image, (enlarge_left + original_width, enlarge_top - (original_height * 2)))
enlarged_image.paste(returned_input_image, (enlarge_left + original_width, enlarge_top + original_height))
enlarged_image = enlarged_image.filter(ImageFilter.BoxBlur(20))
# Noise image
noise_image = Image.new(mode = input_image.mode, size = (output_width, output_height), color = "black")
enlarged_pixels = enlarged_image.load()
for i in range(output_width):
for j in range(output_height):
enlarged_pixel = enlarged_pixels[i, j]
noise = min(max(enlarge_left - i, i - (enlarge_left + original_width), enlarge_top - j, j - (enlarge_top + original_height), 0), 255)
noise_image.putpixel((i, j), (noise_color(enlarged_pixel[0], noise), noise_color(enlarged_pixel[1], noise), noise_color(enlarged_pixel[2], noise), 255))
enlarged_image.paste(noise_image, (0, 0))
enlarged_image.paste(input_image, (enlarge_left, enlarge_top))
# Mask
mask_image = Image.new(mode = input_image.mode, size = (output_width, output_height), color = (255, 255, 255, 0))
black_mask = Image.new(mode = input_image.mode, size = (original_width - smooth_border, original_height - smooth_border), color = (0, 0, 0, 0))
mask_image.paste(black_mask, (enlarge_left + (smooth_border // 2), enlarge_top + (smooth_border // 2)))
mask_image = mask_image.filter(ImageFilter.BoxBlur((smooth_border // 2)))
# Limited to 1 million pixels
if 1024 * 1024 < output_width * output_height:
factor = ((1024 * 1024) / (output_width * output_height))**0.5
process_width = math.floor(output_width * factor)
process_height = math.floor(output_height * factor)
limitation = " Due to technical limitation, the image have been downscaled and then upscaled.";
else:
process_width = output_width
process_height = output_height
limitation = "";
# Width and height must be multiple of 8
if (process_width % 8) != 0 or (process_height % 8) != 0:
if ((process_width - (process_width % 8) + 8) * (process_height - (process_height % 8) + 8)) <= (1024 * 1024):
process_width = process_width - (process_width % 8) + 8
process_height = process_height - (process_height % 8) + 8
elif (process_height % 8) <= (process_width % 8) and ((process_width - (process_width % 8) + 8) * process_height) <= (1024 * 1024):
process_width = process_width - (process_width % 8) + 8
process_height = process_height - (process_height % 8)
elif (process_width % 8) <= (process_height % 8) and (process_width * (process_height - (process_height % 8) + 8)) <= (1024 * 1024):
process_width = process_width - (process_width % 8)
process_height = process_height - (process_height % 8) + 8
else:
process_width = process_width - (process_width % 8)
process_height = process_height - (process_height % 8)
progress(None, desc = "Processing...")
output_image = pipe(
seeds = [seed],
width = process_width,
height = process_height,
prompt = prompt,
negative_prompt = negative_prompt,
image = enlarged_image,
mask_image = mask_image,
num_inference_steps = num_inference_steps,
guidance_scale = guidance_scale,
image_guidance_scale = image_guidance_scale,
strength = strength,
denoising_steps = denoising_steps,
show_progress_bar = True
).images[0]
if limitation != "":
output_image = output_image.resize((output_width, output_height))
if debug_mode == False:
input_image = None
enlarged_image = None
mask_image = None
end = time.time()
secondes = int(end - start)
minutes = secondes // 60
secondes = secondes - (minutes * 60)
hours = minutes // 60
minutes = minutes - (hours * 60)
return [
output_image,
"Start again to get a different result. The new image is " + str(output_width) + " pixels large and " + str(output_height) + " pixels high, so an image of " + f'{output_width * output_height:,}' + " pixels. The image have been generated in " + str(hours) + " h, " + str(minutes) + " min, " + str(secondes) + " sec." + limitation,
input_image,
enlarged_image,
mask_image
]
def toggle_debug(is_debug_mode):
if is_debug_mode:
return [gr.update(visible = True)] * 3
else:
return [gr.update(visible = False)] * 3
with gr.Blocks() as interface:
gr.Markdown(
"""
<p style="text-align: center;"><b><big><big><big>Uncrop</big></big></big></b></p>
<p style="text-align: center;">Enlarges the point of view of your image, freely, without account, without watermark, without installation, which can be downloaded</p>
<br/>
<br/>
🚀 Powered by <i>SDXL 1.0</i> artificial intellingence. For illustration purpose, not information purpose. The new content is not based on real information but imagination.
<br/>
<ul>
<li>To change the <b>view angle</b> of your image, I recommend to use <i>Zero123</i>,</li>
<li>To <b>upscale</b> your image, I recommend to use <i>Ilaria Upscaler</i>,</li>
<li>To <b>slightly change</b> your image, I recommend to use <i>Image-to-Image SDXL</i>,</li>
<li>To change <b>one detail</b> on your image, I recommend to use <i>Inpaint SDXL</i>,</li>
<li>To remove the <b>background</b> of your image, I recommend to use <i>BRIA</i>,</li>
<li>To make a <b>tile</b> of your image, I recommend to use <i>Make My Image Tile</i>,</li>
<li>To modify <b>anything else</b> on your image, I recommend to use <i>Instruct Pix2Pix</i>.</li>
</ul>
<br/>
🐌 Slow process... ~1 hour.<br>You can duplicate this space on a free account, it works on CPU and should also run on CUDA.<br/>
<a href='https://huggingface.co/spaces/Fabrice-TIERCELIN/Uncrop?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14'></a>
<br/>
⚖️ You can use, modify and share the generated images but not for commercial uses.
"""
)
with gr.Row():
with gr.Column():
dummy_1 = gr.Label(visible = False)
with gr.Column():
enlarge_top = gr.Number(minimum = 0, value = 64, precision = 0, label = "Enlarge on top ⬆️", info = "in pixels")
with gr.Column():
dummy_2 = gr.Label(visible = False)
with gr.Row():
with gr.Column():
enlarge_left = gr.Number(minimum = 0, value = 64, precision = 0, label = "Enlarge on left ⬅️", info = "in pixels")
with gr.Column():
input_image = gr.Image(label = "Your image", sources = ["upload"], type = "pil")
with gr.Column():
enlarge_right = gr.Number(minimum = 0, value = 64, precision = 0, label = "Enlarge on right ➡️", info = "in pixels")
with gr.Row():
with gr.Column():
dummy_3 = gr.Label(visible = False)
with gr.Column():
enlarge_bottom = gr.Number(minimum = 0, value = 64, precision = 0, label = "Enlarge on bottom ⬇️", info = "in pixels")
with gr.Column():
dummy_4 = gr.Label(visible = False)
with gr.Row():
prompt = gr.Textbox(label = 'Prompt', info = "Describe the subject, the background and the style of image; 77 token limit", placeholder = 'Describe what you want to see in the entire image')
with gr.Row():
with gr.Accordion("Advanced options", open = False):
negative_prompt = gr.Textbox(label = 'Negative prompt', placeholder = 'Describe what you do NOT want to see in the entire image', value = 'Border, frame, painting, scribbling, smear, noise, blur, watermark')
smooth_border = gr.Slider(minimum = 0, maximum = 1024, value = 0, step = 2, label = "Smooth border", info = "lower=preserve original, higher=seamless")
denoising_steps = gr.Slider(minimum = 0, maximum = 1000, value = 1000, step = 1, label = "Denoising", info = "lower=irrelevant result, higher=relevant result")
num_inference_steps = gr.Slider(minimum = 10, maximum = 100, value = 50, step = 1, label = "Number of inference steps", info = "lower=faster, higher=image quality")
guidance_scale = gr.Slider(minimum = 1, maximum = 13, value = 7, step = 0.1, label = "Classifier-Free Guidance Scale", info = "lower=image quality, higher=follow the prompt")
image_guidance_scale = gr.Slider(minimum = 1, value = 1.5, step = 0.1, label = "Image Guidance Scale", info = "lower=image quality, higher=follow the image")
strength = gr.Number(value = 0.99, minimum = 0.01, maximum = 1.0, step = 0.01, label = "Strength", info = "lower=follow the original area (discouraged), higher=redraw from scratch")
randomize_seed = gr.Checkbox(label = "\U0001F3B2 Randomize seed (not working, always checked)", value = True, info = "If checked, result is always different")
seed = gr.Slider(minimum = 0, maximum = max_64_bit_int, step = 1, randomize = True, label = "Seed (if not randomized)")
debug_mode = gr.Checkbox(label = "Debug mode", value = False, info = "Show intermediate results")
with gr.Row():
submit = gr.Button("Uncrop", variant = "primary")
with gr.Row():
uncropped_image = gr.Image(label = "Uncropped image")
with gr.Row():
information = gr.Label(label = "Information")
with gr.Row():
original_image = gr.Image(label = "Original image", visible = False)
with gr.Row():
enlarged_image = gr.Image(label = "Enlarged image", visible = False)
with gr.Row():
mask_image = gr.Image(label = "Mask image", visible = False)
submit.click(toggle_debug, debug_mode, [
original_image,
enlarged_image,
mask_image
], queue = False, show_progress = False).then(check, inputs = [
input_image,
enlarge_top,
enlarge_right,
enlarge_bottom,
enlarge_left,
prompt,
negative_prompt,
smooth_border,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
strength,
randomize_seed,
seed,
debug_mode
], outputs = [], queue = False,
show_progress = False).success(uncrop, inputs = [
input_image,
enlarge_top,
enlarge_right,
enlarge_bottom,
enlarge_left,
prompt,
negative_prompt,
smooth_border,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
strength,
randomize_seed,
seed,
debug_mode
], outputs = [
uncropped_image,
information,
original_image,
enlarged_image,
mask_image
], scroll_to_output = True)
gr.Examples(
inputs = [
input_image,
enlarge_top,
enlarge_right,
enlarge_bottom,
enlarge_left,
prompt,
negative_prompt,
smooth_border,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
strength,
randomize_seed,
seed,
debug_mode
],
outputs = [
uncropped_image,
information,
original_image,
enlarged_image,
mask_image
],
examples = [
[
"Example1.webp",
1024,
1024,
1024,
1024,
"A woman, black hair, nowadays, in the street, ultrarealistic, realistic, photorealistic, 8k",
"Border, frame, painting, drawing, cartoon, scribbling, smear, noise, blur, watermark",
0,
1000,
50,
7,
1.5,
0.99,
True,
42,
False
],
[
"Example2.png",
1024,
1024,
1024,
1024,
"A man, jumping in the air, outside, ultrarealistic, realistic, photorealistic, 8k",
"Border, frame, painting, drawing, cartoon, scribbling, smear, noise, blur, watermark",
0,
1000,
50,
7,
1.5,
0.99,
True,
42,
False
],
[
"Example3.jpg",
0,
512,
0,
512,
"An old yellow car, in a town, ultrarealistic, realistic, photorealistic, 8k",
"Border, frame, painting, drawing, cartoon, scribbling, smear, noise, blur, watermark",
0,
1000,
50,
7,
1.5,
0.99,
True,
42,
False
],
],
cache_examples = False,
)
interface.queue().launch() |