How do I exclude the color White (255,255,255) and Black (0,0,0) from extcolors?

Viewed 41

As a note I copied the original code from: https://towardsdatascience.com/image-color-extraction-with-python-in-4-steps-8d9370d9216e

I am using this script to analyze how many colors are available in line drawings and assigning a % weight to them (say for things like): Org. Chart Since the background is mostly white, I want to exclude it from the palette of available colors and from the total calculation. That said, since the lines are black (and always so) I also want to exclude this color as well, but not sure how to do it.

The code:

input_name = 'Cleaned_3.png'
output_width = 1800                   #set the output size
img = Image.open(input_name)
wpercent = (output_width/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((output_width,hsize), Image.ANTIALIAS)

#save
resize_name = '../NODE/Debug/resize_' + input_name  #the resized image name
img.save(resize_name)                 #output location can be specified before resize_name

#read
plt.figure(figsize=(9, 9))
img_url = resize_name
img = plt.imread(img_url)
plt.imshow(img)
plt.axis('off')
plt.show()

#tolerance: Group colors and give a better visual representation. 
#Scale from 0 to 100. Where 0 won’t group and 100 will group all into one.
#limit: upper limit to the number of extracted colors presented in the output.
colors_x = extcolors.extract_from_path(img_url, tolerance = 36, limit = 20)
colors_x

and I get am output like this:

([((255, 255, 255), 2028196),
  ((248, 157, 216), 26394),
  ((221, 54, 162), 25392),
  ((109, 160, 218), 9270),
  ((255, 117, 0), 2291),
  ((237, 207, 144), 1425),
  ((224, 117, 115), 1230),
  ((88, 25, 92), 929),
  ((130, 20, 18), 37),
  ((123, 93, 66), 29),
  ((33, 67, 105), 3),
  ((31, 0, 12), 2),
  ((172, 125, 25), 2)],
 2095200)

In the above example you will see that white occupies 2,028,196 pixels, and as a percentage it makes 96.8% of the "colors", which skews my actual colors weights greatly. So in reality I want this:

([((248, 157, 216), 26394),
  ((221, 54, 162), 25392),
  ((109, 160, 218), 9270),
  ((255, 117, 0), 2291),
  ((237, 207, 144), 1425),
  ((224, 117, 115), 1230),
  ((88, 25, 92), 929),
  ((130, 20, 18), 37),
  ((123, 93, 66), 29),
  ((33, 67, 105), 3),
  ((31, 0, 12), 2),
  ((172, 125, 25), 2)],
 67004)

So I can say that the first color makes up 39.39% as opposed to 1.25%.

Any insights?

1 Answers

It is easy to use a comprehension to filter out (255,255,255) and (0,0,0), but first you need to count the number of white_black pixels to subtract from the final results like this:

white_black = 0 
for t in colors_x[0]:
    if t[0] == (255,255,255) or t[0] == (0,0,0):
        white_black += t[1]
# Reconstruct the colors_x tuple
colors_x = ([t for t in colors_x[0] if t[0] != (255,255,255) and t[0] != (0,0,0)], colors_x[1] - white_black)

which would give:

([((248, 157, 216), 26394),
  ((221, 54, 162), 25392),
  ((109, 160, 218), 9270),
  ((255, 117, 0), 2291),
  ((237, 207, 144), 1425),
  ((224, 117, 115), 1230),
  ((88, 25, 92), 929),
  ((130, 20, 18), 37),
  ((123, 93, 66), 29),
  ((33, 67, 105), 3),
  ((31, 0, 12), 2),
  ((172, 125, 25), 2)],
 67004)
Related