Madiy commited on
Commit
abe6d72
·
verified ·
1 Parent(s): aa337f9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -12
app.py CHANGED
@@ -225,19 +225,18 @@ def colour_image(image, strength, compute_metrics=False):
225
  original_image = image.convert("RGB")
226
  ow, oh = original_image.size
227
 
228
- # Calculate padding to reach nearest multiple of 16
229
  target_w = int(16 * math.ceil(ow / 16))
230
  target_h = int(16 * math.ceil(oh / 16))
231
 
232
- # Pad image to prevent center-cropping
233
- padded_image = Image.new("RGB", (target_w, target_h), (128, 128, 128))
234
- padded_image.paste(original_image, (0, 0))
235
 
236
  prompt = "colorize this black and white photograph with natural, vibrant, realistic colors."
237
 
238
  with torch.inference_mode():
239
  output = kontext_pipe(
240
- image=padded_image,
241
  prompt=prompt,
242
  height=target_h,
243
  width=target_w,
@@ -246,11 +245,11 @@ def colour_image(image, strength, compute_metrics=False):
246
  max_sequence_length=512,
247
  ).images[0]
248
 
249
- # Crop off padded boundary
250
- output = output.crop((0, 0, ow, oh))
251
- output_rgb = np.array(output)
252
 
253
- # Apply Strength Blending
254
  if strength < 1.0:
255
  grey = np.array(original_image.convert("L"))
256
  grey_3ch = np.stack([grey, grey, grey], axis=-1)
@@ -417,9 +416,7 @@ def fast_single_pass_outpaint(
417
  # ================================================================
418
  # 10. TAB HELPERS & GRADIO UI
419
  # ================================================================
420
- # ================================================================
421
- # 10. TAB HELPERS & GRADIO UI
422
- # ================================================================
423
  def send_to_outpaint(img):
424
  return (img, gr.update(selected="tab_outpaint")) if img is not None else (None, gr.update(selected="tab_outpaint"))
425
 
 
225
  original_image = image.convert("RGB")
226
  ow, oh = original_image.size
227
 
228
+ # 1. Calculate target dimensions as exact multiples of 16
229
  target_w = int(16 * math.ceil(ow / 16))
230
  target_h = int(16 * math.ceil(oh / 16))
231
 
232
+ # 2. Rescale directly to target dimensions (preserves full scene & avoids cropping)
233
+ resized_input = original_image.resize((target_w, target_h), Image.LANCZOS)
 
234
 
235
  prompt = "colorize this black and white photograph with natural, vibrant, realistic colors."
236
 
237
  with torch.inference_mode():
238
  output = kontext_pipe(
239
+ image=resized_input,
240
  prompt=prompt,
241
  height=target_h,
242
  width=target_w,
 
245
  max_sequence_length=512,
246
  ).images[0]
247
 
248
+ # 3. Resize output back to exact original aspect ratio & size for 1:1 pixel mapping
249
+ output_aligned = output.resize((ow, oh), Image.LANCZOS)
250
+ output_rgb = np.array(output_aligned)
251
 
252
+ # 4. Apply Strength Blending (pixel-aligned blend prevents ghosting)
253
  if strength < 1.0:
254
  grey = np.array(original_image.convert("L"))
255
  grey_3ch = np.stack([grey, grey, grey], axis=-1)
 
416
  # ================================================================
417
  # 10. TAB HELPERS & GRADIO UI
418
  # ================================================================
419
+
 
 
420
  def send_to_outpaint(img):
421
  return (img, gr.update(selected="tab_outpaint")) if img is not None else (None, gr.update(selected="tab_outpaint"))
422