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

added colorReplacer

Browse files
Files changed (3) hide show
  1. ColorReplacer.py +31 -0
  2. recolorLinearColorTransfer.py +4 -1
  3. server.py +5 -1
ColorReplacer.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 recolor_selected_area(image_np, color_to_replace, replacement_color, 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(result_image_np)
30
+ result_image.save('./result.jpg')
31
+ return result_image
recolorLinearColorTransfer.py CHANGED
@@ -90,11 +90,14 @@ def create_target_image(hex_colors):
90
 
91
  return target_image
92
 
 
93
  def recolor(image_np, colors):
94
  colors = [color.replace('#', '') for color in colors]
95
  palette = create_target_image(colors)
96
 
97
- # No need to convert to BGR and then read it again
 
 
98
  target_img = image_np.astype(float)/256
99
  source_img = palette.astype(float)/256
100
  output_img = match_color(target_img, source_img)
 
90
 
91
  return target_image
92
 
93
+
94
  def recolor(image_np, colors):
95
  colors = [color.replace('#', '') for color in colors]
96
  palette = create_target_image(colors)
97
 
98
+ # Only take the first three channels (RGB) of the image
99
+ image_np = image_np[:, :, :3]
100
+
101
  target_img = image_np.astype(float)/256
102
  source_img = palette.astype(float)/256
103
  output_img = match_color(target_img, source_img)
server.py CHANGED
@@ -15,6 +15,7 @@ import recolorPaletteBasedTransfer
15
  import recolorReinhardV2Algo
16
  import recolorLinearColorTransfer
17
  import matchCollection
 
18
  from typing import Optional
19
 
20
  app = FastAPI()
@@ -111,6 +112,9 @@ async def recolor(file: UploadFile = File(...), colors: str = Form(...), model:
111
  elif method == "LinearColorTransfer":
112
  print('LinearColorTransfer generated')
113
  recolorLinearColorTransfer.recolor(image_np, colors)
 
 
 
114
 
115
  #mask image:
116
  if mask is not None:
@@ -129,7 +133,7 @@ async def recolor(file: UploadFile = File(...), colors: str = Form(...), model:
129
  # Save the new image
130
  new_image = Image.fromarray(new_image_np)
131
  new_image.save('./result.jpg')
132
-
133
  img_file = open("./result.jpg", "rb")
134
  return StreamingResponse(img_file, media_type="image/jpeg")
135
 
 
15
  import recolorReinhardV2Algo
16
  import recolorLinearColorTransfer
17
  import matchCollection
18
+ import ColorReplacer
19
  from typing import Optional
20
 
21
  app = FastAPI()
 
112
  elif method == "LinearColorTransfer":
113
  print('LinearColorTransfer generated')
114
  recolorLinearColorTransfer.recolor(image_np, colors)
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:
 
133
  # Save the new image
134
  new_image = Image.fromarray(new_image_np)
135
  new_image.save('./result.jpg')
136
+
137
  img_file = open("./result.jpg", "rb")
138
  return StreamingResponse(img_file, media_type="image/jpeg")
139