Exemplo n.º 1
0
def main(input_image, output_image, loglevel, num_palettes, colors_per_palette,
         transparent_color):
    """
    Converts any image into a indexed image containing a number of smaller sub-palettes (--num-palettes), each with a
    fixed color length (--colors-per-palette). The conversion will assign each tile in the image one of
    these sub-palettes to use (single-palette-per-tile constraint).

    INPUT_IMAGE is the path of the image to convert and OUTPUT_IMAGE is where to save the converted image. All image
    types supported by PIL (the Python image library) can be used.
    :return:
    """
    logger.setLevel(logging.getLevelName(loglevel))
    echo("Converting...")
    if not os.path.exists(input_image):
        raise ClickException("The input image doesn't exist.")
    with open(input_image, 'rb') as input_file:
        try:
            image = Image.open(input_file)
        except OSError:
            raise ClickException(
                "The input image is not a supported image file.")
        try:
            converter = AikkuImageConverter(
                image, convert_hex_str_color_to_tuple(transparent_color))
            img = converter.convert(num_palettes, colors_per_palette)
        except RuntimeError as err:
            raise ClickException("The image could not be converted: " +
                                 str(err))
    echo(f"Saving image to {output_image}...")
    try:
        img.save(output_image)
    except OSError as err:
        raise ClickException("Error saving the image: " + str(err))
Exemplo n.º 2
0
 def _convert(self, image, transparent_color, mode, num_pals, dither_level):
     if mode == ImageConversionMode.JUST_REORGANIZE:
         converter = ImageConverter(image, transparent_color=transparent_color)
         return converter.convert(num_pals, colors_per_palette=16, color_steps=-1, max_colors=256,
                                  low_to_high=False, mosaic_limiting=False)
     converter = AikkuImageConverter(image, transparent_color)
     dither_mode = DitheringMode.NONE
     if mode == ImageConversionMode.DITHERING_ORDERED:
         dither_mode = DitheringMode.ORDERED
     elif mode == ImageConversionMode.DITHERING_FLOYDSTEINBERG:
         dither_mode = DitheringMode.FLOYDSTEINBERG
     return converter.convert(
         num_pals,
         dithering_mode=dither_mode,
         dithering_level=dither_level
     )
Exemplo n.º 3
0
 def _convert(self, image, transparent_color, mode, num_pals, dither_level):
     if mode == ImageConversionMode.JUST_REORGANIZE:
         converter = ImageConverter(image,
                                    transparent_color=transparent_color)
         try:
             return converter.convert(num_pals,
                                      colors_per_palette=16,
                                      color_steps=-1,
                                      max_colors=256,
                                      low_to_high=False,
                                      mosaic_limiting=False)
         except ValueError as e:
             setattr(e, USER_ERROR_MARK, True)
             raise e
     converter = AikkuImageConverter(image, transparent_color)
     dither_mode = DitheringMode.NONE
     if mode == ImageConversionMode.DITHERING_ORDERED:
         dither_mode = DitheringMode.ORDERED
     elif mode == ImageConversionMode.DITHERING_FLOYDSTEINBERG:
         dither_mode = DitheringMode.FLOYDSTEINBERG
     return converter.convert(num_pals,
                              dithering_mode=dither_mode,
                              dithering_level=dither_level)