5

I have a numpy matrix where each row is a picture. I can reshape the rows and display the images with matplotlib.pyplot. The problem is: I don't want to display the Images separately, I want to display them after each other like a video.

How is that possible in python?

jcdmb
  • 3,016
  • 5
  • 38
  • 53
  • 1
    what have you tried? maybe iterate through the matrix with a `time pause` and continuously change your display – Ryan Saxe Jul 09 '13 at 06:55
  • what i did when i wanted that was to save it in a fits file with pyfits, and than just show it with DS9, in there you can iterate through every frame separately etc- (in my case i hade a 3D array, and i just saved it as 3 different files, with axis swapped, to be able to "walk through it", by one axis per file, though if time.pause works, no need to make it more difficult than that. My solution does allow you to save them and do it again later, without needing to simulate whatever you wanted to see in a 3D matrix though – usethedeathstar Jul 09 '13 at 07:57

2 Answers2

5

Well, I don't know if it is the best way but I've used matplotlib.pyplot to solve my problem. Import it as "plt" and do the following:

matrix=numpy.genfromtxt(path,delimiter=',') # Read the numpy matrix with images in the rows
c=matrix[0]
c=c.reshape(120, 165) # this is the size of my pictures
im=plt.imshow(c)
for row in matrix:
    row=row.reshape(120, 165) # this is the size of my pictures
    im.set_data(row)
    plt.pause(0.02)
plt.show()
jcdmb
  • 3,016
  • 5
  • 38
  • 53
  • The solution is a bit hacky, but if you only want to debug sth then this is completely fine I think, however, I would be interested in a more sophisticated version where you can save the video to a file. –  Nov 24 '17 at 14:33
  • This doesnt work on jupyter notebooks unfortunately. – sachinruk Aug 06 '18 at 07:34
0

Hello my solutions using matplotlib animations:

from matplotlib import pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation 

video # my numpy matrix of shape (frames, channel, w, h)

# initializing a figure in 
# which the graph will be plotted
fig = plt.figure(figsize=(10,10)) 

# create an axis
ax = plt.axes() 

# initializing a line variable
img = ax.imshow(np.ndarray(shape=video.shape[2:])
   
def animate(i):
    img.set_data(i)
    return img,
   
anim = FuncAnimation(fig, animate, frames=video, interval=20, blit=True)
  
anim.save(filename, writer='ffmpeg', fps=5, bitrate=2000)

Note if you are in a jupyter notebook you can use a temporary file and a ipywidgets to display the video without saving it.

from tempfile import NamedTemporaryFile
f = NamedTemporaryFile(mode='w+', suffix='.mp4')

#...previous code

anim.save(f.name, writer='ffmpeg', fps=5, bitrate=2000)
from ipywidgets import Video
Video.from_file(f.name)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 18 '22 at 15:45