Simple Random Art Generation using Python Programming

Hey fellow Python coder! In this tutorial, we will be covering how you can generate Random Art images using various methods. I hope you have fun learning through this tutorial.  Let’s start with a simple art generation in the upcoming sections.

Simple Art Generation

You can simply generate a random art using a random set of pixel values using the random.rand function. And then transfer the set of pixels to an image. This will result in a block image with random pixel values.

import numpy as np
import matplotlib.pyplot as plt

artPattern = np.random.rand(50, 50)
plt.imshow(artPattern)
plt.title("Simple Art Pattern")
plt.axis('off')
plt.show()

The resulting image generated is as follows.

Simple Art Generation in Python

What if we wish to generate a different art where we combine two different sets of pixel values using any mathematical approach which will result in a new set of pixel values? We will then pass the new pixel values to an image to generate an art.

artPattern_1 = np.random.rand(50, 50)
artPattern_2 = np.random.rand(50, 50)
finalArt = (artPattern_1 * artPattern_2) / 2

plt.imshow(finalArt)
plt.title("Combination of Two Separate Art Patterns")
plt.axis('off')
plt.show()

The new resulting artistic block image is as follows.

Simple Art Generation in Python

Random Bubble Art

Now the art images generated above are quite simple. But what if we wish to introduce bubbles on our canvas? For that, one of the approaches can be drawing bubbles on the plot using the code snippet shown below.

x = np.random.rand(300)
y = np.random.rand(300)
bubbleSizes = np.random.randint(10, 300, 300)
plt.scatter(x, y, s=bubbleSizes, c=np.random.rand(300), alpha=0.6)
plt.title("Bubble Art")
plt.axis('off')
plt.show()

In the code, we will first generate a random set of x and y coordinates using the same function i.e. random.rand function. We will also generate a random set of bubble sizes using the random.randint function. We will draw the bubbles on the canvas using the scatter plot where to add coloring to the bubbles we will again generate random colors to the c parameter using the random.rand function. We have added the alpha value to add a bit of transparency to the plot so that the overlapping of the bubbles is quite visible in a pretty way.

The resulting art generated is as follows.

Bubble Art

Art Generation using the PIL Library

To generate artistic images we will make use of two sub-modules of the PIL library namely, Image and ImageDraw modules.  After the necessary imports, we will set the width and height of the canvas and then create a white canvas using the Image.new function and pass the necessary parameters. Along with this, we will also create a drawing pen using the ImageDraw.Draw function.

Now in this approach, we will be drawing random lines on the canvas using 8 different random colors. Then we will be drawing num_lines different lines using the loops and random coordinate system using random.randint function that must lie between width and height. And later drawing the lines using the line function. Finally, we will save the canvas and also display the canvas on the screen.

from PIL import Image, ImageDraw
import random

width, height = 800, 600
canvas = Image.new('RGB', (width, height), color='white')
drawingPen = ImageDraw.Draw(canvas)
colors = ['red', 'green', 'blue', 'yellow', 'orange', 'purple', 'cyan', 'magenta']
num_lines = 50
for i in range(num_lines):
    x1, y1 = random.randint(0, width), random.randint(0, height)
    x2, y2 = random.randint(0, width), random.randint(0, height)
    color = random.choice(colors)
    drawingPen.line([(x1, y1), (x2, y2)], fill=color, width=3)
canvas.save('simple_art.png')
canvas.show()

The resulting artistic image is as follows.

Art Generation using the PIL Library

Hope you had fun visualizing the random arts on your screen and learned something new through this learning. If you liked this tutorial, you must also check and have a read at the following tutorials as well.

Also Read:

  1. Waffle Chart using pyWaffle in Python
  2. Create a Pie chart using Plotly in Python
  3. Venn Diagram in Python Programming

Happy Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *