jing96963 commited on
Commit
7de112f
·
1 Parent(s): f8b70f9

Add Color Match feature (Reinhard RGB transfer)

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py CHANGED
@@ -193,6 +193,19 @@ def run_rmbg(pil_image: Image.Image) -> Image.Image:
193
  return result
194
 
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  def add_image_watermark(result: Image.Image, ref: Image.Image, size: int = 200, padding: int = 16) -> Image.Image:
197
  result = result.copy().convert("RGBA")
198
  thumb = ref.convert("RGBA")
@@ -376,6 +389,7 @@ def infer(
376
  randomize_seed,
377
  guidance_scale,
378
  steps,
 
379
  progress=gr.Progress(track_tqdm=True),
380
  ):
381
  gc.collect()
@@ -432,6 +446,20 @@ def infer(
432
 
433
  if ref_image is not None and len(pil_images) > 1:
434
  result = add_image_watermark(result, pil_images[1])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
435
  return result, seed
436
 
437
  finally:
@@ -563,6 +591,8 @@ with gr.Blocks(
563
  lines=3,
564
  )
565
 
 
 
566
  with gr.Accordion("Advanced Settings", open=False):
567
  seed_input = gr.Slider(
568
  label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0,
@@ -593,6 +623,7 @@ with gr.Blocks(
593
  inputs=[
594
  source_input, ref_input, prompt_input, negative_prompt_input,
595
  seed_input, randomize_seed_input, guidance_scale_input, steps_input,
 
596
  ],
597
  outputs=[output_image, seed_input],
598
  ).then(
 
193
  return result
194
 
195
 
196
+ def color_match_reinhard(source: Image.Image, result: Image.Image) -> Image.Image:
197
+ """Reinhard RGB 均值/标准差色彩迁移:将 result 的色调对齐 source。"""
198
+ src = np.array(source.convert("RGB")).astype(np.float32)
199
+ res = np.array(result.convert("RGB")).astype(np.float32)
200
+ out = np.zeros_like(res)
201
+ for c in range(3):
202
+ s_mean, s_std = src[:, :, c].mean(), src[:, :, c].std()
203
+ r_mean, r_std = res[:, :, c].mean(), res[:, :, c].std()
204
+ ratio = s_std / (r_std + 1e-6) if r_std > 0.5 else 1.0
205
+ out[:, :, c] = (res[:, :, c] - r_mean) * ratio + s_mean
206
+ return Image.fromarray(np.clip(out, 0, 255).astype(np.uint8))
207
+
208
+
209
  def add_image_watermark(result: Image.Image, ref: Image.Image, size: int = 200, padding: int = 16) -> Image.Image:
210
  result = result.copy().convert("RGBA")
211
  thumb = ref.convert("RGBA")
 
389
  randomize_seed,
390
  guidance_scale,
391
  steps,
392
+ color_match,
393
  progress=gr.Progress(track_tqdm=True),
394
  ):
395
  gc.collect()
 
446
 
447
  if ref_image is not None and len(pil_images) > 1:
448
  result = add_image_watermark(result, pil_images[1])
449
+
450
+ if color_match:
451
+ # 用原图背景(无笔迹)作为色彩参考
452
+ if isinstance(source_image, dict):
453
+ bg = source_image.get("background")
454
+ if bg is not None:
455
+ ref_pil = Image.fromarray(bg).convert("RGB") if isinstance(bg, np.ndarray) else bg.convert("RGB")
456
+ else:
457
+ ref_pil = src_pil
458
+ else:
459
+ ref_pil = src_pil
460
+ ref_pil_resized = ref_pil.resize(result.size, Image.LANCZOS)
461
+ result = color_match_reinhard(ref_pil_resized, result)
462
+
463
  return result, seed
464
 
465
  finally:
 
591
  lines=3,
592
  )
593
 
594
+ color_match_input = gr.Checkbox(label="Color Match — 色彩对齐原图", value=True)
595
+
596
  with gr.Accordion("Advanced Settings", open=False):
597
  seed_input = gr.Slider(
598
  label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0,
 
623
  inputs=[
624
  source_input, ref_input, prompt_input, negative_prompt_input,
625
  seed_input, randomize_seed_input, guidance_scale_input, steps_input,
626
+ color_match_input,
627
  ],
628
  outputs=[output_image, seed_input],
629
  ).then(