IMGDIFF API Endpoint

Version 2.197 (Release Notes ↗)

Description

Compute the difference between two images using the mean square error metric and optionally return the reconstructed image. Note that the two images must be exactly of the same width & height. Call resize or crop before to fit your images to the same dimension if any.

HTTP Methods

GET

HTTP Parameters

Required

Fields Type Description
src URL Source image URL. If you want to upload your image directly from your app, call store before invoking this endpoint and use the output link.
target URL Target image to compare to. Again, call store if you want to upload an image directly and use the output link.
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 endpoint returns a JSON Object with the link to the reconstructed image. Set this to true to receive the raw image binary data instead.

HTTP Response

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).
diff Float Computed difference between the two images.
id String Unique media ID.
error String Error message if status != 200.

The API 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 reconstructed image binary contents is returned instead.

Code Samples


import requests
from typing import Dict, Any

def compare_images(
    src_url: str,
    target_url: str,
    api_key: str,
    api_endpoint: str = "https://api.pixlab.io/imgdiff"
) -> None:
    """Compare two images and output the difference and reconstructed image.
    
    Args:
        src_url: URL of the source image
        target_url: URL of the target image
        api_key: PixLab API key
        api_endpoint: PixLab API endpoint (defaults to imgdiff)
        
    Returns:
        None: Outputs results to stdout
    """
    try:
        params = {
            'src': src_url,
            'target': target_url,
            'key': api_key
        }
        
        response = requests.get(api_endpoint, params=params, timeout=10)
        response.raise_for_status()
        
        data: Dict[str, Any] = response.json()
        
        if data['status'] != 200:
            print(f"Error: {data.get('error', 'Unknown error')}")
        else:
            print(f"Diff Output: {data.get('diff', 'N/A')}")
            print(f"Reconstructed image link: {data.get('link', 'N/A')}")
            
    except requests.exceptions.RequestException as e:
        print(f"API request failed: {e}")
    except json.JSONDecodeError:
        print("Error: Invalid JSON response from API")
    except KeyError as e:
        print(f"Error: Missing expected key in response - {e}")

if __name__ == "__main__":
    # Example usage
    source_image = 'https://pixlab.io/images/jdr.jpg'
    target_image = 'https://pixlab.io/images/jdr_draw.jpg'
    api_key = 'My_Key'  # Replace with actual API key
    
    compare_images(source_image, target_image, api_key)
← Return to API Endpoint Listing