Spaces:
Running
Running
File size: 9,752 Bytes
4f5118b 4667d53 4f5118b 2516d07 4f5118b 2516d07 4667d53 4f5118b 2516d07 4f5118b 4d28740 4667d53 2516d07 2f9b0fd 2516d07 4f5118b 4d28740 4f5118b 4d28740 4f5118b 2516d07 4f5118b 4667d53 4f5118b |
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 |
from diffusers import StableDiffusionXLInpaintPipeline
import gradio as gr
import numpy as np
import time
import math
import random
import imageio
from PIL import Image
from PIL import ImageFilter
import torch
import modin.pandas as pd
max_64_bit_int = 2**63 - 1
device = "cuda" if torch.cuda.is_available() else "cpu"
pipe = StableDiffusionXLInpaintPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", safety_checker = None)
pipe = pipe.to(device)
def noise_color(color, noise):
return color + random.randint(- noise, noise)
def predict(source_img, enlarge_top, enlarge_right, enlarge_bottom, enlarge_left, prompt, negative_prompt, denoising_steps, num_inference_steps, guidance_scale, randomize_seed, seed, debug_mode, progress=gr.Progress()):
start = time.time()
progress(0, desc = "Preparing data...")
if source_img is None:
raise gr.Error("Please provide an image.")
if prompt is None or prompt == "":
raise gr.Error("Please provide a prompt input.")
if negative_prompt is None or negative_prompt == "":
raise gr.Error("Please provide a negative prompt input.")
if enlarge_top < 0 or enlarge_right < 0 or enlarge_bottom < 0 or enlarge_left < 0:
raise gr.Error("Please only provide positive margins.")
if enlarge_top == 0 and enlarge_right == 0 and enlarge_bottom == 0 and enlarge_left == 0:
raise gr.Error("At least one border must be enlarged.")
if randomize_seed:
seed = random.randint(0, max_64_bit_int)
random.seed(seed)
#pipe = pipe.manual_seed(seed)
imageio.imwrite("data.png", source_img)
# Input image
input_image = Image.open("data.png").convert("RGB")
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_height, original_width), 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(25))
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(25))
# 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(abs(enlarge_left - i), abs(enlarge_left + original_width - i), abs(enlarge_top - j), abs(enlarge_top + original_height - j), 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 - 20, original_height - 20), color = (0, 0, 0, 0))
mask_image.paste(black_mask, (enlarge_left + 10, enlarge_top + 10))
mask_image = mask_image.filter(ImageFilter.BoxBlur(10))
limitation = "";
# Limited to 1 million pixels
if 1024 * 1024 < output_width * output_height:
factor = ((1024 * 1024) / (output_width * output_height))**0.5
output_width = math.floor(output_width * factor)
output_height = math.floor(output_height * factor)
limitation = " Due to technical limitation, the image have been downscaled.";
# Width and height must be multiple of 8
output_width = output_width - (output_width % 8)
output_height = output_height - (output_height % 8)
progress(None, desc = "Processing...")
output_image = pipe(seeds=[seed], width = output_width, height = output_height, prompt = prompt, negative_prompt = negative_prompt, image = enlarged_image, mask_image = mask_image, num_inference_steps = num_inference_steps, guidance_scale = guidance_scale, denoising_steps = denoising_steps, show_progress_bar = True).images[0]
if debug_mode == False:
input_image = None
enlarged_image = None
mask_image = None
end = time.time()
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 " + str(output_width * output_height) + " pixels." + limitation,
input_image,
enlarged_image,
mask_image
]
title = "Uncrop"
description = "<p style='text-align: center;'>Enlarges the point of view of your image, up to 1 million pixels, freely, without account, without watermark, which can be downloaded</p><br/><br/>🚀 Powered by <i>SDXL 1.0</i> artificial intellingence<br/><ul><li>If you need to change the <b>view angle</b> of your image, I recommend you to use <i>Zero123</i>,</li><li>If you need to <b>upscale</b> your image, I recommend you to use <i>Ilaria Upscaler</i>,</li><li>If you need to <b>slightly change</b> your image, I recommend you to use <i>Image-to-Image SDXL</i>,</li><li>If you need to change <b>one detail</b> on your image, I recommend you to use <i>Inpaint SDXL</i>.</li></ul><br/>🐌 Slow process... ~20 min with 20 inference steps, ~6 hours with 25 inference steps.<br>You can duplicate this space on a free account, it works on CPU.<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/><br/>⚖️ You can use, modify and share the generated images but not for commercial uses."
gr.Interface(fn = predict, inputs = [
gr.Image(label = "Your image", source = "upload", type = "numpy"),
gr.Number(minimum = 0, value = 64, precision = 0, label = "Enlarge on top", info = "in pixels"),
gr.Number(minimum = 0, value = 64, precision = 0, label = "Enlarge on right", info = "in pixels"),
gr.Number(minimum = 0, value = 64, precision = 0, label = "Enlarge on bottom", info = "in pixels"),
gr.Number(minimum = 0, value = 64, precision = 0, label = "Enlarge on left", info = "in pixels"),
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'),
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'),
gr.Slider(minimum = 0, maximum = 1000, value = 1000, step = 1, label = "Denoising", info = "lower=irrelevant result, higher=relevant result"),
gr.Slider(minimum = 10, maximum = 25, value = 10, step = 1, label = "Number of inference steps", info = "lower=faster, higher=image quality"),
gr.Slider(minimum = 1, maximum = 13, value = 7, step = 0.1, label = "Classifier-Free Guidance Scale", info = "lower=image quality, higher=follow the prompt"),
gr.Checkbox(label = "Randomize seed (not working, always checked)", value = True, info = "If checked, result is always different"),
gr.Slider(minimum = 0, maximum = max_64_bit_int, step = 1, randomize = True, label = "Seed (if not randomized)"),
gr.Checkbox(label = "Debug mode", value = True, info = "Show intermediate results")
], outputs = [
gr.Image(label = "Uncropped image"),
gr.Label(label = "Information"),
gr.Image(label = "Original image"),
gr.Image(label = "Enlarged image"),
gr.Image(label = "Mask image")
], title = title, description = description).launch(max_threads = True) |