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"
only_face = False
montage_file_path = gimpify.create_random_montage(montages_path, path_to_backgrounds_folder, path_to_faces_folder, only_face)
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.
If the function finds that you already have a json file inside these folders provided (background's and face's), it will try to use this json files to speed up the execution. If you have new images, it will only process this new images (= new paths/names). If, instead, you remove images from the folder, the json will also be updated.
If, for any reason, you don't change the name of an image while the image itself is different, you will have to erase the json (or recreate it, as shown down below).
2.2: The curated way
If you are more sophisticated, you can create both json files with the face parameters before creating a montage, and then call one of the three methods below to obtain the image. If you put new images programmatically you can recreate this json files and use this new images right away.
2.2.1 First, create_face_json and create_background_json
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, just_update=True)
gimpify.create_background_json(path_to_backgrounds_folder, path_to_backgrounds_folder)
Then use this paths to both json files to create random images (shown down below). Keep in mind that one the json is created, if you put new images in the folder, and the option just_update is True and there is already a json file with the parameters, it will automatically add only these new images and erase the ones that were removed. If it is false, it will create the json again. You have now 3 different options regarding the way you want your montages:
2.2.2a 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.2b 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)
This method will not store the image face parameters json.
2.2.2c 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gimpify-watxaut-0.0.1.tar.gz
.
File metadata
- Download URL: gimpify-watxaut-0.0.1.tar.gz
- Upload date:
- Size: 9.2 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | fef55f45ee4522b788b81a398e8a93ef1f39ede62cdd75ffaf999382392da936 |
|
MD5 | 6ecd532f9f9154041d688b10324fe733 |
|
BLAKE2b-256 | fb11a4dc94f5ccc08fc679d9aad872f6057cdb0a67f0287b339427df6b23fe21 |
File details
Details for the file gimpify_watxaut-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: gimpify_watxaut-0.0.1-py3-none-any.whl
- Upload date:
- Size: 10.1 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1868fee0697ee05b2b2a004ba991b0aabfd3e6f4e1300cabda094577c012f61a |
|
MD5 | 22b070e817fdcafc3a9b47f7cc9ff378 |
|
BLAKE2b-256 | dfff9e2e09245c4a6a5fc38fc4868275f574b8b4a774fdb3abe0469ad463aeee |