Beispiel #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)
 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"])
 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"])
 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"])
 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"])
 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"])
Beispiel #7
0
 def test_max_colors_3(self):
     image_palette = extract_colors(self.EXAMPLE_IMAGE, max_colors=3)
     self.assertEquals(len(image_palette), 3)
 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"])
Beispiel #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)
Beispiel #10
0
 def test_valid_image(self):
     image_palette = extract_colors(self.EXAMPLE_IMAGE)
     self.assertTrue(image_palette)
Beispiel #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"])
Beispiel #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"])
Beispiel #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"])
Beispiel #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"])
 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"])
Beispiel #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)
Beispiel #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"])