Optidash Blog

Leverage Face Detection When Resizing With Fit Mode

Optimize your image resizing with our fit mode, now enhanced with face-detection. Ensure your images are perfectly framed, focusing on single or multiple faces.

Fit mode is one of our most popular modes for image resizing. It ensures that the area described by the dimensions is entirely covered with the image and crops out any outstanding image parts that fall outside of the desired frame size. Today, we extended that mode with face-detection capabilities, allowing the resized image to focus on a single face or all the faces found in the input image.

Gravity In Fit Mode

To use face-directed resizing with fit mode, simply set face or faces (plural) as the value for the gravity parameter. For example:

{
    "resize": {
        "mode": "fit",
        "gravity": "face",
        "width": 640,
        "height": 480
    }
}

face gravity will center the output image on the largest (usually most prominent) detected face, while faces will compute a union of bounding boxes of all faces found in the image and focus the image on the center of that union. In other words, faces will make the best efforts to contain all of the faces in the output image. If no faces were found, the API will fall back to the default center gravity.

Given the input image 750px × 350px
Resize it with "fit" mode setting "width" to 250px, "height" to 200px and crop gravity to "face"

Quick Example

Here’s a quick example of said oprtation implemented with our official Node integration:

const Optidash = require("optidash");

const opti = new Optidash("your-api-key");

opti
    .upload("/path/to/image.jpg")
    .resize({
        mode: "fit",
        gravity: "face",
        width: 640,
        height: 480
    })
    .toJSON((err, meta) => {
        if (err) {
            return console.log(err);
        }
    });

Should you have questions about fit resizing, face detection or requests for other features do not hesitate to contact us anytime.