davidsvy / color-stealing

Adding color to binary fractals by stealing it

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Color Stealing

Python implementation of the Color Stealing algorithm. In short, this technique constructs aesthetically pleasing fractals by copying color patterns from a given auxiliary image to a binary fractal.

Definitely not a cherry-picked sample.

Table of contents

Summary

Perhaps the most simple method to construct a fractal image is the Chaos Game. A fractal can be defined by its iterated function system (IFS), which is a set of n functions. For images, each function is with a corresponding probability . The Chaos Game begins by sampling a random 2d coordinate . At the j-th iteration, an integer is sampled based on the distribution p. The next coordinate is computed as . This coordinate is then plotted on a 2d grid, which was initially empty. After a number of iterations, the result is a binary image. Examples of such images can be found in the second column of the generated samples as well as at templates/images.

The Color Stealing algorithm extends the Chaos Game by adding color to the constructed fractal. This requires an auxiliary image as input. Specifically, two IFSs are kept: one with functions and probabilities for the fractal image and one with functions and no probabilities for the auxiliary image. Similarly, two coordinates are initially sampled: . At the j-th iteration, an integer is likewise sampled from the distribution p, which is followed by calculations and . Then the fractal is extended: . This process eventually results in images such as the third column of generated samples.

Generated samples

Below is a list of examples constructed by the provided scripts. Each sample was generated using the command python run.py -s -i 100000 -nt -l <ifs_path> -c <img_path>, where <ifs_path> was chosen from templates/ifs and <img_path> from assets/auxiliary. Running this commands produces a different result each time, due to rng. Obviously, all the examples are cherry-picked.

Auxiliary Image Binary Fractal Color Stealing

Installation

git clone https://github.com/davidsvy/color-stealing
cd color-stealing
pip install -r requirements.txt

Usage

python run.py OPTIONS

Saving and rendering:

-s, --save [FILE_NAME]           If provided, saves the image and the IFS.
                                 If FILE_NAME is not given, the image will 
                                 be saved at DIR/images/TIMESTEP.png and
                                 the IFS at DIR/ifs/TIMESTEP.csv. DIR is
                                 the next argument and TIMESTEP is the
                                 timestep at the time of saving the files. If
                                 FILE_NAME is given, the image will be saved at 
                                 DIR/images/FILE_NAME.png an the IFS at 
                                 DIR/ifs/FILE_NAME.csv. For the script to run, 
                                 either -p or -s must be provided.

-d, --dir DIR                    Directory where the image & the IFS file will
                                 be saved.

-p, --plot                       If given, the constructed fractal will be
                                 rendered using plt.imshow(). For the script
                                 to run, either -p or -s must be provided.

-cm, --cmap CMAP                 cmap for for plt.imshow(). Accepted values are
                                 'i', 'p', 'm', 'c', 'v'. Default value is 'i'.
                                 CMAP is ignored if either -f or -c is provided.

Fractal parameters:

-r, --resolution RES             Resolution of the square fractal image. Default
                                 value is 1024.

-b, --batch_size BS              Number of points calculated at each iteration.
                                 If -f is given, setting BS <= 4 might result in 
                                 better visuals. Default value is 128.

-i, --n_iter N_ITER              Number of iterations for the chaos game algorithm.
                                 Default value is 50000.

-ni, --n_ignore N_IGNORE         Number of initial iterations that will not be 
                                 rendered. Default value is 200.

-nt, --no_tqdm                   If given, the tqdm progress bar will not be used.

Loading saved IFS:

-l, --load PATH                  If not provided, the scripts will sample a random IFS.
                                 If provided, the fractal stored inside PATH will be 
                                 generated. PATH must be a path to a valid IFS csv 
                                 file.

-m, --mutate PATH                Same as the previous argument, with the difference
                                 that the loaded IFS will be slightly mutated in a 
                                 random fashion.

Color Stealing:

-c, --color_steal PATH           If not provided (and -f not provided), a binary 
                                 fractal will be generated. If provided, the fractal 
                                 will be constructed by copying color patterns from 
                                 the image stored at PATH using the Color Stealing
                                 algorithm.

Fractal Flame:

-f, --flame                      If not provided (and -c not provided), a binary 
                                 fractal will be generated. If provided, the image  
                                 will be generated using the Fractal Flame
                                 algorithm. Unfortunately, this mechanic is not 
                                 working well yet. Setting BS <= 4 might result in 
                                 better results.

-g, --gamma GAMMA                Gamma parameter for the fractal frame algorithm.
                                 Default value is 20.

-su, --sup SUP                   Supersampling ratio for the fractal flame algorithm. 
                                 Default value is 3.

Contact

The author of this repo can be mailed at dsvyezhentsev@gmail.com.

Citations

@inproceedings{Barnsley2003ERGODICT,
  title={ERGODIC THEORY , FRACTAL TOPS AND COLOUR},
  author={Michael F. Barnsley},
  year={2003}
}

About

Adding color to binary fractals by stealing it

License:MIT License


Languages

Language:Python 100.0%