Reputation: 489
I have an algorithm, which after each iteration creates a matrix. After some operations on matrix it is plotted to the user. if I run program 6 times I will get:
My goal is to changing image dynamically, like a movie.
I have no idea from which side to start. I found some ways of creating video from images in python and then wrap it in the video player. But it seems to be a bit complicated and it is impossible to see the changes while algorithm is working. Are there any suggestions how to do it?
Upvotes: 3
Views: 3669
Reputation: 1531
You can use FFMPEG
ffmpeg -r 1 -i img%01d.png -vcodec mpeg4 -y movie.mp4
-r 1
is the number of frames/images per second. Increase it to make the video faster.
In Python:
def convert():
os.system("ffmpeg -r 1 -i img%01d.png -vcodec mpeg4 -y movie.mp4")
Alternatively, you can use ImageIO to generate a GIF. You can set the parameters for duration of each frame.
import imageio
with imageio.get_writer('/path_to_video.gif', mode='I') as writer:
for filename in filenames:
image = imageio.imread(filename)
writer.append_data(image)
Read the manual on website for more detailed instructions.
Or with cv2,
import cv2
img1 = cv2.imread('1.jpg')
img2 = cv2.imread('2.jpg')
img3 = cv2.imread('3.jpg')
height , width , layers = img1.shape
video = cv2.VideoWriter('video.avi',-1,1,(width,height))
video.write(img1)
video.write(img2)
video.write(img3)
cv2.destroyAllWindows()
video.release()
Upvotes: 7
Reputation: 163
Video Data Generation code from scratch in Python (use Jupyter):
import numpy as np
import skvideo.io as sk
# creating sample video data
num_vids = 5
num_imgs = 100
img_size = 50
min_object_size = 1
max_object_size = 5
for i_vid in range(num_vids):
imgs = np.zeros((num_imgs, img_size, img_size)) # set background to 0
vid_name = ‘vid’ + str(i_vid) + ‘.mp4’
w, h = np.random.randint(min_object_size, max_object_size, size=2)
x = np.random.randint(0, img_size — w)
y = np.random.randint(0, img_size — h)
i_img = 0
while x>0:
imgs[i_img, y:y+h, x:x+w] = 255 # set rectangle as foreground
x = x-1
i_img = i_img+1
sk.vwrite(vid_name, imgs.astype(np.uint8))
# play a video
from IPython.display import Video
Video(“vid3.mp4”) # the script & video should be in same folder
Upvotes: 1