API Endpoint Access URL
https://api.pixlab.io/drawrectangles
Get Your API Key & Try DRAWRECTANGLES Now ↗Description
Draw rectangles on input images, ideal for marking detected faces when used with face detection. Essential for developers and creators. Draw as much rectangles as desired on a given input image. This is very useful especially if mixed with facedetect to mark detected faces.
HTTP Methods
POST
HTTP Parameters
Required
Optional
POST Request Body
Allowed Content-Type:
Only JSON data is allowed. the field of interest here is the
So, a typical
Fields | Type | Description |
---|---|---|
img | URL | Input image URL. If you want to upload your image directly from your app, call store before invoking this one. |
cord | Array | JSON array holding the coordinates of the rectangles to be drawn (See below). |
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 command return a JSON object holding the link to the image output. But, if this parameter is set to true then the image binary contents is returned instead. |
color | String | Global border color for each rectangle to be drawn which default to white . Use hex color code such as #cef45f if you want to. |
fillcolor | String | Global fill color for each rectangle to be drawn which default to none (Transparent). Use hex color code if you want to. |
strokewidth | Float | Global stroke (Border) width for each rectangle to be drawn which default to 1.2. |
strokeopacity | Float | Global stroke opacity for each rectangle to be drawn which default to 0.9. |
POST Request Body
Allowed Content-Type:
application/json
Only JSON data is allowed. the field of interest here is the
cord
parameter which must be a JSON array holding the object coordinates of each rectangle to be drawn on the input image. The following are the required parameters for each object in the cord
array:Fields | Type | Description |
---|---|---|
x | Integer | X coordinate of the top left corner. |
y | Integer | Y coordinate of the top left corner. |
width | Integer | Width of the desired rectangle. |
height | Integer | Height of the desired rectangle. |
color | String | Optional border color to apply for this specific rectangle which default to white . Use hex color code such as #cef45f if you want to. |
fillcolor | String | Optional Fill color for this specific rectangle which default to none (Transparent). |
So, a typical
cord
array should look like this (See the example section for a working snippet).
cord = [
{
x: 200,
y: 290,
width: 90,
height: 60
},
{
x: 165,
y: 95,
width: 145,
height: 145,
color: 'red'
}
]
HTTP Response
application/json
if the optional blob parameter is not set.This endpoint returns a JSON response unless the blob parameter is specified, in which case raw image binary data is returned. The JSON response contains the following fields:
Fields | Type | Description |
---|---|---|
status |
Integer | Status code 200 indicates success, any other code indicates failure. |
link |
URL | Link to the processed image stored on pixlab.xyz storage. Configure custom S3 storage via console.pixlab.io. |
id |
String | Unique identifier for the processed image. |
error |
String | Error description when status ≠ 200. |
Code Samples
import requests
import json
from typing import Dict, Any
def draw_rectangle_on_face(image_url: str, api_key: str, coordinates: Dict[str, int]) -> None:
"""Draw rectangles on faces in an image using PixLab API.
Args:
image_url: URL of the image to process.
api_key: PixLab API key.
coordinates: Dictionary containing rectangle coordinates (x, y, width, height).
"""
payload = {
'img': image_url,
'key': api_key,
'cord': [coordinates]
}
try:
response = requests.post(
'https://api.pixlab.io/drawrectangles',
headers={'Content-Type': 'application/json'},
data=json.dumps(payload),
timeout=10
)
response.raise_for_status()
reply = response.json()
if reply['status'] != 200:
print(f"Error: {reply.get('error', 'Unknown error')}")
else:
print(f"Processed image location: {reply['link']}")
except requests.exceptions.RequestException as e:
print(f"API request failed: {e}")
except json.JSONDecodeError:
print("Error decoding API response")
if __name__ == "__main__":
draw_rectangle_on_face(
image_url='http://cf.broadsheet.ie/wp-content/uploads/2015/03/jeremy-clarkson_3090507b.jpg',
api_key='PIXLAB_API_KEY',
coordinates={
"x": 164,
"y": 95,
"width": 145,
"height": 145
}
)
fetch('https://api.pixlab.io/drawrectangles', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
img: 'http://cf.broadsheet.ie/wp-content/uploads/2015/03/jeremy-clarkson_3090507b.jpg',
key: 'PIXLAB_API_KEY',
cord: [
{
x: 164,
y: 95,
width: 145,
height: 145
}
]
})
})
.then(response => response.json())
.then(reply => {
if (reply.status !== 200) {
console.log(reply.error);
} else {
console.log("Pic location: " + reply.link);
}
})
.catch(error => console.error('Error:', error));
<?php
$data = [
'img' => 'http://cf.broadsheet.ie/wp-content/uploads/2015/03/jeremy-clarkson_3090507b.jpg',
'key' => 'PIXLAB_API_KEY',
'cord' => [
[
'x' => 164,
'y' => 95,
'width' => 145,
'height' => 145
]
]
];
$ch = curl_init('https://api.pixlab.io/drawrectangles');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
curl_close($ch);
$reply = json_decode($response, true);
if ($reply['status'] != 200) {
echo $reply['error'];
} else {
echo "Pic location: " . $reply['link'];
}
require 'net/http'
require 'uri'
require 'json'
uri = URI.parse('https://api.pixlab.io/drawrectangles')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json')
request.body = {
'img' => 'http://cf.broadsheet.ie/wp-content/uploads/2015/03/jeremy-clarkson_3090507b.jpg',
'key' => 'PIXLAB_API_KEY',
'cord' => [
{
'x' => 164,
'y' => 95,
'width' => 145,
'height' => 145
}
]
}.to_json
response = http.request(request)
reply = JSON.parse(response.body)
if reply['status'] != 200
puts reply['error']
else
puts "Pic location: #{reply['link']}"
end