legoandmars commited on
Commit
e20bdd1
·
1 Parent(s): 9874e9a

fixes to transparency handling to fix artifacting

Browse files
Files changed (1) hide show
  1. app.py +9 -4
app.py CHANGED
@@ -105,9 +105,10 @@ def read_mask(pil_img_full, size: int = 256) -> Tuple[th.Tensor, th.Tensor]:
105
  pil_img_full = pil_img_full.convert('RGBA')
106
 
107
  pil_img = pil_img_full.getchannel( 'A' ) # Mode 'L'
108
- pil_img = pil_img.resize((size, size), resample=PIL.Image.BICUBIC)
109
 
110
  img = np.array(pil_img)[..., np.newaxis]
 
111
  return th.from_numpy(img)[None].permute(0, 3, 1, 2).float() / 255.0
112
 
113
  def pil_to_numpy(pil_img: Image) -> Tuple[th.Tensor, th.Tensor]:
@@ -134,11 +135,15 @@ def inpaint(input_img, prompt):
134
  source_image_64 = read_image(input_img, size=64)
135
 
136
  source_mask_64 = read_mask(input_img, size=64)
137
- source_mask_64 = (source_mask_64>0.5).float()
 
 
 
 
138
 
139
  # these are better but will leave a "mark"
140
- source_mask_256 = read_mask(input_img, size=256)
141
- source_mask_256 = (source_mask_256>0.5).float()
142
  # source_mask_256 = F.interpolate(source_mask_64, (256, 256), mode='nearest')
143
  # source_image_256 = pil_to_numpy(input_img_256)
144
  # source_image_64 = pil_to_numpy(input_img_64)
 
105
  pil_img_full = pil_img_full.convert('RGBA')
106
 
107
  pil_img = pil_img_full.getchannel( 'A' ) # Mode 'L'
108
+ pil_img = pil_img.resize((size, size), resample=PIL.Image.BILINEAR)
109
 
110
  img = np.array(pil_img)[..., np.newaxis]
111
+ img[img < 255] = 0 # extremely aggressive transparency removal. maybe too aggressive. slider?
112
  return th.from_numpy(img)[None].permute(0, 3, 1, 2).float() / 255.0
113
 
114
  def pil_to_numpy(pil_img: Image) -> Tuple[th.Tensor, th.Tensor]:
 
135
  source_image_64 = read_image(input_img, size=64)
136
 
137
  source_mask_64 = read_mask(input_img, size=64)
138
+ source_mask_256 = F.interpolate(source_mask_64, (256, 256), mode='nearest')
139
+ # source_mask_64 = (source_mask_64>0.5).float()
140
+ # 0.75 = transparency threshold
141
+ # pixels below 75% opacity will be deleted
142
+ # add as an option in the future?
143
 
144
  # these are better but will leave a "mark"
145
+ # source_mask_256 = read_mask(input_img, size=256)
146
+ # source_mask_256 = (source_mask_256>0.5).float()
147
  # source_mask_256 = F.interpolate(source_mask_64, (256, 256), mode='nearest')
148
  # source_image_256 = pil_to_numpy(input_img_256)
149
  # source_image_64 = pil_to_numpy(input_img_64)