vettorazi commited on
Commit
0b5aa60
1 Parent(s): 3fe854a

added colormask

Browse files
Files changed (2) hide show
  1. ColorMask.py +31 -0
  2. server.py +4 -0
ColorMask.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from PIL import Image
3
+ import cv2
4
+
5
+
6
+ def hex_to_rgb(hex_color):
7
+ hex_color = hex_color.lstrip('#')
8
+ return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
9
+ def rgb_to_bgr(rgb_color):
10
+ return rgb_color[::-1]
11
+ def create_mask(image_np, color_to_replace, tolerance=20):
12
+ # Convert hex colors to RGB and then to BGR
13
+ color_to_replace_rgb = hex_to_rgb(color_to_replace)
14
+ # replacement_color_rgb = hex_to_rgb(replacement_color)
15
+ color_to_replace_bgr = rgb_to_bgr(color_to_replace_rgb)
16
+ # replacement_color_bgr = rgb_to_bgr(replacement_color_rgb)
17
+ # Calculate the lower and upper bounds for color tolerance in BGR
18
+ lower_bound = np.array([max(c - tolerance, 0) for c in color_to_replace_bgr])
19
+ upper_bound = np.array([min(c + tolerance, 255) for c in color_to_replace_bgr])
20
+ # Convert the image to BGR format for OpenCV processing
21
+ image_bgr = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
22
+ # Create a mask for pixels within the tolerance range using cv2.inRange
23
+ mask = cv2.inRange(image_bgr, lower_bound, upper_bound)
24
+ # Replace color in the masked area
25
+ # image_bgr[mask > 0] = replacement_color_bgr
26
+ # Convert back to RGB
27
+ # result_image_np = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
28
+ # Save the result
29
+ result_image = Image.fromarray(mask)
30
+ result_image.save('./result.jpg')
31
+ return result_image
server.py CHANGED
@@ -16,6 +16,7 @@ import recolorReinhardV2Algo
16
  import recolorLinearColorTransfer
17
  import matchCollection
18
  import ColorReplacer
 
19
  from typing import Optional
20
 
21
  app = FastAPI()
@@ -115,6 +116,9 @@ async def recolor(file: UploadFile = File(...), colors: str = Form(...), model:
115
  elif method == "ColorReplacer":
116
  print('ColorReplacer started')
117
  ColorReplacer.recolor_selected_area(image_np, colors[0], colors[1])
 
 
 
118
 
119
  #mask image:
120
  if mask is not None:
 
16
  import recolorLinearColorTransfer
17
  import matchCollection
18
  import ColorReplacer
19
+ import ColorMask
20
  from typing import Optional
21
 
22
  app = FastAPI()
 
116
  elif method == "ColorReplacer":
117
  print('ColorReplacer started')
118
  ColorReplacer.recolor_selected_area(image_np, colors[0], colors[1])
119
+ elif method == "ColorMask":
120
+ print('ColorMask started')
121
+ ColorMask.create_mask(image_np, colors[0])
122
 
123
  #mask image:
124
  if mask is not None: