API Endpoint Access URL
https://api.pixlab.io/imgdiff
Get Your API Key & Try IMGDIFF Now ↗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)
// Compute the difference between two images and output the reconstructed image and the diff output.
// Keep in mind that the two images must be of the same size or call 'resize' or 'crop' before to
// fit the images to the same dimension.
const src = 'https://pixlab.io/images/jdr.jpg'; // Source image which is the famous Michael Jordan's crying face.
const target = 'https://pixlab.io/images/jdr_draw.jpg'; // Target image which is the same Jordan's face but a MEME is drawn on top of it.
const apiKey = 'My_Key';
fetch(`https://api.pixlab.io/imgdiff?src=${encodeURIComponent(src)}&target=${encodeURIComponent(target)}&key=${encodeURIComponent(apiKey)}`)
.then(response => response.json())
.then(reply => {
if (reply.status !== 200) {
console.log(reply.error);
} else {
console.log("Diff Output: " + reply.diff);
console.log("Reconstructed image link: " + reply.link);
}
})
.catch(error => console.error('Error:', error));
<?php
$src = 'https://pixlab.io/images/jdr.jpg';
$target = 'https://pixlab.io/images/jdr_draw.jpg';
$key = 'My_Key';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.pixlab.io/imgdiff?src=' . urlencode($src) . '&target=' . urlencode($target) . '&key=' . urlencode($key));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$reply = json_decode($response, true);
if ($reply['status'] != 200) {
echo $reply['error'];
} else {
echo "Diff Output: " . $reply['diff'] . "\n";
echo "Reconstructed image link: " . $reply['link'] . "\n";
}
require 'net/http'
require 'uri'
require 'json'
# Compute the difference between two images and output the reconstructed image and the diff output.
# Keep in mind that the two images must be of the same size or call 'resize' or 'crop' before to
# fit the images to the same dimension.
src = 'https://pixlab.io/images/jdr.jpg' # Source image which is the famous Michael Jordan's crying face.
target = 'https://pixlab.io/images/jdr_draw.jpg' # Target image which is the same Jordan's face but a MEME is drawn on top of it.
uri = URI.parse('https://api.pixlab.io/imgdiff')
params = {
'src' => src,
'target' => target,
'key' => 'My_Key'
}
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get_response(uri)
reply = JSON.parse(response.body)
if reply['status'] != 200
puts reply['error']
else
puts "Diff Output: #{reply['diff']}"
puts "Reconstructed image link: #{reply['link']}"
end
Similar API Endpoints
bg-remove, nsfw, sfw, ocr, facedetect, docscan, tagimg, crop, mogrify, facemotion, facelookup ↗, screencapture