Here is what I have now.
The imported libraries:
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
import cv2
import time
For instance(this part of code is from internet example), I draw a plot with a "text" on it.
fig = plt.figure(figsize=(6,6.5))
canvas = FigureCanvas(fig)
ax = fig.gca()
ax.text(0.0,0.0,"Test", fontsize=45)
ax.axis('off')
Then, I save it to, well, "1.png".
plt.savefig("1.png")
Finally, I create a video writer and write the png into the video.
size = (600,650)
fps = 2
fourcc = cv2.VideoWriter_fourcc('I', '4', '2', '0')
video = cv2.VideoWriter("1.avi", fourcc, fps, size)
image = cv2.imread('1.png')
video.write(image)
video.release()
Here is the question: I need to save the picture and then read it as the numpy array that opencv can read, is there any way that I can directly transform the fig object to the image object(numpy array)?
Thank you guys very much!
question from:https://stackoverflow.com/questions/66050702/how-to-generate-a-video-with-python-matplotlib-and-opencv