def test_extract_16overuse_bmp(self):
     """Extract a palette from a 16-color indexed bmp with 8bpp."""
     img = Image.open('testdata/full-image-16color-overuse.bmp')
     processor = image_processor.ImageProcessor()
     extractor = extract_indexed_image_palette.ExtractIndexedImagePalette(
         processor)
     pal = extractor.extract_palette(self.imgpal(img))
     self.assertEqual(str(pal),
                      'P/16-30-01-0f/16-30-01-38/16-30-19-28/16-23-23-28/')
Beispiel #2
0
    def make_palette(self, palette_text, bg_color, is_sprite):
        """Construct the palette object from parsable text or color_needs.

    palette_text: Optional text to parse palette object from.
    bg_color: Background color. Must match palette's background color, if given.
    is_sprite: Whether this is a sprite palette.
    """
        pal = None
        if palette_text:
            # If palette argument was passed, use as either a literal or a file.
            try:
                if palette_text.startswith('P/'):
                    # Literal palette string.
                    parser = palette.PaletteParser()
                    pal = parser.parse(palette_text)
                elif os.path.isfile(palette_text):
                    # File containing a palette.
                    reader = palette.PaletteFileReader()
                    pal = reader.read(palette_text)
            except errors.PaletteParseError as e:
                self._err.add(e)
                return None
            if not bg_color is None and pal.bg_color != bg_color:
                self._err.add(
                    errors.PaletteBackgroundColorConflictError(
                        pal.bg_color, bg_color))
                return None
            return pal
        if self.img.palette:
            # If the image uses indexed color, try to extract a palette.
            extractor = extract_indexed_image_palette.ExtractIndexedImagePalette(
                self)
            pal = extractor.extract_palette(self.img.palette, self.img.format)
            if pal:
                return pal
        # If sprite mode, and there's no bg color, we can figure it out based upon
        # how many empty tiles there need to be.
        if is_sprite and bg_color is None and not self._test_only_auto_sprite_bg:
            for color, num in self._needs_provider.counts():
                if num >= 0x40 and bg_color is None:
                    bg_color = color
        # Make the palette from the color needs.
        guesser = guess_best_palette.GuessBestPalette()
        if not bg_color is None:
            guesser.set_bg_color(bg_color)
        color_sets = self._needs_provider.elems()
        try:
            pal = guesser.guess_palette(color_sets)
        except errors.TooManyPalettesError as e:
            self._err.add(e)
            return None
        return pal
    def parse_palette(self, palette_text, bg_color):
        """Parse the palette either from command-line flag, or indexed image.

    palette_text: Optional text to parse palette object from.
    bg_color: Background color. Must match palette's background color, if given.
    """
        pal = None
        if palette_text:
            # If palette argument was passed, parse it.
            try:
                if palette_text == '+':
                    # Attempt to extract the palette from the image, see below.
                    pass
                elif palette_text == '-':
                    # Do not attempt to extract the palette.
                    return None
                elif palette_text.startswith('P/'):
                    # Literal palette string.
                    parser = palette.PaletteParser()
                    pal = parser.parse(palette_text)
                elif os.path.isfile(palette_text):
                    # File containing a palette.
                    reader = palette.PaletteFileReader()
                    pal = reader.read(palette_text)
                else:
                    raise errors.PaletteParseError('Unknown palette "%s"' %
                                                   palette_text)
            except errors.PaletteParseError as e:
                self._err.add(e)
                return None
            if pal:
                if bg_color is not None and pal.bg_color != bg_color:
                    self._err.add(
                        errors.PaletteBackgroundColorConflictError(
                            pal.bg_color, bg_color))
                    return None
                return pal
        # If image uses indexed color, attempt to extract the palette.
        if self.img.palette:
            extractor = extract_indexed_image_palette.ExtractIndexedImagePalette(
                self)
            try:
                w = wrapped_image_palette.WrappedImagePalette.from_image(
                    self.img)
                pal = extractor.extract_palette(w)
            except errors.PaletteExtractionError as e:
                if palette_text is not None:
                    self._err.add(e)
                return None
            if pal:
                return pal
        return None
 def test_wrapped_64_bytes_png(self):
     processor = image_processor.ImageProcessor()
     extractor = extract_indexed_image_palette.ExtractIndexedImagePalette(
         processor)
     bytes = [
         252, 56, 0, 0, 255, 255, 255, 1, 0, 0, 252, 2, 0, 0, 0, 3, 252, 56,
         0, 4, 255, 255, 255, 5, 0, 0, 252, 6, 252, 216, 132, 8, 252, 56, 0,
         8, 255, 255, 255, 9, 0, 184, 0, 10, 248, 184, 0, 11, 252, 56, 0,
         12, 173, 145, 249, 13, 173, 145, 249, 14, 248, 184, 0, 15
     ]
     pal = extractor.extract_palette(MockWrappedImagePalette(bytes, 'PNG'))
     self.assertEqual(str(pal),
                      'P/16-30-01-0f/16-30-01-38/16-30-19-28/16-23-23-28/')
 def test_wrapped_48_bytes_bmp(self):
     processor = image_processor.ImageProcessor()
     extractor = extract_indexed_image_palette.ExtractIndexedImagePalette(
         processor)
     bytes = [
         0, 56, 252, 255, 255, 255, 252, 0, 0, 0, 0, 0, 0, 56, 252, 255,
         255, 255, 252, 0, 0, 132, 216, 252, 0, 56, 252, 255, 255, 255, 0,
         184, 0, 0, 184, 248, 0, 56, 252, 249, 145, 173, 249, 145, 173, 0,
         184, 248
     ]
     pal = extractor.extract_palette(MockWrappedImagePalette(bytes, 'BMP'))
     self.assertEqual(str(pal),
                      'P/16-30-01-0f/16-30-01-38/16-30-19-28/16-23-23-28/')