Using a test image, the most common color is RGB (1, 1, 1). It is very black, but not completely black. My answer uses the PIL library , webcolors and the generous help code from this answer.
from PIL import Image import webcolors def closest_color(requested_color): min_colors = {} for key, name in webcolors.css3_hex_to_names.items(): r_c, g_c, b_c = webcolors.hex_to_rgb(key) rd = (r_c - requested_color[0]) ** 2 gd = (g_c - requested_color[1]) ** 2 bd = (b_c - requested_color[2]) ** 2 min_colors[(rd + gd + bd)] = name return min_colors[min(min_colors.keys())] def get_color_name(requested_color): try: closest_name = actual_name = webcolors.rgb_to_name(requested_color) except ValueError: closest_name = closest_color(requested_color) actual_name = None return actual_name, closest_name if __name__ == '__main__': lt = Image.open('test.gif').convert('RGB').getcolors() lt.sort(key=lambda tup:tup[0], reverse=True) actual_name, closest_name = get_color_name(lt[0][4]) print lt[0], actual_name, closest_name
Conclusion:
(531162, (1, 1, 1)) None black
In this case, you will be interested in the variable closest_name . The first ( lt[0] ) shows you the most common RGB value. This does not have a specific web color name, so None for actual_name
Explanation:
This opens the file you provided by converting it to RGB and then running the PIL getcolors method on the image. The result is a list of tuples in the format (count, RGB_color_value). Then I sort the list (in reverse order). Using functions from another answer , I pass in the most common RGB color value (now the first tuple in the list, and RBG the second element in the tuple).
source share