COLORIZE API Endpoint

Version 2.197 (Release Notes ↗)

Description

Perform automatic image colorization on grayscale images using deep learning models. The process may take a few seconds and is available from the Dev Plan. Based on the Colorful Image Colorization paper. Perform automatic image colorization on an input Grayscale image using deep learning models. A typical input image and the output result should look like the following after processing:

Input Picture

Grayscale picture

After Colorization

colorized picture


The colorization is a heavy process, should take few seconds to execute and is available starting from the Dev Plan and up. The PixLab model is based on the Colorful Image Colorization paper.

HTTP Methods

GET, POST

HTTP Parameters

Required

Fields Type Description
img URL Input grayscale picture URL. If you want to upload your image directly from your app, then submit a multipart/form-data POST request.
key String Your PixLab API Key ↗. You can also embed your key in the WWW-Authenticate: HTTP header and omit this parameter if you want to.

Optional

Fields Type Description
blob Boolean By default, this API endpoint returns a JSON Object holding the link to the image output. Set to true to receive the raw image binary data instead.

POST Request Body

Use when submitting POST requests instead of GET:

Allowed Content-Types:

multipart/form-data
application/json

Use multipart/form-data for direct file uploads (check the REST API code samples or The PixLab Github Repository↗ for implementation examples). For JSON requests, the media file must be hosted externally. Call store to upload images before processing with this endpoint.

HTTP Response

Required

The endpoint expects the following parameters in the request body:

Fields Type Description
key String Your API key
image URL/String Input image URL or base64 encoded string

Optional

Fields Type Description
blob Boolean Return binary image data instead of JSON response

The endpoint returns application/json if the optional blob parameter is not set.

This API endpoint returns a JSON Object after each call only if the optional blob parameter is not set. Otherwise the image binary contents is returned instead. The following are the JSON fields returned in response body:

Fields Type Description
status Integer Status code 200 indicates success, any other code indicates failure.
link URL Link to the media output which is usually stored on the pixlab.xyz storage server unless you set your own S3 keys (refer to your dashboard ↗ on how to do that).
id String Unique media ID.
error String Error message if status != 200.

Code Samples


import requests


def colorize_image(image_url: str, api_key: str) -> str:
    """Colorize a grayscale image using PixLab API.
    
    Args:
        image_url: URL of the grayscale image to colorize.
        api_key: PixLab API key.
        
    Returns:
        URL of the colorized image.
        
    Raises:
        requests.exceptions.RequestException: If the API request fails.
        ValueError: If the API returns an error.
    """
    try:
        response = requests.get(
            'https://api.pixlab.io/colorize',
            params={'img': image_url, 'key': api_key},
            timeout=10
        )
        response.raise_for_status()
        data = response.json()
        
        if data['status'] != 200:
            raise ValueError(data.get('error', 'Unknown error from API'))
            
        return data['link']
        
    except requests.exceptions.RequestException as e:
        raise requests.exceptions.RequestException(f"API request failed: {e}")
    except json.JSONDecodeError:
        raise ValueError("Invalid JSON response from API")


if __name__ == "__main__":
    try:
        colorized_url = colorize_image(
            'https://storage.googleapis.com/i2s/tiger.jpg',
            'PIXLAB_API_KEY'
        )
        print(f"Link to the colorized picture: {colorized_url}")
    except Exception as e:
        print(f"Error: {e}")
← Return to API Endpoint Listing