arkxcc commited on
Commit
f2521fe
1 Parent(s): 056c37e

fix: StrictTransfer

Browse files
Files changed (4) hide show
  1. .gitignore +2 -1
  2. recolorStrictTransfer.py +1 -1
  3. result.jpg +0 -0
  4. server.py +8 -4
.gitignore CHANGED
@@ -1,3 +1,4 @@
1
  node/
2
  __pycache__/
3
- .env/
 
 
1
  node/
2
  __pycache__/
3
+ .env/
4
+ result.jpg
recolorStrictTransfer.py CHANGED
@@ -4,7 +4,7 @@ import cv2
4
  from sklearn.cluster import MiniBatchKMeans
5
 
6
  def quantize_global(image, k):
7
- k_means = MiniBatchKMeans(n_clusters=k, compute_labels=False)
8
  k_means.fit(image.reshape(-1, 1))
9
  labels = k_means.predict(image.reshape(-1, 1))
10
  return np.uint8(k_means.cluster_centers_[labels]).reshape(image.shape)
 
4
  from sklearn.cluster import MiniBatchKMeans
5
 
6
  def quantize_global(image, k):
7
+ k_means = MiniBatchKMeans(n_clusters=k, compute_labels=False, n_init=1)
8
  k_means.fit(image.reshape(-1, 1))
9
  labels = k_means.predict(image.reshape(-1, 1))
10
  return np.uint8(k_means.cluster_centers_[labels]).reshape(image.shape)
result.jpg DELETED
Binary file (421 kB)
 
server.py CHANGED
@@ -77,14 +77,13 @@ async def color_palettes(colors: str = Form(...)):
77
  return {"inputColor": colors, "complementaryColors": complementaryColors, "adjacentColors": adjacentColors, "gradientColors": gradientColors}
78
 
79
  @app.post("/recolor/")
80
- async def recolor(file: UploadFile = File(...), colors: str = Form(...), new_colors: str = Form(...), random_colors: bool = Form(False), model: str = Form(...), mask: Optional[UploadFile] = File(None)):
81
  """
82
  Receive an image file and an array of strings representing colors of a selected pallete and recolor an image.
83
  """
84
  method = model
85
  invertColors = False
86
  colors = [color.strip() for color in colors.split(',')]
87
- new_colors = [new_color.strip() for new_color in new_colors.split(',')]
88
  file_content = await file.read()
89
  image_bytes = io.BytesIO(file_content)
90
  image = Image.open(image_bytes)
@@ -92,6 +91,9 @@ async def recolor(file: UploadFile = File(...), colors: str = Form(...), new_col
92
  image = ImageOps.invert(image)
93
  image_np = np.array(image)
94
 
 
 
 
95
  if method == "CCA":
96
  print('CCA generated')
97
  #Characteristic Color Analysis
@@ -125,10 +127,12 @@ async def recolor(file: UploadFile = File(...), colors: str = Form(...), new_col
125
  print('LinearColorTransfer generated')
126
  recolorLinearColorTransfer.recolor(image_np, colors)
127
 
128
- elif method == "StrictTransfer":
129
- print('StrictTransfer started')
130
  if random_colors:
 
131
  random.shuffle(new_colors)
 
132
  recolorStrictTransfer.recolor(image_np, colors, new_colors)
133
 
134
  elif method == "ColorMask":
 
77
  return {"inputColor": colors, "complementaryColors": complementaryColors, "adjacentColors": adjacentColors, "gradientColors": gradientColors}
78
 
79
  @app.post("/recolor/")
80
+ async def recolor(file: UploadFile = File(...), colors: str = Form(...), new_colors: Optional[str] = Form(None), random_colors: bool = Form(False), model: str = Form(...), mask: Optional[UploadFile] = File(None)):
81
  """
82
  Receive an image file and an array of strings representing colors of a selected pallete and recolor an image.
83
  """
84
  method = model
85
  invertColors = False
86
  colors = [color.strip() for color in colors.split(',')]
 
87
  file_content = await file.read()
88
  image_bytes = io.BytesIO(file_content)
89
  image = Image.open(image_bytes)
 
91
  image = ImageOps.invert(image)
92
  image_np = np.array(image)
93
 
94
+ if new_colors is not None:
95
+ new_colors = [new_color.strip() for new_color in new_colors.split(',')]
96
+
97
  if method == "CCA":
98
  print('CCA generated')
99
  #Characteristic Color Analysis
 
127
  print('LinearColorTransfer generated')
128
  recolorLinearColorTransfer.recolor(image_np, colors)
129
 
130
+ elif method.startswith("StrictTransfer"):
131
+ print('StrictTransfer started', colors, new_colors)
132
  if random_colors:
133
+ random.shuffle(colors)
134
  random.shuffle(new_colors)
135
+ print('StrictTransfer random', colors, new_colors)
136
  recolorStrictTransfer.recolor(image_np, colors, new_colors)
137
 
138
  elif method == "ColorMask":