Esempio n. 1
0
def extract_colors(image_path,
                   palette=XKCD_49_PALETTE,
                   color_space=ColorSpace.LAB,
                   max_colors=8,
                   scale_by=0.25):
    """ Returns list of most represented colors on image, ordered by number of appearances.

    Params:
    image_path - image location
    palette - colors palette used for examination (either dict or Palette instance)
    color_space - color space for which calculations are performed (either LAB or RGB)
    max_colors - maximum number of colors in palette (can be less if less number of colors has been identified)
    scale_by - ratio by which image is reduced before an examination, use this for optimization, number [0.0, 1.0]

    Returns:
    list of color names from a palette specification, ordered by number of occurrences in an image.
    """
    # load image
    image = load_image(image_path, scale_by=scale_by)

    # get colors from image
    image_colors = get_image_colors(image)

    # create palette
    if isinstance(palette, dict):
        palette = Palette(color_space=color_space, **palette)
    else:
        # TODO: better handle mismatch between provided color_space arg and palette configuration
        assert palette.color_space == color_space

    colors = classify_colors(image_colors, palette=palette)

    # order by the most significant
    colors = sorted([[k, v] for k, v in colors.items()],
                    key=lambda x: -1 * x[1])

    # remove colors with no representation on image
    colors = filter(lambda x: x[1] > 0, colors)

    return [c[0] for c in colors][:max_colors]
Esempio n. 2
0
 def test_nearest_web_red(self):
     palette = Palette(**WEB_PALETTE)
     red = Color.from_hex("#FF0000")
     self.assertEquals(palette.find_nearest(red)[0], "red")
     self.assertEquals(palette.find_nearest(red)[1], red)
Esempio n. 3
0
 def test_nearest_web_almost_white(self):
     palette = Palette(**WEB_PALETTE)
     almost_white = Color.from_hex("#FFFFFe")
     white = Color.from_hex("#FFFFFF")
     self.assertEquals(palette.find_nearest(almost_white)[0], "white")
     self.assertEquals(palette.find_nearest(almost_white)[1], white)
Esempio n. 4
0
 def test_create_from_web_palette(self):
     palette = Palette(**WEB_PALETTE)
     pcc = PaletteColorsCounter.from_palette(palette)
     self.assertEquals(sorted(pcc.keys()), sorted(palette.names))
Esempio n. 5
0
    def test_palette_has_index(self):
        palette = Palette(a=Color.from_hex("#FF0000"))

        index = palette._tree
        self.assertTrue(index is not None)
Esempio n. 6
0
 def test_nearest_web_red(self):
     palette = Palette(**WEB_PALETTE)
     red = Color.from_hex("#FF0000")
     self.assertEquals(palette.find_nearest(red)[0], "red")
     self.assertEquals(palette.find_nearest(red)[1], red)
Esempio n. 7
0
 def test_nearest_web_almost_white(self):
     palette = Palette(**WEB_PALETTE)
     almost_white = Color.from_hex("#FFFFFe")
     white = Color.from_hex("#FFFFFF")
     self.assertEquals(palette.find_nearest(almost_white)[0], "white")
     self.assertEquals(palette.find_nearest(almost_white)[1], white)
Esempio n. 8
0
 def test_color_names(self):
     self.assertEquals(len(Palette(**WEB_PALETTE).names),
                       len(WEB_PALETTE.keys()))