Esempio n. 1
0
def extract_colors_from_files(max_colors=1, scale_by=0.5, color_space=ColorSpace.LAB,
                              palette_name="xkcd_49", fail_silently=True,  *args):
    """ Returns color: [files] mapping for a given set of files.

     Allows to specify many files for examination (*args).

     :param palette_name: colors palette name used for examination (web, xkcd_49 or xkcd_full)
     :param color_space: color space for which calculations are performed (either LAB or RGB)
     :param max_colors: maximum number of colors in palette (can be less if less number of colors has been identified)
     :param scale_by: ratio by which image is reduced before an examination, use this for optimization, number [0.0, 1.0]
     :param fail_silently: tells if program should continue on image read errors

     Returns JSON-formatted object mapping colors to a list of images used for examination.
    """
    results = {}

    # get palette by name
    palette = PALETTES[palette_name]

    for image_path in args:
        try:
            colors = api.extract_colors(image_path,
                                        palette=palette,
                                        max_colors=max_colors,
                                        scale_by=scale_by,
                                        color_space=color_space)
        except IOError:
            if not fail_silently:
                raise
        else:
            for color in colors:
                if color not in results:
                    results[color] = []
                results[color].append(image_path)

    return dumps(results)
Esempio n. 2
0
 def test_flag(self):
     image_path = self._full_path("images/flag.jpg")
     palette = extract_colors(image_path, max_colors=3)
     self.assertEquals(palette, ["navy blue", "red", "white"])
Esempio n. 3
0
 def test_bag(self):
     image_path = self._full_path("images/bag.jpg")
     palette = extract_colors(image_path, max_colors=2)
     self.assertEquals(palette, ["white", "salmon"])
Esempio n. 4
0
 def test_simple_black(self):
     image_path = self._full_path("images/1x1/000000.jpg")
     image_palette = extract_colors(image_path)
     self.assertEquals(image_palette, ["black"])
Esempio n. 5
0
 def test_simple_white(self):
     image_path = self._full_path("images/1x1/ffffff.jpg")
     image_palette = extract_colors(image_path)
     self.assertEquals(image_palette, ["white"])
Esempio n. 6
0
 def test_simple_red(self):
     image_path = self._full_path("images/1x1/ff0000.jpg")
     image_palette = extract_colors(image_path)
     self.assertEquals(image_palette, ["red"])
Esempio n. 7
0
 def test_max_colors_3(self):
     image_palette = extract_colors(self.EXAMPLE_IMAGE, max_colors=3)
     self.assertEquals(len(image_palette), 3)
Esempio n. 8
0
 def test_simple_white(self):
     image_path = self._full_path("images/1x1/ffffff.jpg")
     image_palette = extract_colors(image_path)
     self.assertEquals(image_palette, ["white"])
Esempio n. 9
0
 def test_scale_down_does_not_change_results_if_small(self):
     palette_from_scale_1 = extract_colors(self.EXAMPLE_IMAGE, max_colors=4, scale_by=1.0)
     palette_from_scale_2 = extract_colors(self.EXAMPLE_IMAGE, max_colors=4, scale_by=0.9)
     self.assertEquals(palette_from_scale_1, palette_from_scale_2)
Esempio n. 10
0
 def test_valid_image(self):
     image_palette = extract_colors(self.EXAMPLE_IMAGE)
     self.assertTrue(image_palette)
Esempio n. 11
0
 def test_sign(self):
     image_path = self._full_path("images/sign.jpg")
     palette = extract_colors(image_path, max_colors=3)
     self.assertEquals(palette, ["light blue", "orange", "black"])
Esempio n. 12
0
 def test_flag(self):
     image_path = self._full_path("images/flag.jpg")
     palette = extract_colors(image_path, max_colors=3)
     self.assertEquals(palette, ["navy blue", "red", "white"])
Esempio n. 13
0
 def test_bag(self):
     image_path = self._full_path("images/bag.jpg")
     palette = extract_colors(image_path, max_colors=2)
     self.assertEquals(palette, ["white", "salmon"])
Esempio n. 14
0
 def test_simple_black(self):
     image_path = self._full_path("images/1x1/000000.jpg")
     image_palette = extract_colors(image_path)
     self.assertEquals(image_palette, ["black"])
Esempio n. 15
0
 def test_sign(self):
     image_path = self._full_path("images/sign.jpg")
     palette = extract_colors(image_path, max_colors=3)
     self.assertEquals(palette, ["light blue", "orange", "black"])
Esempio n. 16
0
 def test_scale_change_results_if_significant(self):
     palette_from_scale_1 = extract_colors(self.EXAMPLE_IMAGE, max_colors=4, scale_by=1.0)
     palette_from_scale_2 = extract_colors(self.EXAMPLE_IMAGE, max_colors=4, scale_by=0.1)
     self.assertNotEquals(palette_from_scale_1, palette_from_scale_2)
Esempio n. 17
0
 def test_simple_red(self):
     image_path = self._full_path("images/1x1/ff0000.jpg")
     image_palette = extract_colors(image_path)
     self.assertEquals(image_palette, ["red"])