DEV Community

codesharedot
codesharedot

Posted on

Computer generated art with Python

A computer can be used to generate art. Earliest attempts can be seen in old screensavers or demos. The most modern form of art generation uses AI/Machine Learning/Deep Learning.

Create Art with Python

Even with basic Python knowledge you can computer generate art. For instance, we can make the computer generate random numbers and use that random data to form a picture of bubbles.

Computer Art

The Code

The modules numpy and matplotlib will be used

#!/usr/bin/python3
import numpy as np
import matplotlib.pyplot as plt
Enter fullscreen mode Exit fullscreen mode

Then a main function

if __name__ =="__main__":
Enter fullscreen mode Exit fullscreen mode

where random data is created (x,y). Random colors are also created.

    N = 100
    x = np.zeros(N)
    y = np.zeros(N)
    for i in range(N):
        x[i] = np.random.rand()
        y[i] = np.random.rand()
    colors = np.random.rand(N)
Enter fullscreen mode Exit fullscreen mode

Then we plot the picture

    area = (30 * np.random.rand(N))**2  
    ax = plt.subplot(111)
    ax.scatter(x, y, s=area, c=colors, alpha=0.6)
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.spines['left'].set_visible(False)
    plt.axis('off')
    plt.show()
Enter fullscreen mode Exit fullscreen mode

Related links:

Top comments (0)