DRAWRECTANGLES API Endpoint

Version 2.197 (Release Notes ↗)

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
FieldsTypeDescription
imgURLInput image URL. If you want to upload your image directly from your app, call store before invoking this one.
cordArrayJSON array holding the coordinates of the rectangles to be drawn (See below).
keyStringYour PixLab API Key ↗. You can also embed your key in the WWW-Authenticate: HTTP header and omit this parameter if you want to.

Optional
FieldsTypeDescription
blobBooleanBy 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.
colorStringGlobal border color for each rectangle to be drawn which default to white. Use hex color code such as #cef45f if you want to.
fillcolorStringGlobal fill color for each rectangle to be drawn which default to none (Transparent). Use hex color code if you want to.
strokewidthFloatGlobal stroke (Border) width for each rectangle to be drawn which default to 1.2.
strokeopacityFloatGlobal 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:
FieldsTypeDescription
xIntegerX coordinate of the top left corner.
yIntegerY coordinate of the top left corner.
widthIntegerWidth of the desired rectangle.
heightIntegerHeight of the desired rectangle.
colorStringOptional border color to apply for this specific rectangle which default to white. Use hex color code such as #cef45f if you want to.
fillcolorStringOptional 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
        }
    )
← Return to API Endpoint Listing