Skip to main content

Warp, morph and average human faces!

Project description

Warp, average and morph human faces!
Scripts will automatically detect frontal faces and skip images if none is detected.

Built with Python, OpenCV, Numpy, Scipy, Stasm.

Supported on Python 2.7, Python 3.6+ and OpenCV >= 3 (tested with OpenCV 3.4.1)
Tested on macOS Mojave and 64bit Linux (dockerized).



Use as local command-line utility

$ git clone

Morphing Faces

Morph from a source to destination image:

python facemorpher/ --src=<src_imgpath> --dest=<dest_imgpath> --plot

Morph through a series of images in a folder:

python facemorpher/ --images=<folder> --out_video=out.avi

All options listed in (pasted below):

Morph from source to destination face or
Morph through all images in a folder

Usage: (--src=<src_path> --dest=<dest_path> | --images=<folder>)
            [--width=<width>] [--height=<height>]
            [--num=<num_frames>] [--fps=<frames_per_second>]
            [--out_frames=<folder>] [--out_video=<filename>]
            [--plot] [--background=(black|transparent|average)]

    -h, --help              Show this screen.
    --src=<src_imgpath>     Filepath to source image (.jpg, .jpeg, .png)
    --dest=<dest_imgpath>   Filepath to destination image (.jpg, .jpeg, .png)
    --images=<folder>       Folderpath to images
    --width=<width>         Custom width of the images/video [default: 500]
    --height=<height>       Custom height of the images/video [default: 600]
    --num=<num_frames>      Number of morph frames [default: 20]
    --fps=<fps>             Number frames per second for the video [default: 10]
    --out_frames=<folder>   Folder path to save all image frames
    --out_video=<filename>  Filename to save a video
    --plot                  Flag to plot images [default: False]
    --background=<bg>       Background of images to be one of (black|transparent|average) [default: black]
    --version               Show version.

Averaging Faces

Average faces from all images in a folder:

python facemorpher/ --images=<images_folder> --out=average.png

All options listed in (pasted below):

Face averager

Usage: --images=<images_folder> [--blur] [--plot]
            [--width=<width>] [--height=<height>]
            [--out=<filename>] [--destimg=<filename>]

    -h, --help             Show this screen.
    --images=<folder>      Folder to images (.jpg, .jpeg, .png)
    --blur                 Flag to blur edges of image [default: False]
    --width=<width>        Custom width of the images/video [default: 500]
    --height=<height>      Custom height of the images/video [default: 600]
    --out=<filename>       Filename to save the average face [default: result.png]
    --destimg=<filename>   Destination face image to overlay average face
    --plot                 Flag to display the average face [default: False]
    --background=<bg>      Background of image to be one of (black|transparent|average) [default: black]
    --version              Show version.

Steps (facemorpher folder)

1. Locator

  • Locates face points (using stasm)

  • For a different locator, return an array of (x, y) control face points

2. Aligner

  • Align faces by resizing, centering and cropping to given size

3. Warper

  • Given 2 images and its face points, warp one image to the other

  • Triangulates face points

  • Affine transforms each triangle with bilinear interpolation

4a. Morpher

  • Morph between 2 or more images

4b. Averager

  • Average faces from 2 or more images


Optional blending of warped image:

  • Weighted average

  • Alpha feathering

  • Poisson blend

Examples - Being John Malkovich

Create a morphing video between the 2 images:

> python facemorpher/ --src=alyssa.jpg --dest=john_malkovich.jpg

(out.avi played and recorded as gif)


Save the frames to a folder:

> python facemorpher/ --src=alyssa.jpg --dest=john_malkovich.jpg
--out_frames=out_folder --num=30

Plot the frames:

> python facemorpher/ --src=alyssa.jpg --dest=john_malkovich.jpg
--num=12 --plot

Average all face images in a folder:

85 images used

> python facemorpher/ --images=images --blur --background=transparent
--width=220 --height=250

Use as pip library

$ pip install facemorpher


Additional options are exactly the same as the command line

import facemorpher

# Get a list of image paths in a folder
imgpaths = facemorpher.list_imgpaths('imagefolder')

# To morph, supply an array of face images:
facemorpher.morpher(imgpaths, plot=True)

# To average, supply an array of face images:
facemorpher.averager(['image1.png', 'image2.png'], plot=True)

Once pip installed, 2 binaries are also available as a command line utility:

$ facemorpher --src=<src_imgpath> --dest=<dest_imgpath> --plot
$ faceaverager --images=<images_folder> --plot

Try out in a docker container

Mount local folder to /images in docker container, run it and enter a bash session. –rm removes the container when you close it.

$ docker run -v  /Users/alyssa/Desktop/images:/images --name py3 --rm -it jjanzic/docker-python3-opencv bash

Once you’re in the container, install facemorpher and try the examples listed above

root@0dad0912ebbe:/# pip install facemorpher
root@0dad0912ebbe:/# facemorpher --src=<img1> --dest=<img2> --plot



Build & publish Docs




Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

facemorpher-5.2.1.tar.gz (13.1 kB view hashes)

Uploaded Source

Built Distributions

facemorpher-5.2.1-py3-none-any.whl (17.6 kB view hashes)

Uploaded Python 3

facemorpher-5.2.1-py2-none-any.whl (20.9 kB view hashes)

Uploaded Python 2

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page