0

I have the following code from here to overlay a scatter plot on an image with each point plotted one by one. However, I need to do this for 6000 frames and it seems to be taking one minuter per frame to save the video. Can someone suggest a faster way to save the video? Or maybe point out what I am doing wrong that it is taking so long?

A method to scatter the points using opencv is also welcome.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random
from PIL import Image
import urllib.request

random.seed(20210702)
N = 50
x = random.sample(range(0, 380), N)
y = random.sample(range(0, 380), N)
size =  [20 for x in range(380)]
colors = []

cm = plt.get_cmap('jet', N)

fig,ax = plt.subplots(figsize=(9, 4.5))
graph = ax.scatter([], [], marker='+')# 

url = 'https://raw.githubusercontent.com/kornelski/pngquant/master/test/img/test.png'
im = Image.open(urllib.request.urlopen(url))
print(im.size)

def animate(i):
    ax.imshow(im)
    graph.set_offsets(np.vstack((x[:i+1], y[:i+1])).T)
    graph.set_sizes(size[:i+1])
    colors.append(cm(i))
    graph.set_facecolors(colors) 
    return graph

ani = FuncAnimation(fig, animate, frames=6000, repeat=False, interval=0.00001)
FFwriter = animation.FFMpegWriter(fps=1)
ani.save('animation.mp4', writer = FFwriter)
user42
  • 568
  • 4
  • 16

0 Answers0