multimodalart HF staff commited on
Commit
923fca3
1 Parent(s): 85973d3

Update sketch_helper.py

Browse files
Files changed (1) hide show
  1. sketch_helper.py +11 -10
sketch_helper.py CHANGED
@@ -31,17 +31,18 @@ def get_high_freq_colors(image, similarity_threshold=30):
31
  return [high_freq_colors, image_copy]
32
 
33
  def color_quantization(image, color_frequency_list):
34
- # Extract the color values from the frequency list
35
- color_values = [color for _, color in color_frequency_list]
36
-
37
- # Replace the colors that are not in the frequency list with white
38
- mask = np.ones(image.shape[:2], dtype=bool)
39
- for color in color_values:
40
- color_mask = np.all(image == color, axis=2)
41
- mask = np.logical_and(mask, np.logical_not(color_mask))
42
- image[mask] = (255, 255, 255)
43
 
44
- return image
 
 
45
 
46
  def create_binary_matrix(img_arr, target_color):
47
  # Create mask of pixels with target color
 
31
  return [high_freq_colors, image_copy]
32
 
33
  def color_quantization(image, color_frequency_list):
34
+ # Convert the color frequency list to a set of unique colors
35
+ unique_colors = set([color for _, color in color_frequency_list])
36
+
37
+ # Create a mask for the image with True where the color is in the unique colors set
38
+ mask = np.any(np.all(image.reshape(-1, 1, 3) == np.array(list(unique_colors)), axis=2), axis=1).reshape(image.shape[:2])
39
+
40
+ # Create a new image with all pixels set to white
41
+ new_image = np.full_like(image, 255)
 
42
 
43
+ # Copy the pixels from the original image that have a color in the color frequency list
44
+ new_image[mask] = image[mask]
45
+ return new_image
46
 
47
  def create_binary_matrix(img_arr, target_color):
48
  # Create mask of pixels with target color