PIXELGENERATE API Endpoint

Version 2.197 (Release Notes ↗)

Description

Generate random pixel images for machine learning training with the PIXELGENERATE API endpoint. Ideal for creating background samples. Generates a set of random pixels. This endpoint is similar to newimage except that the image contents is filled with random pixels. This is very useful for generating background (negative) samples for feeding Machine Learning training algorithms.

HTTP Methods

GET

HTTP Parameters

Required

Fields Type Description
width Integer Desired image width. If omitted, the height parameter value will be used.
height Integer Desired image height. If omitted, the width parameter value will be used.
key String Your PixLab API Key ↗. Alternatively, you can pass it in the WWW-Authenticate: HTTP header and omit this parameter.

Optional

Fields Type Description
blob Boolean When true, returns the raw image binary instead of a JSON response containing the image URL.
color String Background color (e.g. white, green). Defaults to transparent. Use tr for transparency or hex codes like #cef48e.
export Format Output format (default: PNG). Supported: JPEG, BMP, etc.

HTTP Response

Fields Type Description
status Integer Status code 200 indicates success, any other code indicates failure.
link URL Link to the image 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.

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

This endpoint returns a JSON Object after each call only if the optional blob parameter is not set. Otherwise the image binary contents is returned directly.

Code Samples


import requests
import sys

def generate_random_image(api_key: str, width: int = 300, height: int = 300) -> str:
    """Generate a random pixel image using the PixLab API.
    
    Args:
        api_key: Your PixLab API key.
        width: Width of the generated image in pixels.
        height: Height of the generated image in pixels.
    
    Returns:
        URL of the generated image.
    
    Raises:
        SystemExit: If the API request fails.
    """
    try:
        response = requests.get(
            'https://api.pixlab.io/pixelgenerate',
            params={
                'key': api_key,
                'width': width,
                'height': height
            },
            timeout=10
        )
        response.raise_for_status()
        data = response.json()
        
        if data['status'] != 200:
            print(f"Error: {data.get('error', 'Unknown error')}", file=sys.stderr)
            sys.exit(1)
            
        return data['ssl_link']
    
    except requests.exceptions.RequestException as e:
        print(f"API request failed: {e}", file=sys.stderr)
        sys.exit(1)

if __name__ == "__main__":
    API_KEY = "PIXLAB_API_KEY"  # Replace with your actual API key
    image_url = generate_random_image(API_KEY)
    print(f"Randomly generated image location: {image_url}")
← Return to API Endpoint Listing