Anustup commited on
Commit
135150b
·
verified ·
1 Parent(s): 60bd22a

Update texture_transfer.py

Browse files
Files changed (1) hide show
  1. texture_transfer.py +11 -11
texture_transfer.py CHANGED
@@ -36,15 +36,15 @@ def create_image_tile(input_patch, x_dim, y_dim):
36
 
37
 
38
  def create_image_cutout(texture_transfer_image, actual_mask):
39
- image = Image.open(texture_transfer_image)
40
  mask = Image.open(actual_mask).convert('L')
41
  if mask.size != image.size:
42
  image = image.resize(mask.size, Image.Resampling.NEAREST)
43
  image_np = np.array(image)
44
  mask_np = np.array(mask)
45
- binary_mask = (mask_np > 127).astype(np.uint8) * 255
46
- masked_image_np = image_np * np.expand_dims(binary_mask, axis=-1) // 255
47
- masked_image = Image.fromarray(masked_image_np)
48
  masked_image.save('cut_out_image.png')
49
 
50
 
@@ -56,16 +56,16 @@ def paste_image(base_image_path, cutout_image_path, mask_path):
56
  cutout_rgb = cutout.convert("RGB")
57
  cutout_alpha = cutout.split()[-1]
58
  else:
59
- cutout_rgb = cutout
60
  cutout_alpha = mask
61
  cutout_rgb = cutout_rgb.resize(background.size, Image.Resampling.NEAREST)
62
  cutout_alpha = cutout_alpha.resize(background.size, Image.Resampling.NEAREST)
63
- cutout_alpha_np = np.array(cutout_alpha)
64
- binary_mask = (cutout_alpha_np > 128).astype(np.uint8) * 255
65
  cutout_rgb_np = np.array(cutout_rgb)
66
  background_np = np.array(background)
67
- cutout_masked = cutout_rgb_np * np.expand_dims(binary_mask, axis=-1) // 255
68
- background_masked = background_np * np.expand_dims(255 - binary_mask, axis=-1) // 255
 
 
69
  result_np = cutout_masked + background_masked
70
- result = Image.fromarray(result_np.astype(np.uint8))
71
- result.save('result.png')
 
36
 
37
 
38
  def create_image_cutout(texture_transfer_image, actual_mask):
39
+ image = Image.open(texture_transfer_image).convert('RGB')
40
  mask = Image.open(actual_mask).convert('L')
41
  if mask.size != image.size:
42
  image = image.resize(mask.size, Image.Resampling.NEAREST)
43
  image_np = np.array(image)
44
  mask_np = np.array(mask)
45
+ binary_mask = (mask_np > 127).astype(np.uint8)
46
+ masked_image_np = image_np * np.expand_dims(binary_mask, axis=-1)
47
+ masked_image = Image.fromarray(masked_image_np.astype(np.uint8))
48
  masked_image.save('cut_out_image.png')
49
 
50
 
 
56
  cutout_rgb = cutout.convert("RGB")
57
  cutout_alpha = cutout.split()[-1]
58
  else:
59
+ cutout_rgb = cutout.convert("RGB")
60
  cutout_alpha = mask
61
  cutout_rgb = cutout_rgb.resize(background.size, Image.Resampling.NEAREST)
62
  cutout_alpha = cutout_alpha.resize(background.size, Image.Resampling.NEAREST)
 
 
63
  cutout_rgb_np = np.array(cutout_rgb)
64
  background_np = np.array(background)
65
+ cutout_alpha_np = np.array(cutout_alpha)
66
+ cutout_alpha_np = cutout_alpha_np / 255.0
67
+ cutout_masked = (cutout_rgb_np * cutout_alpha_np[..., np.newaxis]).astype(np.uint8)
68
+ background_masked = (background_np * (1 - cutout_alpha_np[..., np.newaxis])).astype(np.uint8)
69
  result_np = cutout_masked + background_masked
70
+ result = Image.fromarray(result_np)
71
+ result.save('result.png')