Skip to main content
Glama
ext-sakamoro

Aseprite MCP Tools

by ext-sakamoro

extract_palette_from_image

Extract color palettes from Aseprite images for pixel art projects. Specify maximum colors and optionally save the palette file.

Instructions

Extract a color palette from an existing image.

Args: filename: Name of the Aseprite file to extract palette from max_colors: Maximum number of colors to extract (default: 16) output_filename: Optional filename to save the palette

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameYes
max_colorsNo
output_filenameNo

Implementation Reference

  • The handler function that extracts unique colors from an Aseprite image and creates a new palette.
    async def extract_palette_from_image(
        filename: str,
        max_colors: int = 16,
        output_filename: Optional[str] = None
    ) -> str:
        """Extract a color palette from an existing image.
    
        Args:
            filename: Name of the Aseprite file to extract palette from
            max_colors: Maximum number of colors to extract (default: 16)
            output_filename: Optional filename to save the palette
        """
        try:
            # Validate inputs
            file_path = validate_file_path(filename, must_exist=True)
            
            if max_colors < 1 or max_colors > 256:
                raise ValidationError("max_colors", max_colors, "Max colors must be between 1 and 256")
            
            # Build Lua script
            builder = LuaBuilder()
            builder.open_sprite(str(file_path))
            builder.add_line('local spr = app.activeSprite')
            builder.if_condition('not spr')
            builder.add_line('error("No active sprite")')
            builder.end_if()
            builder.add_line()
            
            # Extract unique colors
            builder.add_line('local colorSet = {}')
            builder.add_line('local colorList = {}')
            builder.add_line()
            
            # Iterate through all pixels in all cels
            builder.for_loop('_, cel', 'ipairs(spr.cels)')
            builder.add_line('local img = cel.image')
            builder.if_condition('img')
            builder.for_loop('y', 0, 'img.height - 1')
            builder.for_loop('x', 0, 'img.width - 1')
            builder.add_line('local pixel = img:getPixel(x, y)')
            builder.add_line('local color = spr.pixelColor.rgba(pixel)')
            builder.if_condition('color.a > 0')  # Skip transparent pixels
            builder.add_line('local key = string.format("%02X%02X%02X", color.r, color.g, color.b)')
            builder.if_condition('not colorSet[key]')
            builder.add_line('colorSet[key] = true')
            builder.add_line('table.insert(colorList, color)')
            builder.if_condition(f'#colorList >= {max_colors}')
            builder.add_line('break')
            builder.end_if()
            builder.end_if()
            builder.end_if()
            builder.end_loop()
            builder.if_condition(f'#colorList >= {max_colors}')
            builder.add_line('break')
            builder.end_if()
            builder.end_loop()
            builder.end_if()
            builder.if_condition(f'#colorList >= {max_colors}')
            builder.add_line('break')
            builder.end_if()
            builder.end_loop()
            builder.add_line()
            
            # Create palette from extracted colors
            builder.add_line('local palette = Palette(#colorList)')
            builder.for_loop('i, color', 'ipairs(colorList)')
            builder.add_line('palette:setColor(i - 1, color)')
            builder.end_loop()
            
            # Apply palette to sprite
            builder.add_line('spr:setPalette(palette)')
            
            # Save if output filename specified
            if output_filename:
                output_path = validate_file_path(output_filename, must_exist=False)
                builder.save_sprite(str(output_path))
            else:
                builder.save_sprite()
            
            builder.add_line('return #colorList')
            
            # Execute script
            cmd = get_command()
            success, output = cmd.execute_lua_script(builder.build())
            
            return f"Extracted palette with up to {max_colors} colors from {file_path}"
            
        except (ValidationError, AsepriteError) as e:
            return f"Failed to extract palette: {e}"
        except Exception as e:
            return f"Unexpected error: {e}"

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ext-sakamoro/AsepriteMCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server