pycontextfree


Pycontextfree is a library for creating images with Python by defining a set of generative rules. This approach is inspired by CFDG tool, yet unlike CFDG where special language is used, with pycontextfree rules are written in pure Python. This gives you extra flexibility by allowing to combine probabilistic rules with pythonic control flow and, additionally, by exposing cairo-cffi backend. In essence pycontextfree provides a set of drawing primitives, nested scopes of transformed geometry and photometry, and a mechanism to use Python functions as probabilistic rules. So that you can write something like this (on the left) and get something like that (on the right):

cf.py (Source)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from contextfree.contextfree import *
@check_limits
def branch():
    line(0, 1)
    with translate(0, 0.9):
        with scale(0.7 + rnd(0.3)):
            with rotate(-0.4 + rnd(0.5)):
                branch()
            with rotate(0.4 + rnd(0.5)):
                branch()
init(canvas_size=(300, 300))
branch()
display_ipython()
You can install pycontextfree by typing pip3 install pycontextfree. For mode details, take a look at the tutorial , API refence and examples in the gallery.

If you would like to support the project, feel free to buy me a coffe Buy me a coffee