Creating Art with Numpy: A Beginner’s Guide to Image Manipulation with Arrays
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:
import numpy as np
: This line imports the NumPy library, which is used for working with arrays, and renames it tonp
for convenience.import matplotlib.pyplot as plt
: This line imports thepyplot
module from thematplotlib
library, which is used for creating plots and visualizations, and renames it toplt
for convenience.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).canvas[:, :, :] = [0, 0, 0]
: This line sets the color of the entire canvas to black (all pixel values are set to 0).center_x, center_y = 100, 100
: These lines define the center coordinates of a circle that will be drawn on the canvas.radius = 50
: This line defines the radius of the circle that will be drawn on the canvas.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.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).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).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).canvas[50:150, 20:80, 1] = 255
:canvas[50:150, 20:80, 2] = 0
: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.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
: 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.canvas[triangle_mask, 1] = 0
:canvas[triangle_mask, 2] = 255
:plt.imshow(canvas)
: This line displays the final image (art) created on the canvas using Matplotlib’simshow
function.plt.axis(‘off’)
: This line turns off axis lines and labels in the plot.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()