Creating Art with Numpy: A Beginner’s Guide to Image Manipulation with Arrays

Abhinav Shreyash
3 min readAug 3, 2023

In this article, We will discuss Creating an art or image by yourself using arrays and Numpy.

import numpy as np
import matplotlib.pyplot as plt

# Create a canvas (image) with dimensions 200x200 pixels
canvas = np.zeros((200, 200, 3), dtype=np.uint8)

# Set the color of the canvas (black background)
canvas[:, :, :] = [0, 0, 0]

# Create some colorful shapes using NumPy arrays
# Example: Drawing a red circle
center_x, center_y = 100, 100
radius = 50
canvas[center_y-radius:center_y+radius, center_x-radius:center_x+radius, 0] = 255
canvas[center_y-radius:center_y+radius, center_x-radius:center_x+radius, 1] = 0
canvas[center_y-radius:center_y+radius, center_x-radius:center_x+radius, 2] = 0

# Example: Drawing a green rectangle
canvas[50:150, 20:80, 0] = 0
canvas[50:150, 20:80, 1] = 255
canvas[50:150, 20:80, 2] = 0

# Example: Drawing a blue triangle
triangle_coords = np.array([(100, 10), (80, 60), (120, 60)])
triangle_mask = plt.matplotlib.path.Path(triangle_coords).contains_points(np.indices(canvas.shape[:2]).reshape(2, -1).T).reshape(canvas.shape[:2])
canvas[triangle_mask, 0] = 0
canvas[triangle_mask, 1] = 0
canvas[triangle_mask, 2] = 255

# Display the art image
plt.imshow(canvas)
plt.axis(‘off’)
plt.show()

Here’s a line-by-line explanation of the code:

  1. import numpy as np: This line imports the NumPy library, which is used for working with arrays, and renames it to np for convenience.
  2. import matplotlib.pyplot as plt: This line imports the pyplot module from the matplotlib library, which is used for creating plots and visualizations, and renames it to plt for convenience.
  3. canvas = np.zeros((200, 200, 3), dtype=np.uint8): This line creates a 200x200 pixel canvas (image) with 3 color channels (RGB) and sets all pixel values to 0 (black).
  4. canvas[:, :, :] = [0, 0, 0]: This line sets the color of the entire canvas to black (all pixel values are set to 0).
  5. center_x, center_y = 100, 100: These lines define the center coordinates of a circle that will be drawn on the canvas.
  6. radius = 50: This line defines the radius of the circle that will be drawn on the canvas.
  7. canvas[center_y-radius:center_y+radius, center_x-radius:center_x+radius, 0] = 255: This line sets the red color channel of all pixels within the circle to 255 (maximum value), creating a red circle on the canvas.
  8. canvas[center_y-radius:center_y+radius, center_x-radius:center_x+radius, 1] = 0: This line sets the green color channel of all pixels within the circle to 0 (minimum value).
  9. canvas[center_y-radius:center_y+radius, center_x-radius:center_x+radius, 2] = 0: This line sets the blue color channel of all pixels within the circle to 0 (minimum value).
  10. canvas[50:150, 20:80, 0] = 0: These lines draw a green rectangle on the canvas by setting the red and blue color channels of all pixels within the rectangle to 0 and setting the green color channel to 255 (maximum value).
  11. canvas[50:150, 20:80, 1] = 255:
  12. canvas[50:150, 20:80, 2] = 0:
  13. triangle_coords = np.array([(100, 10), (80, 60), (120, 60)]): These lines draw a blue triangle on the canvas by defining the coordinates of its vertices and creating a mask that selects all pixels within the triangle.
  14. triangle_mask = plt.matplotlib.path.Path(triangle_coords).contains_points(np.indices(canvas.shape[:2]).reshape(2, -1).T).reshape(canvas.shape[:2]):
  15. canvas[triangle_mask, 0] = 0: These lines set the red and green color channels of all pixels within the triangle to 0 and set the blue color channel to 255 (maximum value), creating a blue triangle on the canvas.
  16. canvas[triangle_mask, 1] = 0:
  17. canvas[triangle_mask, 2] = 255:
  18. plt.imshow(canvas): This line displays the final image (art) created on the canvas using Matplotlib’s imshow function.
  19. plt.axis(‘off’): This line turns off axis lines and labels in the plot.
  20. plt.show(): This line displays the plot.

Hope My explanation was satisfying to you .

Thank You for giving your precious time for stopping by and reading this blog

# Example: Drawing a blue triangle
triangle_coords = np.array([(100, 10), (80, 60), (120, 60)])
triangle_mask = plt.matplotlib.path.Path(triangle_coords).contains_points(np.indices(canvas.shape[:2]).reshape(2, -1).T).reshape(canvas.shape[:2])
canvas[triangle_mask, 0] = 0
canvas[triangle_mask, 1] = 0
canvas[triangle_mask, 2] = 255

# Display the art image
plt.imshow(canvas)
plt.axis(‘off’)
plt.show()

--

--