Anustup commited on
Commit
f25db56
·
verified ·
1 Parent(s): 0831268

Update texture_transfer.py

Browse files
Files changed (1) hide show
  1. texture_transfer.py +71 -71
texture_transfer.py CHANGED
@@ -1,71 +1,71 @@
1
- from PIL import Image, ImageOps
2
- import numpy as np
3
- from create_print_layover import create_hard_light_layover
4
- import cv2
5
-
6
-
7
- def create_layover(background_image, layer_image, opacity):
8
- background_img_raw = Image.open(background_image)
9
- background_img_raw = background_img_raw.convert("RGBA")
10
- background_img = np.array(background_img_raw)
11
- background_img_float = background_img.astype(float)
12
- foreground_img_raw = Image.open(layer_image)
13
- foreground_img_raw = foreground_img_raw.convert("RGBA")
14
- foreground_img = np.array(foreground_img_raw)
15
- foreground_img_float = foreground_img.astype(float)
16
- blended_img_float = create_hard_light_layover(background_img_float, foreground_img_float, opacity)
17
- blended_img = np.uint8(blended_img_float)
18
- blended_img_raw = Image.fromarray(blended_img)
19
- output_path = "lay_over_image.png"
20
- blended_img_raw.save(output_path)
21
- return output_path
22
-
23
-
24
- def create_image_tile(input_patch, x_dim, y_dim):
25
- input_image = Image.open(input_patch)
26
- input_image = input_image.convert("RGB")
27
- width, height = input_image.size
28
- output_image = Image.new("RGB", (x_dim, y_dim))
29
- for y in range(0, y_dim, height):
30
- for x in range(0, x_dim, width):
31
- region_height = min(height, y_dim - y)
32
- region_width = min(width, x_dim - x)
33
- region = input_image.crop((0, 0, region_width, region_height))
34
- output_image.paste(region, (x, y))
35
- output_image.save('tiled_image.png')
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
-
51
- def paste_image(base_image_path, cutout_image_path, mask_path):
52
- background = Image.open(base_image_path).convert("RGB")
53
- cutout = Image.open(cutout_image_path)
54
- mask = Image.open(mask_path).convert("L")
55
- if cutout.mode == 'RGBA':
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')
 
1
+ from PIL import Image, ImageOps
2
+ import numpy as np
3
+ from create_print_layover import create_hard_light_layover
4
+ import cv2
5
+
6
+
7
+ def create_layover(background_image, layer_image, opacity):
8
+ # background_img_raw = Image.open(background_image)
9
+ background_img_raw = background_image.convert("RGBA")
10
+ background_img = np.array(background_img_raw)
11
+ background_img_float = background_img.astype(float)
12
+ # foreground_img_raw = Image.open(layer_image)
13
+ foreground_img_raw = layer_image.convert("RGBA")
14
+ foreground_img = np.array(foreground_img_raw)
15
+ foreground_img_float = foreground_img.astype(float)
16
+ blended_img_float = create_hard_light_layover(background_img_float, foreground_img_float, opacity)
17
+ blended_img = np.uint8(blended_img_float)
18
+ blended_img_raw = Image.fromarray(blended_img)
19
+ output_path = "lay_over_image.png"
20
+ blended_img_raw.save(output_path)
21
+ return output_path
22
+
23
+
24
+ def create_image_tile(input_patch, x_dim, y_dim):
25
+ input_image = Image.open(input_patch)
26
+ input_image = input_image.convert("RGB")
27
+ width, height = input_image.size
28
+ output_image = Image.new("RGB", (x_dim, y_dim))
29
+ for y in range(0, y_dim, height):
30
+ for x in range(0, x_dim, width):
31
+ region_height = min(height, y_dim - y)
32
+ region_width = min(width, x_dim - x)
33
+ region = input_image.crop((0, 0, region_width, region_height))
34
+ output_image.paste(region, (x, y))
35
+ output_image.save('tiled_image.png')
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
+
51
+ def paste_image(base_image_path, cutout_image_path, mask_path):
52
+ background = Image.open(base_image_path).convert("RGB")
53
+ cutout = Image.open(cutout_image_path)
54
+ mask = Image.open(mask_path).convert("L")
55
+ if cutout.mode == 'RGBA':
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')