Anustup commited on
Commit
0831268
1 Parent(s): f22117e

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +46 -0
  2. create_print_layover.py +80 -0
  3. requirements.txt +4 -0
  4. texture_transfer.py +71 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from texture_transfer import create_image_tile, create_layover, create_image_cutout, paste_image
4
+
5
+ st.title("Texture Correction App")
6
+
7
+ if 'clicked' not in st.session_state:
8
+ st.session_state.clicked = False
9
+
10
+
11
+ def click_button():
12
+ st.session_state.clicked = True
13
+
14
+
15
+ with st.sidebar:
16
+ st.text("Play around this values")
17
+ opacity = st.slider("Control opacity", min_value=0.0, max_value=100.0, value=100.0)
18
+
19
+ product, generated, texture_correct = st.columns(3)
20
+ with product:
21
+ st.header("Input product texture patch")
22
+ product_image = st.file_uploader("Patch of the Garment(Cut out an area from the garment)",
23
+ type=["png", "jpg", "jpeg"])
24
+ if product_image:
25
+ st.image(product_image)
26
+
27
+ with generated:
28
+ st.header("Input generated product Image & Mask")
29
+ generated_image = st.file_uploader("Generated Image", type=["png", "jpg", "jpeg"])
30
+ gen_image_mask = st.file_uploader("Generated Mask", type=["png", "jpg", "jpeg"])
31
+ if generated_image:
32
+ st.image(generated_image)
33
+
34
+ with texture_correct:
35
+ st.button("Texture Correct", on_click=click_button)
36
+ if st.session_state.clicked:
37
+ with st.spinner("Texture correcting🫡..."):
38
+ product_image = Image.open(generated_image)
39
+ product_image_dim = product_image.size
40
+ x_dim = product_image_dim[0]
41
+ y_dim = product_image_dim[0]
42
+ create_image_tile(product_image, x_dim, y_dim)
43
+ overlay = create_layover(generated_image, 'tiled_image.png', opacity)
44
+ create_image_cutout('lay_over_image.png', gen_image_mask)
45
+ paste_image(generated_image, 'cut_out_image.png', gen_image_mask)
46
+ st.image("result.png", caption="Texture Corrected Image", use_column_width=True)
create_print_layover.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+
4
+ def assert_image_format(image, fcn_name: str, arg_name: str, force_alpha: bool = True):
5
+ if not isinstance(image, np.ndarray):
6
+ err_msg = 'The blend_modes function "{fcn_name}" received a value of type "{var_type}" for its argument ' \
7
+ '"{arg_name}". The function however expects a value of type "np.ndarray" for this argument. Please ' \
8
+ 'supply a variable of type np.ndarray to the "{arg_name}" argument.' \
9
+ .format(fcn_name=fcn_name, arg_name=arg_name, var_type=str(type(image).__name__))
10
+ raise TypeError(err_msg)
11
+
12
+ if not image.dtype.kind == 'f':
13
+ err_msg = 'The blend_modes function "{fcn_name}" received a numpy array of dtype (data type) kind ' \
14
+ '"{var_kind}" for its argument "{arg_name}". The function however expects a numpy array of the ' \
15
+ 'data type kind "f" (floating-point) for this argument. Please supply a numpy array with the data ' \
16
+ 'type kind "f" (floating-point) to the "{arg_name}" argument.' \
17
+ .format(fcn_name=fcn_name, arg_name=arg_name, var_kind=str(image.dtype.kind))
18
+ raise TypeError(err_msg)
19
+
20
+ if not len(image.shape) == 3:
21
+ err_msg = 'The blend_modes function "{fcn_name}" received a {n_dim}-dimensional numpy array for its argument ' \
22
+ '"{arg_name}". The function however expects a 3-dimensional array for this argument in the shape ' \
23
+ '(height x width x R/G/B/A layers). Please supply a 3-dimensional numpy array with that shape to ' \
24
+ 'the "{arg_name}" argument.' \
25
+ .format(fcn_name=fcn_name, arg_name=arg_name, n_dim=str(len(image.shape)))
26
+ raise TypeError(err_msg)
27
+
28
+ if force_alpha and not image.shape[2] == 4:
29
+ err_msg = 'The blend_modes function "{fcn_name}" received a numpy array with {n_layers} layers for its ' \
30
+ 'argument "{arg_name}". The function however expects a 4-layer array representing red, green, ' \
31
+ 'blue, and alpha channel for this argument. Please supply a numpy array that includes all 4 layers ' \
32
+ 'to the "{arg_name}" argument.' \
33
+ .format(fcn_name=fcn_name, arg_name=arg_name, n_layers=str(image.shape[2]))
34
+ raise TypeError(err_msg)
35
+
36
+
37
+ def assert_opacity(opacity, fcn_name: str, arg_name: str = 'opacity'):
38
+ if not isinstance(opacity, float) and not isinstance(opacity, int):
39
+ err_msg = 'The blend_modes function "{fcn_name}" received a variable of type "{var_type}" for its argument ' \
40
+ '"{arg_name}". The function however expects the value passed to "{arg_name}" to be of type ' \
41
+ '"float". Please pass a variable of type "float" to the "{arg_name}" argument of function ' \
42
+ '"{fcn_name}".' \
43
+ .format(fcn_name=fcn_name, arg_name=arg_name, var_type=str(type(opacity).__name__))
44
+ raise TypeError(err_msg)
45
+
46
+ if not 0.0 <= opacity <= 1.0:
47
+ err_msg = 'The blend_modes function "{fcn_name}" received the value "{val}" for its argument "{arg_name}". ' \
48
+ 'The function however expects that the value for "{arg_name}" is inside the range 0.0 <= x <= 1.0. ' \
49
+ 'Please pass a variable in that range to the "{arg_name}" argument of function "{fcn_name}".' \
50
+ .format(fcn_name=fcn_name, arg_name=arg_name, val=str(opacity))
51
+ raise ValueError(err_msg)
52
+
53
+
54
+ def _compose_alpha(img_in, img_layer, opacity):
55
+ comp_alpha = np.minimum(img_in[:, :, 3], img_layer[:, :, 3]) * opacity
56
+ new_alpha = img_in[:, :, 3] + (1.0 - img_in[:, :, 3]) * comp_alpha
57
+ np.seterr(divide='ignore', invalid='ignore')
58
+ ratio = comp_alpha / new_alpha
59
+ ratio[ratio == np.NAN] = 0.0
60
+ return ratio
61
+
62
+
63
+ def create_hard_light_layover(img_in, img_layer, opacity, disable_type_checks: bool = False):
64
+ if not disable_type_checks:
65
+ _fcn_name = 'hard_light'
66
+ assert_image_format(img_in, _fcn_name, 'img_in')
67
+ assert_image_format(img_layer, _fcn_name, 'img_layer')
68
+ assert_opacity(opacity, _fcn_name)
69
+ img_in_norm = img_in / 255.0
70
+ img_layer_norm = img_layer / 255.0
71
+ ratio = _compose_alpha(img_in_norm, img_layer_norm, opacity)
72
+ comp = np.greater(img_layer_norm[:, :, :3], 0.5) \
73
+ * np.minimum(1.0 - ((1.0 - img_in_norm[:, :, :3])
74
+ * (1.0 - (img_layer_norm[:, :, :3] - 0.5) * 2.0)), 1.0) \
75
+ + np.logical_not(np.greater(img_layer_norm[:, :, :3], 0.5)) \
76
+ * np.minimum(img_in_norm[:, :, :3] * (img_layer_norm[:, :, :3] * 2.0), 1.0)
77
+ ratio_rs = np.reshape(np.repeat(ratio, 3), [comp.shape[0], comp.shape[1], comp.shape[2]])
78
+ img_out = comp * ratio_rs + img_in_norm[:, :, :3] * (1.0 - ratio_rs)
79
+ img_out = np.nan_to_num(np.dstack((img_out, img_in_norm[:, :, 3]))) # add alpha channel and replace nans
80
+ return img_out * 255.0
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit~=1.36.0
2
+ opencv-python~=4.10.0.84
3
+ numpy~=2.0.0
4
+ pillow~=10.4.0
texture_transfer.py ADDED
@@ -0,0 +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')