# Image2Pixel

> Turn images into pixel-perfect retro art


## Overview

- **Endpoint**: `https://fal.run/fal-ai/image2pixel`
- **Model ID**: `fal-ai/image2pixel`
- **Category**: image-to-image
- **Kind**: inference
**Tags**: post-processing, pixel-art



## Pricing

- **Price**: $0 per compute seconds

For more details, see [fal.ai pricing](https://fal.ai/pricing).

## API Information

This model can be used via our HTTP API or more conveniently via our client libraries.
See the input and output schema below, as well as the usage examples.


### Input Schema

The API accepts the following input parameters:


- **`image_url`** (`string`, _required_):
  The image URL to process into improved pixel art
  - Examples: "https://storage.googleapis.com/falserverless/example_inputs/image2pixel-input.jpg"

- **`max_colors`** (`integer`, _optional_):
  Maximum number of colors in the output palette. Set None to disable limit. Default value: `32`
  - Default: `32`
  - Range: `1` to `256`

- **`auto_color_detect`** (`boolean`, _optional_):
  Enable automatic detection of optimal number of colors.
  - Default: `false`

- **`fixed_palette`** (`list<string>`, _optional_):
  Optional fixed color palette as hex strings (e.g., ['#000000', '#ffffff']).
  - Array of string

- **`detect_method`** (`DetectMethodEnum`, _optional_):
  Scale detection method to use. Default value: `"auto"`
  - Default: `"auto"`
  - Options: `"auto"`, `"runs"`, `"edge"`

- **`scale`** (`integer`, _optional_):
  Force a specific pixel scale. If None, auto-detect.
  - Range: `1` to `64`

- **`downscale_method`** (`DownscaleMethodEnum`, _optional_):
  Downscaling method to produce the pixel-art output. Default value: `"dominant"`
  - Default: `"dominant"`
  - Options: `"dominant"`, `"median"`, `"mode"`, `"mean"`, `"content-adaptive"`

- **`trim_borders`** (`boolean`, _optional_):
  Trim borders of the image.
  - Default: `false`

- **`transparent_background`** (`boolean`, _optional_):
  Remove background of the image. This will check for contiguous color regions from the edges after correction and make them transparent.
  - Default: `false`

- **`cleanup_morph`** (`boolean`, _optional_):
  Apply morphological operations to remove noise.
  - Default: `false`

- **`cleanup_jaggy`** (`boolean`, _optional_):
  Remove isolated diagonal pixels (jaggy edge cleanup).
  - Default: `false`

- **`snap_grid`** (`boolean`, _optional_):
  Align output to the pixel grid. Default value: `true`
  - Default: `true`

- **`alpha_threshold`** (`integer`, _optional_):
  Alpha binarization threshold (0-255). Default value: `128`
  - Default: `128`
  - Range: `0` to `255`

- **`dominant_color_threshold`** (`float`, _optional_):
  Dominant color threshold (0.0-1.0). Default value: `0.05`
  - Default: `0.05`
  - Range: `0` to `1`

- **`background_tolerance`** (`integer`, _optional_):
  Background tolerance (0-255).
  - Default: `0`
  - Range: `0` to `255`

- **`background_mode`** (`BackgroundModeEnum`, _optional_):
  Controls where to flood-fill from when removing the background. Default value: `"corners"`
  - Default: `"corners"`
  - Options: `"edges"`, `"corners"`, `"midpoints"`

- **`sync_mode`** (`boolean`, _optional_):
  If `True`, the media will be returned as a data URI and the output data won't be available in the request history.
  - Default: `false`



**Required Parameters Example**:

```json
{
  "image_url": "https://storage.googleapis.com/falserverless/example_inputs/image2pixel-input.jpg"
}
```

**Full Example**:

```json
{
  "image_url": "https://storage.googleapis.com/falserverless/example_inputs/image2pixel-input.jpg",
  "max_colors": 32,
  "detect_method": "auto",
  "downscale_method": "dominant",
  "snap_grid": true,
  "alpha_threshold": 128,
  "dominant_color_threshold": 0.05,
  "background_mode": "corners"
}
```


### Output Schema

The API returns the following output format:

- **`pixel_scale`** (`integer`, _required_):
  The detected pixel scale of the input.
  - Examples: 21

- **`palette`** (`list<string>`, _required_):
  The palette of the processed media.
  - Array of string
  - Examples: ["#000000","#252524","#282826","#2b2b2a","#323232","#4c4c4c","#d57322","#dbdbdc","#f6922b","#fefefe"]

- **`num_colors`** (`integer`, _required_):
  The number of colors in the processed media.
  - Examples: 10

- **`images`** (`list<ImageFile>`, _required_):
  The processed pixel-art image (PNG) and the scaled image (PNG).
  - Array of ImageFile
  - Examples: [{"width":1008,"url":"https://storage.googleapis.com/falserverless/example_outputs/image2pixel-output.png","file_name":"image2pixel-output.png","height":1008,"content_type":"image/png"},{"width":48,"url":"https://storage.googleapis.com/falserverless/example_outputs/image2pixel-output-scaled.png","file_name":"image2pixel-output-scaled.png","height":48,"content_type":"image/png"}]



**Example Response**:

```json
{
  "pixel_scale": 21,
  "palette": [
    "#000000",
    "#252524",
    "#282826",
    "#2b2b2a",
    "#323232",
    "#4c4c4c",
    "#d57322",
    "#dbdbdc",
    "#f6922b",
    "#fefefe"
  ],
  "num_colors": 10,
  "images": [
    {
      "width": 1008,
      "url": "https://storage.googleapis.com/falserverless/example_outputs/image2pixel-output.png",
      "file_name": "image2pixel-output.png",
      "height": 1008,
      "content_type": "image/png"
    },
    {
      "width": 48,
      "url": "https://storage.googleapis.com/falserverless/example_outputs/image2pixel-output-scaled.png",
      "file_name": "image2pixel-output-scaled.png",
      "height": 48,
      "content_type": "image/png"
    }
  ]
}
```


## Usage Examples

### cURL

```bash
curl --request POST \
  --url https://fal.run/fal-ai/image2pixel \
  --header "Authorization: Key $FAL_KEY" \
  --header "Content-Type: application/json" \
  --data '{
     "image_url": "https://storage.googleapis.com/falserverless/example_inputs/image2pixel-input.jpg"
   }'
```

### Python

Ensure you have the Python client installed:

```bash
pip install fal-client
```

Then use the API client to make requests:

```python
import fal_client

def on_queue_update(update):
    if isinstance(update, fal_client.InProgress):
        for log in update.logs:
           print(log["message"])

result = fal_client.subscribe(
    "fal-ai/image2pixel",
    arguments={
        "image_url": "https://storage.googleapis.com/falserverless/example_inputs/image2pixel-input.jpg"
    },
    with_logs=True,
    on_queue_update=on_queue_update,
)
print(result)
```

### JavaScript

Ensure you have the JavaScript client installed:

```bash
npm install --save @fal-ai/client
```

Then use the API client to make requests:

```javascript
import { fal } from "@fal-ai/client";

const result = await fal.subscribe("fal-ai/image2pixel", {
  input: {
    image_url: "https://storage.googleapis.com/falserverless/example_inputs/image2pixel-input.jpg"
  },
  logs: true,
  onQueueUpdate: (update) => {
    if (update.status === "IN_PROGRESS") {
      update.logs.map((log) => log.message).forEach(console.log);
    }
  },
});
console.log(result.data);
console.log(result.requestId);
```


## Additional Resources

### Documentation

- [Model Playground](https://fal.ai/models/fal-ai/image2pixel)
- [API Documentation](https://fal.ai/models/fal-ai/image2pixel/api)
- [OpenAPI Schema](https://fal.ai/api/openapi/queue/openapi.json?endpoint_id=fal-ai/image2pixel)

### fal.ai Platform

- [Platform Documentation](https://docs.fal.ai)
- [Python Client](https://docs.fal.ai/clients/python)
- [JavaScript Client](https://docs.fal.ai/clients/javascript)
