Anustup commited on
Commit
dffefa4
·
verified ·
1 Parent(s): 697bc20

Update create_print_layover.py

Browse files
Files changed (1) hide show
  1. create_print_layover.py +97 -0
create_print_layover.py CHANGED
@@ -78,3 +78,100 @@ def create_hard_light_layover(img_in, img_layer, opacity, disable_type_checks: b
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
+
82
+ def stitch_images(image1, image2, overlap_width):
83
+ """Stitch two images side by side with overlapping edges."""
84
+ height = min(image1.shape[0], image2.shape[0])
85
+ image1 = cv2.resize(image1, (image1.shape[1], height))
86
+ image2 = cv2.resize(image2, (image2.shape[1], height))
87
+
88
+ mask = np.zeros((height, overlap_width), dtype=np.float32)
89
+ mask[:, :overlap_width] = np.linspace(1, 0, overlap_width)
90
+ mask = np.dstack([mask] * 3)
91
+
92
+ overlap1 = image1[:, -overlap_width:]
93
+ overlap2 = image2[:, :overlap_width]
94
+ blended_overlap = overlap1 * mask + overlap2 * (1 - mask)
95
+ blended_overlap = blended_overlap.astype(np.uint8)
96
+
97
+ stitched_image = np.hstack((image1[:, :-overlap_width], blended_overlap, image2[:, overlap_width:]))
98
+ return stitched_image
99
+
100
+
101
+ def tile_image_to_dimensions(image, target_width, target_height, overlap_width):
102
+ times_width = target_width // (image.shape[1] - overlap_width) + 1
103
+ times_height = target_height // (image.shape[0] - overlap_width) + 1
104
+ row_image = image
105
+ for _ in range(times_width - 1):
106
+ row_image = stitch_images(row_image, image, overlap_width)
107
+ final_image = row_image
108
+ for _ in range(times_height - 1):
109
+ final_image = np.vstack((final_image, row_image))
110
+ final_image = final_image[:target_height, :target_width]
111
+ return final_image
112
+
113
+
114
+ def create_image_with_feather_tile(tile_image, width, height, overlap_width):
115
+ from PIL import Image
116
+ tile_image = Image.open(tile_image)
117
+ tile_image.save("tiled_image_pil_converted.png")
118
+ tile_cv2 = cv2.imread("tiled_image_pil_converted.png")
119
+ tiled_image = tile_image_to_dimensions(tile_cv2, width, height, overlap_width)
120
+ cv2.imwrite('tiled_image.png', tiled_image)
121
+
122
+
123
+ def stitch_images_with_control(image1, image2, overlap_width, direction='horizontal'):
124
+ """Stitch two images side by side or top and bottom with overlapping edges."""
125
+ if direction == 'horizontal':
126
+ height = min(image1.shape[0], image2.shape[0])
127
+ image1 = cv2.resize(image1, (image1.shape[1], height))
128
+ image2 = cv2.resize(image2, (image2.shape[1], height))
129
+ mask = np.zeros((height, overlap_width), dtype=np.float32)
130
+ mask[:, :overlap_width] = np.linspace(1, 0, overlap_width)
131
+ mask = np.dstack([mask] * 3)
132
+ overlap1 = image1[:, -overlap_width:]
133
+ overlap2 = image2[:, :overlap_width]
134
+ blended_overlap = overlap1 * mask + overlap2 * (1 - mask)
135
+ blended_overlap = blended_overlap.astype(np.uint8)
136
+ stitched_image = np.hstack((image1[:, :-overlap_width], blended_overlap, image2[:, overlap_width:]))
137
+ elif direction == 'vertical':
138
+ width = min(image1.shape[1], image2.shape[1])
139
+ image1 = cv2.resize(image1, (width, image1.shape[0]))
140
+ image2 = cv2.resize(image2, (width, image2.shape[0]))
141
+ mask = np.zeros((overlap_width, width), dtype=np.float32)
142
+ mask[:overlap_width, :] = np.linspace(1, 0, overlap_width).reshape(-1, 1)
143
+ mask = np.dstack([mask] * 3)
144
+ overlap1 = image1[-overlap_width:, :]
145
+ overlap2 = image2[:overlap_width, :]
146
+ blended_overlap = overlap1 * mask + overlap2 * (1 - mask)
147
+ blended_overlap = blended_overlap.astype(np.uint8)
148
+ stitched_image = np.vstack((image1[:-overlap_width, :], blended_overlap, image2[overlap_width:, :]))
149
+ else:
150
+ raise ValueError("Direction must be 'horizontal' or 'vertical'")
151
+ return stitched_image
152
+
153
+
154
+ def color_extract(image_path, new_width, new_height):
155
+ from PIL import Image
156
+ format_image = Image.open(image_path)
157
+ format_image.save("pil_image.png")
158
+ image = cv2.imread("pil_image.png")
159
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
160
+ height, width, _ = image_rgb.shape
161
+ center_pixel = image_rgb[height // 2, width // 2]
162
+ color = center_pixel
163
+ new_image = np.full((new_height, new_width, 3), color, dtype=np.uint8)
164
+ output_path = 'color_image.jpg'
165
+ cv2.imwrite(output_path, cv2.cvtColor(new_image, cv2.COLOR_RGB2BGR))
166
+ print(f'Color image saved at {output_path}')
167
+
168
+
169
+ def control_texture(texture_image, direction, overlap, width, height):
170
+ import os
171
+ import cv2
172
+ color_extract(texture_image, width, height)
173
+ create_image_with_feather_tile(texture_image, width, height, overlap)
174
+ control_tile_image = stitch_images_with_control('color_image.jpg',
175
+ 'tiled_image.png', overlap, direction)
176
+ os.remove('tiled_image.png')
177
+ cv2.imwrite('tiled_image.png', control_tile_image)