Using Python mode in Processing

Hi @all! :wave:

Here is Luiz.I'maback-endengineeratSQUEEZE. This is the 9th post for the Python Advent Calendar. I hope you guys don't mind a bit of English. :sweat_smile:

I have been working with Python only for 5 months, so there isn't much I can bring yet.

Last year I wrote a post about Creative Code with Processing.Ifyoudon'tknowProcessingorwhatiscreativecoding, I recommend you to check my previous post before proceeding on this one.

Processing is a language based on Java, but with a simple add-on, it's possible to write sketches in Python. Today, let's see how it works.

How to start

  1. Download Processing if you still don't have it.
  2. Install the Python mode, as described here.

Example: Cellular Automation

Cellular automationisadiscretemathematicalmodel,oftenusedinGenerativeArt.

Just as a demonstration, I adapted the example from this book, from the original Processing syntax to Python.

This is the result:

def setup():
    size(300, 300)
    colorMode(RGB)

    global generation
    generation = 0
    
    global memory
    memory = [[0 for x in range(width)] for y in range(height)] 
        
    for x in range(0, width):
        for y in range(0, height):
            if random(1) > 0.5:
                set(x, y, color(0, 0, 0))
            else:
                set(x, y, color(255, 255, 255))

def draw():
    global generation

    for x in range(1, width):
        for y in range(1, height):
            
            k = 0
            for i in range(-1, 2):
                for j in range(-1, 2):
                    if i == 0 and j == 0:
                        continue

                    c = get(x + i, y + j)
            
                    if red(c) == 0:
                        k += 1
            
            if k == 3:
                memory[x][y] = 0

            elif k >= 5:
                memory[x][y] = 255

    for x in range(0, width):
        for y in range(0, height):
            set(x, y, color(memory[x][y]))
    
    generation += 1
    print generation         

You are probably thinking that it doesn't really look like "real Python code" (what is that global?). Indeed, some workarounds are necessary to make it work. However, in general, it's the same Python you use every day. For more complex projects, it's possible to create classes, Python libraries and everything else.

This is how a basic cellular automata looks like:

celluar_automata

Change the values in the code and observe how they affect the result! Also, instead of only black and white, try to add some colors! :)

Check the Processing Language Reference for more information about functions you can use.

Conclusion

If you like Python and would like to play with creative coding or generative art, the Python Mode for Processing is definitely your best choice.

By the way, SQUEEZE IS HIRING!. Join us and make part of a international team in a fast growing Japanese startup! :muscle_tone3:

Recommended Posts

Using Python mode in Processing
Multithreaded processing in python
Text processing in Python
Queue processing in Python
Asynchronous processing (threading) in python
Image Processing Collection in Python
GUI programming in Python using Appjar
Signal processing in Python (1): Fourier transform
Precautions when using pit in Python
100 Language Processing Knock Chapter 1 in Python
Try using LevelDB in Python (plyvel)
Using global variables in python functions
Let's see using input in python
Infinite product in Python (using functools)
Edit videos in Python using MoviePy
Handwriting recognition using KNN in Python
Try using Leap Motion in Python
Depth-first search using stack in Python
When using regular expressions in Python
GUI creation in python using tkinter 2
Asynchronous processing using LINE BOT: RQ (Redis Queue) in Python
Quadtree in Python --2
CURL in python
Mouse operation using Windows API in Python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Try using the Wunderlist API in Python
Periodic execution processing when using tkinter [Python3]
python image processing
[Python] Speeding up processing using cache tools
Get Suica balance in Python (using libpafe)
(Bad) practice of using this in Python
[Python] Matrix multiplication processing time using NumPy
Slowly hash passwords using bcrypt in Python
Meta-analysis in Python
Try using the Kraken API in Python
Using venv in Windows + Docker environment [Python]
Unittest in python
Asynchronous processing using Linebot in Job queue
[FX] Hit oanda-API in Python using Docker
Tweet using the Twitter API in Python
Discord in Python
[Python] [Windows] Serial communication in Python using DLL
Start using Python
I tried using Bayesian Optimization in Python
DCI in Python
Python file processing
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Get Youtube data in Python using Youtube Data API
Plink in Python
Constant in python
[Python] Various data processing using Numpy arrays
Scraping a website using JavaScript in Python
Develop slack bot in python using chat.postMessage
Lifegame in Python.
Sqlite in python