pims
provides three classes for loading video.
ImageSequence
reads images from a directory.Video
reads standard video files (AVI, MOV, etc.).TiffStack
reads multi-frame TIF / TIFF files.Once loaded, these objects can be handled alike. In software terms, each is a subclass of a generic Frames
object.
The differences between the formats are handled quietly by pims
.
Take ImageSequence
as an example. We have a folder of images here:
ls image_sequence
T76S3F00001.png T76S3F00002.png T76S3F00003.png T76S3F00004.png T76S3F00005.png
We can load them into an ImageSequence
object.
import pims
v = pims.ImageSequence('image_sequence/*.png')
We can see basic properties.
v
<Frames> Source: /home/tdimiduk/code/pims/examples/image_sequence/*.png Length: 5 frames Frame Shape: 424 x 640 Pixel Datatype: float32
We can print the first frame (it's an array of brightness values) or view those values as an image.
print v[0]
[[ 0.47450981 0.47843137 0.47843137 ..., 0.47058824 0.46666667 0.47450981] [ 0.4627451 0.46666667 0.4627451 ..., 0.47058824 0.47450981 0.47843137] [ 0.47058824 0.47843137 0.4627451 ..., 0.47450981 0.48235294 0.49019608] ..., [ 0.49019608 0.49411765 0.49019608 ..., 0.47843137 0.47843137 0.47843137] [ 0.49411765 0.49803922 0.49411765 ..., 0.48627451 0.49019608 0.49019608] [ 0.49411765 0.49803922 0.49411765 ..., 0.49411765 0.49019608 0.48627451]]
%matplotlib inline
v[0]
We can select a subset of the frames for viewing or processing. Examples:
v[3]
frame three (an array)v[:10]
first 10 frames (a list of arrays)v[2:5]
frames 2-5 including 2 and 5 (a list of arrays)v[100:]
frames 100 to the end (a list of arrays)for frame in v[:]:
# Do something with frame, a numpy array.
ImageSequence
relies only on numpy
and scipy
, which are required dependencies of mr
, so it works
out of the box. Video
needs OpenCV, which includes the Python module cv2
.
TiffStack
needs tifffile
or Pillow
.
Once these dependencies are in place, Video
and TiffStack
work in the same way as ImageSequence
.
v = pims.Video('/home/dallan/mr/mr/tests/water/bulk-water.mov')
# This file is not included in PIMS, to keep the file size small.
# Try it with a video file of your own.
v
<Frames> Source File: /home/dallan/mr/mr/tests/water/bulk-water.mov Frame Dimensions: 640 x 424 Cursor at Frame 0 of 480
v = pims.TiffStack('tiff_stack.tif')
v
<Frames> Source: tiff_stack.tif Length: 5 frames Frame Shape: 512 x 512 Pixel Datatype: <type 'numpy.uint16'>