I have an image from the PIL library in python. I wanted to add some basic animation by plotting the image one pixel at a time. To learn the basics of plotting, I am going through the image row by row and plotting image.getpixel((0, 0)), image.getpixel((0, 1)), (0, 2) and so on for each row. But my end goal is to plot the image starting from the center, then moving to the 8 neighboring cells to that, and then their neighboring cells etc.
This is the code I have currently:
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
# Create a figure and axis
fig, ax = plt.subplots()
# Plot each pixel
width, height = image.size
for x in range(width):
for y in range(height):
# Get the pixel color and convert it to RGBA format
pixel = image.getpixel((x, y))
color = (pixel[0] / 255, pixel[1] / 255, pixel[2] / 255, 1.0)
# Create a rectangle patch for the pixel
rect = Rectangle((x, y), 1, 1, color=color)
ax.add_patch(rect)
# Set the limits of the plot
ax.set_xlim([0, width])
ax.set_ylim([height, 0])
# Remove the axis ticks and labels
ax.set_xticks([])
ax.set_yticks([])
plt.show()
But, this code plots the image at the very end. Please suggest me some way to plot the image inside the loop and update the plot rather than creating a new one.
Also, I tried looking for libraries that do this, but I couldn't find one. My secondary request is suggestions for libraries.