nuwandaa commited on
Commit
e130f47
·
verified ·
1 Parent(s): 058d49f

Add alpha matting

Browse files
Files changed (1) hide show
  1. app.py +22 -4
app.py CHANGED
@@ -6,6 +6,9 @@ from PIL import Image, ImageDraw, ImageFilter
6
  import numpy as np
7
  import spaces
8
  from huggingface_hub import hf_hub_download
 
 
 
9
 
10
  pipe = FluxFillPipeline.from_pretrained(
11
  "black-forest-labs/FLUX.1-Fill-dev",
@@ -140,12 +143,27 @@ def inpaint(image, width, height, overlap_percentage, num_inference_steps, resiz
140
  guidance_scale=30,
141
  ).images[0]
142
 
143
- result = result.convert("RGBA")
144
  # mask_blur = mask.filter(ImageFilter.GaussianBlur(10))
145
- # cnet_image = Image.composite(result, cnet_image, mask)
146
- cnet_image.paste(result, (0, 0), mask)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
- return cnet_image, background
149
 
150
  def preview_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
151
  background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
 
6
  import numpy as np
7
  import spaces
8
  from huggingface_hub import hf_hub_download
9
+ from trimap_module import trimap
10
+ from pymatting import *
11
+
12
 
13
  pipe = FluxFillPipeline.from_pretrained(
14
  "black-forest-labs/FLUX.1-Fill-dev",
 
143
  guidance_scale=30,
144
  ).images[0]
145
 
146
+ result = result.convert("RGB")
147
  # mask_blur = mask.filter(ImageFilter.GaussianBlur(10))
148
+ # cnet_image = Image.composite(result, cnet_image, mask)
149
+ size = 10; # how many pixel extension do you want to dilate
150
+ number = 1; # numbering purpose
151
+ trimap_mask = trimap(image, name, size, number, erosion=False)
152
+
153
+ # estimate alpha from image and trimap
154
+ alpha = estimate_alpha_cf(cnet_image, trimap_mask)
155
+
156
+ # make gray background
157
+ background = result
158
+
159
+ # estimate foreground from image and alpha
160
+ foreground = estimate_foreground_ml(cnet_image, alpha)
161
+
162
+ # blend foreground with background and alpha, less color bleeding
163
+ new_image = blend(foreground, background, alpha)
164
+ # cnet_image.paste(result, (0, 0), mask)
165
 
166
+ return new_image, background
167
 
168
  def preview_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
169
  background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)