Simple Python script that generates cellular automata posters as PDF files.
Simple Python script that generates cellular automata posters as PDF files.
Heavily inspired by this Reddit post – all credit goes to /u/collatz_conjecture, while any criticism is to be directed at me.
Check out @[email protected], a Mastodon bot based on this generator.
You can find the associated PDF files in the examples/
subdirectory.
The script has been tested with Python 3 and uses the cairocffi
package to draw vector shapes to PDF. That package, in turn, depends on CFFI, Cairo and other system-dependent libraries. You'll need to figure out how to install Cairo on your system yourself, but chances are that it's available through your package manager – if you're on macOS, know that Homebrew has it. In fact, it might already be installed and you don't have to do anything! Take a look at the cairocffi
documentation if you run into trouble.
Further, the wolframalpha
package is used to display "fun facts" about the rules – the script contains a comment explaining how to get the Wolfram|Alpha AppID required to enable this feature. Things will still work without it, though, you'll just miss out on some of the fun.
Assuming things have gone smoothly so far, let's proceed – I suggest using the venv
module (which is conveniently included in your Python installation) to avoid dependency hell. Run the following commands to get this script installed on your system:
$ git clone https://github.com/doersino/cellular-automata-posters
$ python3 -m venv cellular-automata-posters
$ cd cellular-automata-posters
$ source bin/activate
$ pip3 install -r requirements.txt
(To deactivate the virtual environment, run deactivate
.)
Now adjust the settings in cap.py
to taste (they're explained inline, I encourage you to play around with them). Then run the script using
$ python3 cap.py
after which you'll find the generated PDF file in your working directory.
CAP_RULE=57 CAP_WIDTH=400 CAP_ANGLE=5.5 CAP_COLORSCHEME="('#478c49', '#d0c043')" CAP_GRIDMODE='None' CAP_FONT='PragmataPro' CAP_PAGEWIDTH=2000 CAP_DEBUG='True' CAP_FILENAME='optionOverrides.pdf' python3 cap.py
will use the values from the environment variables with all other options set to their defaults. The end result – if you can get a hold of the PragmataPro font – will look like this.export CAP_FONT='Times'
before running the script multiple times (potentially for multiple rules). That way, the value (here: font) is set until you overwrite it or terminate the terminal session.utils/
subdirectory contains a few useful scripts, e.g. for generating the example image above. Also available is a LaTeX file that combines multiple PDFs into one, only showing a slice of each – this might come in handy for color proof printing.pdf2svg
utility if you for some reason prefer the results in SVG. File sizes can get quite large, however: around 10-40 MB for each of the examples shown above.context.text_extents
function) for vertical positioning – this would make sure that the vertical margins are correct for any font.You're very welcome to send a pull request implementing one or more of the above (or file an issue if you have any other ideas for improvement)!