Pixaven Blog

Leverage Face Detection When Resizing With Fit Mode

Leverage Face Detection When Resizing With Fit Mode

Fit mode is one our most popular modes for image resizing. It ensures 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 and allow to focus resized image on a single face or all the faces found in the input image.

To use face-directed resizing with fit mode simply set face or faces (plural) as value for 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 and will make 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"

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

const Pixaven = require("pixaven");

* Instantiate `pix`

const pix = new Pixaven("your-api-key");

* Resize the input image with `fit` mode
* and focus the output image on a single, most prominent face

        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.