Ever since I saw the post [1] from the author of
Papers, please I wanted to create something similar for my game. And I did not have any other work needed to be done, so I could finally focus on the development of Get a girlfriend.
In this post I will describe how I generated 1000 random faces and what steps I had to undergo.
Get a girlfriend channels you can follow
TwitterFacebookYoutubeGreenlightOfficial websiteThanks to the time magic of writing this post-mortem I can post here what I have actually achieved.
These are faces my Python script (whole script near the end of the article) randomly generated, with then applied Cutout filter.
Final product.And this is a concept of how it could look in my game "Get a girlfriend". (feel free to comment on how I could improve the visuals or other aspects of the game, this is edited screenshot from my accepted Greenlight submission).
First I downloaded some photos, portraits.
I have used vectorizer.io on every single photo, played with parameters and then removed the background in Photoshop.
Then I installed psd-tools so I could work with Photoshop psd files. First I wanted to use Powershell, but there were complications with getting
Pillow (PIL, image processing library) for Python 3.4.0, so I just used Ubuntu Subsystem, where everything went without a problem.
Okay, so I need to pick regions of the face somehow and then save them and arrange on a new face. Easy, right? This was my first try on how to do it:
I wrote a little python script to test if the PSD loading works, to prepare for the main script.
It outputs:
Okay, even after colorization of the manually pasted eyes and nose+mouth from different face, this looks horrible :D Gotta find a better way.
Here I tried different Photoshop filters to hide the imperfections.
This kind of face part segmentation is not enough, when I interchange the eyes it still is recognizable, even after filter applied. So I made the segments more precise and I also added overlapping hair layer.
Now I remove the eyes and nose+mouth from the faces.
I save eyes individually and fix asymmetry (in cases of faces with hair overlapping eyes).
Here I have sets of eyes and nose+mouth images.. yeah :D
The preparation of all this took me a few hours, now let's do the script!!
All our images and their layer info , we will need it
Some files turn out okay, but some seem a little... off :D
The problem was caused by padding of certain images. Simple crop solves it.
At this stage the generating is fine, but now it needs some serious polishing,also I need to implement a hair overlay. I will just use the whole hair part and paste it as a new layer on the final face+nose+eyes product.
It works!
But there is a problem with transparent edges of the layerand the mask. I am not sure how I managed to mess this up. And it stays even after applying a filter, so I definitely need to fixit.
Problem solved with
Image.alpha_composite! (Image.paste andmask had wrong blending, thank you
P.Melch). The line is gone
The result after applying a photoshop cutout filter(params 3,1,3) is looking pretty good.
I googled and did not find any satisfying solution forcreating the PS Cutout effect programmatically, except very low level stuffusing Sobel filters, median filters and dithering – all of which would bedoable, but I am looking for something more time efficient for me.
And I found it. I have noticed that vectorizer.io can producevery similar output to what I want.
(8 color, 10% blur, sort by area DESC)
So now I will look for some libraries that can dovectorization.
I found some low level libraries and there is just no timefor that.
I will try GIMP plugin, first I try to find equivalent of PSfilter in gimp.
I found FU_artist_cutout.scm for GIMP, which is directequivalent of cutout filter!!
After copying it to gimp scripts folder(Edit->Preferences->Folders->Scripts) and Refershing cache fromFilters menu, I now have the option to use it
It crashed at first, lets try again.
I also found G’MIC utility for image filters, it has API andI will use it if GIMP method fails. (
gimpchat.com)
Lack of live previewin GIMP kind of sucks for initial testing of parameters, I get this 'meh' result:
(notice orange spots around nose and mouth)
Here I try G’MIC online version, and it’s looking quite good
Setting (5,0,2)
Now I will try to reproduce this in command line environment.
As it turns out, gmic can only use scripts, it does not have a command line parameter for cutout effect. In the file
gmic_stdlib.gmic are definitions of filters, so it should suffice to find the definition for cutout effect and copy it
gmic_stdlib.gmic. Whats important are those lines:
fx_cutout :
-repeat $! -l[$>] -split_opacity -l[0]
-
median {10-$3}
-
quantize $1
--area. 0,1 med=${-med} -rm.
-inpaint_holes {$med*$2%},0,1
-if $4 -n 0,255 -endif-endl -a c -endl -done
Best settings for the Cutout filter seems to be the (5,0,2), so I edit the parameters like so:
-
median8-
quantize 5--area. 0,1 med=${-med} -rm.
-inpaint_holes {$med*
0%},0,1
Windows versionof gmic has a problem, I will try using gmic script instead of cmd argumentsonly.
I have created following G'MIC script
Which when run
Produces thefollowing output. Of course that another problem arises :D
Yep, thebackground is not kept transparent how it is supposed to. Needs to be fixed.
Well, as it turnsout, this weird background shows only in Windows Explorer and Picture Viewer.In Photoshop its black. So theoretically, when we get rid of the Alpha channel,it stays black and it will be possible to use it as a „card“ in game, next tothe chat window.
Okay, so now Ionly plug in the command to my python script and set a few hundred iterationsfor some lovely results.
Turns outthat the script runs okay with gmic.exe, but for some *** reason notwith gmic in Ubuntu subsystem (some collision with $! bash operator). Because of that I will generate the images inpython and then do the post-processing in Powershell/Cmd.
Here is the whole
Python script / Face generator.And here is the script in action, generating faces
Then I ran the script for gmic. All images are randomly generated, but sometimes happens that theoriginal face parts are assigned back to the face, like in this case.
The script which applied the cutout effect to all of the 1000 images (Phenom II X4 955, about 20 minutes):
Here is a few faces generated by the script. Some look okay, some do not, that is a subject for improvement. I will try different values for the g'mic script and I am also going to add more faces for better diversity.I am also going to do some men faces for my game.
Yeah, and that't about it! Leave a comment if you have any questions, hate or praise! Thank you for reading
- Eugenik.
Sources:
[1]
TIGSource, dukope - creator of Papers, Please
Example of his work: