Easily create cool random images in python with randimage

submited by
Style Pass
2021-08-16 17:30:19

I am plan­ning to add pro­file pic­tures to my social-app for musi­cians Riff and Match, and I would like to offer users a ran­dom­ly gen­er­at­ed abstract pic­ture. I had a look around for exist­ing solu­tions in python and could­n’t find any­thing to my lik­ing, so I came up with a sim­ple pro­ce­dure that looks good enough to me and is mod­u­lar enough to exper­i­ment in the future with new variations. 

You can find the code and instruc­tions on github - this post is meant as a more detailed expla­na­tion of how the pro­ce­dure works. 

The idea is to lever­age the many beau­ti­ful col­ormaps avail­able in mat­plotlib to avoid rein­vent­ing the wheel — these offer a con­ve­nient way to con­tin­u­ous­ly map any num­ber between 0 and 1 to a col­or belong­ing to a spe­cif­ic curve in col­or-space. It is then enough to vec­tor­ize the image along some ran­dom path (step 1) and map point k in the path to k/N (where N is the length of the path), col­or­ing the cor­re­spond­ing pix­el with the val­ue of the cho­sen col­ormap for k/N.

I had sev­er­al ideas in mind on how to find an inter­est­ing path (some of which I men­tion in the last sec­tion of this post), but to my delight the first try — the EPWT [1] applied to ran­dom­ly gen­er­at­ed mask — gave quite inter­est­ing results so I decid­ed to pub­lish it as-is and maybe down the line play with oth­er variants. 

Leave a Comment