Skip to main content

Just a package to create automatic face image montages

Project description

Gimpify

This repository is intended for creating automatic montages: it pastes faces onto others faces. That's about it.

DISCLAIMER

  • The aim of this package is not to make it beautiful, it is to make it a nightmare
  • I know there is still room for improvement on the maths for pasting images. I will work on it when I have time. Help appreciated!

Installation

pip3 install gimpify-watxaut

This package uses face-recognition from Adam Geitgey to get the faces and pillow to modify and paste them. Both get installed when installing this package.

How to run?

1. Search for your images

First, you will need two separate folders, one with 'backgrounds' and another with 'faces'. When I talk about backgrounds I mean photographs with people AND faces visible. In fact, only the faces are needed, no need to show any other part of the body. One example of background:

When I talk about faces, I mean PNG images with only the face visible, like this one:

And the final result should be close to this one:

As you see, the math is a little off, I know it.

When you have this two folders populated, you are ready to start the second phase.

2.1 The fast approach

If you need to test the power of this package fast, you can do the following:

import gimpify
montages_path = "path/out/montages"
path_to_faces_folder = "path/to/folder/faces"
path_to_backgrounds_folder = "path/to/folder/backgrounds"
montage_file_path = gimpify.create_random_montage(montages_path, path_to_backgrounds_folder, path_to_faces_folder)

In the montage_file_path variable will be the path to your newly created montage. This will create a json file inside the images paths so that the next time will try to get the json instead of loading again the images. CAUTION: if you put new images, you will need to erase the json to force the creation again.

2.2: create_face_json and create_background_json

If you are more sophisticated, you can create both json files with the face parameters before creating a montage, so if you put new images programmatically you can recreate this json files and use this new images right away.

The following code will get the aforementioned done:

import gimpify
path_to_faces_folder = "path/to/folder/faces"
path_to_backgrounds_folder = "path/to/folder/backgrounds"
json_faces = "path/to/json/faces.json"
json_backgrounds = "path/to/json/backgrounds.json"
gimpify.create_face_json(path_to_faces_folder, path_to_faces_folder)
gimpify.create_background_json(path_to_backgrounds_folder, path_to_backgrounds_folder)

Then use this paths to both jsons to create random images. You have now 3 different options regarding the way you want your montages:

2.2.1 Use gimpify.create_random_montage

We saw this method early on, but it's worth mentioning. It takes 4 arguments:

  • :param montage_folder_path: {str} folder to save the montage
  • :param b_path: {str} folder background path or json path with the face params. If folder provided, the 'backgrounds.json' file will be created inside the backgrounds' folder. If json provided, it will use the json to load the params (faster)
  • :param f_path: {str} folder face path or json path with the face's params. If folder provided, the 'faces.json' file will be created inside the faces' folder. If json provided, it will use the json to load the params (faster)
  • :param only_face: {bool}. False by default. Will leave hair and chin if False, else will crop the face

The use for this one is the following:

import gimpify
json_faces = "path/to/json/faces.json"
json_backgrounds = "path/to/json/backgrounds.json"
montage_folder_path = "path/out/montages"
only_face = False
path_to_new_image = gimpify.create_random_montage(montage_folder_path, json_backgrounds, json_faces, only_face)

This way, the module does not create the json params again and thus increasing the speed of the creation.

2.2.2 Use gimpify.create_montage_for_background

Use this method if you want to create a montage for a background of your pleasing, a new photograph that you just made or you need a fast processing for just a background and you don't need to process all the others. E.g.: you send a selfie to a bot and the bot answers with a montage. It takes 4 arguments:

  • :param montage_folder_path: {str} folder to save the montage
  • :param im_b_path: {str} with the background image path
  • :param f_path: {str} folder face path or json path with the face's params. If folder provided, the 'faces.json' file will be created inside the faces' folder
  • :param only_face: {bool} Whether to crop the hair and chin of the face or not
import gimpify
json_faces = "path/to/json/faces.json"
im_b_path = "path/to/background_image.png"
montage_folder_path = "path/out/montages"
only_face = False
path_to_new_image = gimpify.create_montage_for_background(montage_folder_path, im_b_path, json_faces, only_face)

2.2.3 Use gimpify.create_montage

This method is the lowest level available and it returns a PIL Image. It takes in 3 arguments:

  • :param im_background: {str, dict} str if is an image path or dict if it comes from a json read from a background's folder
  • :param json_faces: {dict} json read with all the information about the faces' parameters
  • :param only_face: {bool}. False by default. Will leave hair and chin if False, else will crop the face

-> Returns a PIL Image.Image variable with the image for you to do whatever you want.

The use is the following one:

import gimpify
import json

f_faces = open("path/to/json/faces.json", "r")
json_faces: dict = json.load(f_faces)
f_faces.close()
im_background = "path/to/background_image.png"
only_face = False
path_to_new_image = gimpify.create_montage(im_background, json_faces, only_face)

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

gimpify-watxaut-0.0.1a4.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

gimpify_watxaut-0.0.1a4-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file gimpify-watxaut-0.0.1a4.tar.gz.

File metadata

  • Download URL: gimpify-watxaut-0.0.1a4.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.4

File hashes

Hashes for gimpify-watxaut-0.0.1a4.tar.gz
Algorithm Hash digest
SHA256 c1b34bd6676721b94bf0a37f7d1ae23665f8224c674f30aa273b5b8ff968aa69
MD5 a693f29d25e4df03f32b9cf328dca10d
BLAKE2b-256 1ee610f03932cc6897619af26a9e80949817841f9f4a38305174dcb15c44b3db

See more details on using hashes here.

File details

Details for the file gimpify_watxaut-0.0.1a4-py3-none-any.whl.

File metadata

  • Download URL: gimpify_watxaut-0.0.1a4-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.4

File hashes

Hashes for gimpify_watxaut-0.0.1a4-py3-none-any.whl
Algorithm Hash digest
SHA256 3f5d5a523a263cf879f10b9a92c18092f81b0ffb2de74e5eb06a8722593be6b9
MD5 16df3f6a4d733f6092916f0c4472e03b
BLAKE2b-256 b7cbc764ed61714f53aa99c40abcd017ad1e022bf32d823b8c6edd20cc99a8ec

See more details on using hashes here.

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