Fabrice-TIERCELIN commited on
Commit
96e1ded
1 Parent(s): d68eb2f

Smooth border

Browse files
Files changed (1) hide show
  1. app.py +34 -14
app.py CHANGED
@@ -19,30 +19,39 @@ pipe = pipe.to(device)
19
  def noise_color(color, noise):
20
  return color + random.randint(- noise, noise)
21
 
22
- 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()):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  start = time.time()
24
  progress(0, desc = "Preparing data...")
25
 
26
  if source_img is None:
27
  raise gr.Error("Please provide an image.")
28
 
29
- if prompt is None or prompt == "":
30
- raise gr.Error("Please provide a prompt input.")
31
-
32
- if negative_prompt is None or negative_prompt == "":
33
- raise gr.Error("Please provide a negative prompt input.")
34
-
35
  if enlarge_top is None or enlarge_top == "":
36
- raise gr.Error("Please provide a top input.")
37
 
38
  if enlarge_right is None or enlarge_right == "":
39
- raise gr.Error("Please provide a right input.")
40
 
41
  if enlarge_bottom is None or enlarge_bottom == "":
42
- raise gr.Error("Please provide a bottom input.")
43
 
44
  if enlarge_left is None or enlarge_left == "":
45
- raise gr.Error("Please provide a left input.")
46
 
47
  if enlarge_top < 0 or enlarge_right < 0 or enlarge_bottom < 0 or enlarge_left < 0:
48
  raise gr.Error("Please only provide positive margins.")
@@ -50,6 +59,15 @@ def predict(source_img, enlarge_top, enlarge_right, enlarge_bottom, enlarge_left
50
  if enlarge_top == 0 and enlarge_right == 0 and enlarge_bottom == 0 and enlarge_left == 0:
51
  raise gr.Error("At least one border must be enlarged.")
52
 
 
 
 
 
 
 
 
 
 
53
  if randomize_seed:
54
  seed = random.randint(0, max_64_bit_int)
55
 
@@ -110,9 +128,9 @@ def predict(source_img, enlarge_top, enlarge_right, enlarge_bottom, enlarge_left
110
 
111
  # Mask
112
  mask_image = Image.new(mode = input_image.mode, size = (output_width, output_height), color = (255, 255, 255, 0))
113
- black_mask = Image.new(mode = input_image.mode, size = (original_width - 20, original_height - 20), color = (127, 127, 127, 0))
114
- mask_image.paste(black_mask, (enlarge_left + 10, enlarge_top + 10))
115
- mask_image = mask_image.filter(ImageFilter.BoxBlur(10))
116
 
117
  limitation = "";
118
 
@@ -200,6 +218,7 @@ with gr.Blocks() as interface:
200
  with gr.Row():
201
  with gr.Accordion("Advanced options", open = False):
202
  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')
 
203
  denoising_steps = gr.Slider(minimum = 0, maximum = 1000, value = 1000, step = 1, label = "Denoising", info = "lower=irrelevant result, higher=relevant result")
204
  num_inference_steps = gr.Slider(minimum = 10, maximum = 25, value = 20, step = 1, label = "Number of inference steps", info = "lower=faster, higher=image quality")
205
  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")
@@ -228,6 +247,7 @@ with gr.Blocks() as interface:
228
  enlarge_left,
229
  prompt,
230
  negative_prompt,
 
231
  denoising_steps,
232
  num_inference_steps,
233
  guidance_scale,
 
19
  def noise_color(color, noise):
20
  return color + random.randint(- noise, noise)
21
 
22
+ def predict(
23
+ source_img,
24
+ enlarge_top,
25
+ enlarge_right,
26
+ enlarge_bottom,
27
+ enlarge_left,
28
+ prompt,
29
+ negative_prompt,
30
+ smooth_border,
31
+ denoising_steps,
32
+ num_inference_steps,
33
+ guidance_scale,
34
+ randomize_seed,
35
+ seed,
36
+ debug_mode,
37
+ progress = gr.Progress()):
38
  start = time.time()
39
  progress(0, desc = "Preparing data...")
40
 
41
  if source_img is None:
42
  raise gr.Error("Please provide an image.")
43
 
 
 
 
 
 
 
44
  if enlarge_top is None or enlarge_top == "":
45
+ enlarge_top = 0
46
 
47
  if enlarge_right is None or enlarge_right == "":
48
+ enlarge_right = 0
49
 
50
  if enlarge_bottom is None or enlarge_bottom == "":
51
+ enlarge_bottom = 0
52
 
53
  if enlarge_left is None or enlarge_left == "":
54
+ enlarge_left = 0
55
 
56
  if enlarge_top < 0 or enlarge_right < 0 or enlarge_bottom < 0 or enlarge_left < 0:
57
  raise gr.Error("Please only provide positive margins.")
 
59
  if enlarge_top == 0 and enlarge_right == 0 and enlarge_bottom == 0 and enlarge_left == 0:
60
  raise gr.Error("At least one border must be enlarged.")
61
 
62
+ if prompt is None or prompt == "":
63
+ raise gr.Error("Please provide a prompt input.")
64
+
65
+ if negative_prompt is None:
66
+ negative_prompt = ""
67
+
68
+ if smooth_border is None:
69
+ smooth_border = 20
70
+
71
  if randomize_seed:
72
  seed = random.randint(0, max_64_bit_int)
73
 
 
128
 
129
  # Mask
130
  mask_image = Image.new(mode = input_image.mode, size = (output_width, output_height), color = (255, 255, 255, 0))
131
+ black_mask = Image.new(mode = input_image.mode, size = (original_width - smooth_border, original_height - smooth_border), color = (127, 127, 127, 0))
132
+ mask_image.paste(black_mask, (enlarge_left + (smooth_border // 2), enlarge_top + (smooth_border // 2)))
133
+ mask_image = mask_image.filter(ImageFilter.BoxBlur((smooth_border // 2)))
134
 
135
  limitation = "";
136
 
 
218
  with gr.Row():
219
  with gr.Accordion("Advanced options", open = False):
220
  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')
221
+ smooth_border = gr.Slider(minimum = 0, maximum = 1024, value = 20, step = 2, label = "Smooth border", info = "lower=preserve original, higher=seamless")
222
  denoising_steps = gr.Slider(minimum = 0, maximum = 1000, value = 1000, step = 1, label = "Denoising", info = "lower=irrelevant result, higher=relevant result")
223
  num_inference_steps = gr.Slider(minimum = 10, maximum = 25, value = 20, step = 1, label = "Number of inference steps", info = "lower=faster, higher=image quality")
224
  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")
 
247
  enlarge_left,
248
  prompt,
249
  negative_prompt,
250
+ smooth_border,
251
  denoising_steps,
252
  num_inference_steps,
253
  guidance_scale,