Skip to content
2 changes: 1 addition & 1 deletion docs/updating.md
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,7 @@ pages for more details on the input and output variable types.

* pre v4.0: NA
* post v4.0: frame_size = **pcv.visualize.time_lapse_video**(*img_list, out_filename='./time_lapse_video.mp4', fps=29.97, display=True*)
* post v5.0: frame_size = **pcv.visualize.time_lapse_video**(*source, out_filename='./time_lapse_video.mp4', fps=29.97*)
* post v5.0: **pcv.visualize.time_lapse_video**(*source, out_filename='./time_lapse_video.mp4', fps=29.97*)

#### plantcv.watershed_segmentation

Expand Down
8 changes: 4 additions & 4 deletions docs/visualize_time_lapse_video.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ This function generates and saves the time-lapse video based on a list of paths

**plantcv.visualize.time_lapse_video**(*source, out_filename='./time_lapse_video.mp4', fps=29.97*)

**returns** frame_size
**returns** None

- **Parameters:**
- source - List of paths to the images to create the video or file path to a directory of images to use.
- source - File path to a directory of images to use, list of paths to the images, or list of `numpy.ndarray` objects to create the video
- out_filename - Name of file to save the generated video to.
- fps - Frame rate (frames per second) By default fps=29.97. Commonly used values: 23.98, 24, 25, 29.97, 30, 50, 59.94, 60

Expand All @@ -23,8 +23,8 @@ from plantcv import plantcv as pcv
# Note you will have to change this part on your own path
img_directory = './path_to_images_directory/'

frame_size = pcv.visualize.time_lapse_video(source=img_directory,
out_filename='./eg_time_lapse.mp4')
pcv.visualize.time_lapse_video(source=img_directory,
out_filename='./eg_time_lapse.mp4')
```

**Video generated**
Expand Down
27 changes: 16 additions & 11 deletions plantcv/plantcv/visualize/time_lapse_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def time_lapse_video(source, out_filename='./time_lapse_video.mp4', fps=29.97):
Parameters:
-----------
source = string or list,
Path to a folder of images to use or list of paths to the images to create the video
Path to a folder of images to use, list of paths to the images,
or list of numpy.ndarray objects to create the video
out_filename = string,
name of file to save the generated video to
fps = float,
Expand All @@ -30,15 +31,21 @@ def time_lapse_video(source, out_filename='./time_lapse_video.mp4', fps=29.97):
img_list = source
if isinstance(source, str):
img_list = read_dataset(source, sort=True)
# for file paths read the images
if isinstance(img_list[0], str):
imgs = []
list_r = []
list_c = []
for file in img_list:
img = iio.imread(file)
list_r.append(img.shape[0])
list_c.append(img.shape[1])
imgs.append(img)
else:
list_c = [img.shape[0] for img in img_list]
list_r = [img.shape[1] for img in img_list]
imgs = img_list

imgs = []
list_r = []
list_c = []
for file in img_list:
img = iio.imread(file)
list_r.append(img.shape[0])
list_c.append(img.shape[1])
imgs.append(img)
max_c, max_r = np.max(list_c), np.max(list_r)

# use the largest size of the images as the frame size
Expand All @@ -54,5 +61,3 @@ def time_lapse_video(source, out_filename='./time_lapse_video.mp4', fps=29.97):

frames = [resize(img, frame_size, interpolation=None) for img in imgs]
iio.imwrite(out_filename, frames, plugin="FFMPEG", fps=fps, codec="libx264")

return frame_size
22 changes: 18 additions & 4 deletions tests/plantcv/visualize/test_time_lapse_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from plantcv.plantcv.visualize.time_lapse_video import time_lapse_video


def test_plantcv_visualize_time_lapse_video_list_input(tmpdir):
def test_plantcv_visualize_time_lapse_video_path_list_input(tmpdir):
"""Test for PlantCV."""
# Generate 3 test images and saved in tmpdir
list_im = []
Expand All @@ -17,7 +17,21 @@ def test_plantcv_visualize_time_lapse_video_list_input(tmpdir):
list_im.append(img_i_path)

vid_name = os.path.join(tmpdir, 'test_time_lapse_video.mp4')
_ = time_lapse_video(source=list_im, out_filename=vid_name, fps=29.97)
time_lapse_video(source=list_im, out_filename=vid_name, fps=29.97)
assert os.path.exists(vid_name) and os.path.getsize(vid_name) > 100


def test_plantcv_visualize_time_lapse_video_array_list_input(tmpdir):
"""Test for PlantCV."""
# Generate 3 test images and saved in tmpdir
list_im = []
for _ in range(3):
temp_img = np.random.rand(3, 3)
min_, max_ = np.nanmin(temp_img), np.nanmax(temp_img)
temp_img = np.interp(temp_img, (min_, max_), (0, 255)).astype('uint8')
list_im.append(temp_img)
vid_name = os.path.join(tmpdir, 'test_time_lapse_video.mp4')
time_lapse_video(source=list_im, out_filename=vid_name, fps=29.97)
assert os.path.exists(vid_name) and os.path.getsize(vid_name) > 100


Expand All @@ -32,7 +46,7 @@ def test_plantcv_visualize_time_lapse_video_str_input(tmpdir):
cv2.imwrite(img_i_path, temp_img)

vid_name = os.path.join(tmpdir, 'test_time_lapse_video.mp4')
_ = time_lapse_video(source=str(tmpdir), out_filename=vid_name, fps=29.97)
time_lapse_video(source=str(tmpdir), out_filename=vid_name, fps=29.97)
assert os.path.exists(vid_name) and os.path.getsize(vid_name) > 100


Expand All @@ -50,7 +64,7 @@ def test_plantcv_visualize_time_lapse_video_different_img_sizes_warns(tmpdir, ca
list_im.append(img_i_path)

vid_name = os.path.join(tmpdir, 'test_time_lapse_video.mp4')
_ = time_lapse_video(source=list_im, out_filename=vid_name, fps=29.97)
time_lapse_video(source=list_im, out_filename=vid_name, fps=29.97)
_, err = capsys.readouterr()

assert "Warning" in err and os.path.exists(vid_name)