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 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") 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, smooth_border, 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 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 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 prompt is None or prompt == "": raise gr.Error("Please provide a prompt input.") if negative_prompt is None: negative_prompt = "" if smooth_border is None: smooth_border = 20 if randomize_seed: seed = random.randint(0, max_64_bit_int) random.seed(seed) #pipe = pipe.manual_seed(seed) try: imageio.imwrite("data.png", source_img) except: raise gr.Error("Can't read input image. You can try to first save your image in another format (.webp, .png, .jpeg, .bmp...).") # Input image try: input_image = Image.open("data.png").convert("RGB") except: raise gr.Error("Can't open input image. You can try to first save your image in another format (.webp, .png, .jpeg, .bmp...).") 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(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 = (127, 127, 127, 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))) 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. The image have been generated in " + str(end - start) + " seconds." + 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( """

Uncrop

Enlarges the point of view of your image, up to 1 million pixels, freely, without account, without watermark, which can be downloaded



🚀 Powered by SDXL 1.0 artificial intellingence

🐌 Slow process... ~20 min with 20 inference steps, ~6 hours with 25 inference steps.
You can duplicate this space on a free account, it works on CPU.

⚖️ 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(): source_img = gr.Image(label = "Your image", sources = ["upload"], type = "numpy") 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 = 20, 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 = 25, value = 20, 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") randomize_seed = gr.Checkbox(label = "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(predict, inputs = [ source_img, enlarge_top, enlarge_right, enlarge_bottom, enlarge_left, prompt, negative_prompt, smooth_border, denoising_steps, num_inference_steps, guidance_scale, randomize_seed, seed, debug_mode ], outputs = [ uncropped_image, information, original_image, enlarged_image, mask_image ], scroll_to_output = True) interface.queue().launch()