def test_builtin_reader_plugin(viewer_factory): """Test the builtin reader plugin reads a temporary file.""" with NamedTemporaryFile(suffix='.tif', delete=False) as tmp: data = np.random.rand(20, 20) io.imsave(tmp.name, data) tmp.seek(0) layer_data = read_data_with_plugins(tmp.name) assert isinstance(layer_data, list) assert len(layer_data) == 1 assert isinstance(layer_data[0], tuple) assert np.allclose(data, layer_data[0][0]) view, viewer = viewer_factory() viewer.open(tmp.name, plugin='builtins') assert np.allclose(viewer.layers[0].data, data)
def test_builtin_reader_plugin_stacks(viewer_factory): """Test the builtin reader plugin reads multiple files as a stack.""" data = np.random.rand(5, 20, 20) tmps = [] for plane in data: tmp = NamedTemporaryFile(suffix='.tif', delete=False) io.imsave(tmp.name, plane) tmp.seek(0) tmps.append(tmp) _, viewer = viewer_factory() # open should take both strings and Path object, so we make one of the # pathnames a Path object names = [tmp.name for tmp in tmps] names[0] = Path(names[0]) viewer.open(names, stack=True, plugin='builtins') assert np.allclose(viewer.layers[0].data, data) for tmp in tmps: tmp.close() os.unlink(tmp.name)
def test_builtin_reader_plugin(viewer_factory): """Test the builtin reader plugin reads a temporary file.""" from napari.plugins import plugin_manager plugin_manager.hooks.napari_get_reader.bring_to_front(['builtins']) with NamedTemporaryFile(suffix='.tif', delete=False) as tmp: data = np.random.rand(20, 20) io.imsave(tmp.name, data) tmp.seek(0) layer_data = read_data_with_plugins(tmp.name) assert isinstance(layer_data, list) assert len(layer_data) == 1 assert isinstance(layer_data[0], tuple) assert np.allclose(data, layer_data[0][0]) view, viewer = viewer_factory() viewer.open_path(tmp.name) assert np.allclose(viewer.layers[0].data, data)
def animate( self, path, fps=20, quality=5, format=None, canvas_only=True, scale_factor=None, ): """Create a movie based on key-frames Parameters ------- path : str path to use for saving the movie (can also be a path). Extension should be one of .gif, .mp4, .mov, .avi, .mpg, .mpeg, .mkv, .wmv If no extension is provided, images are saved as a folder of PNGs interpolation_steps : int Number of steps for interpolation. fps : int frames per second quality: float number from 1 (lowest quality) to 9 only applies to non-gif extensions format: str The format to use to write the file. By default imageio selects the appropriate for you based on the filename. canvas_only : bool If True include just includes the canvas, otherwise include the full napari viewer. scale_factor : float Rescaling factor for the image size. Only used without viewer (with_viewer = False). """ self._validate_animation() # create a frame generator frame_gen = self._frame_generator(canvas_only=canvas_only) # create path object path_obj = Path(path) folder_path = path_obj.absolute().parent.joinpath(path_obj.stem) # if path has no extension, save as fold of PNG save_as_folder = False if path_obj.suffix == "": save_as_folder = True # try to create an ffmpeg writer. If not installed default to folder creation if not save_as_folder: try: # create imageio writer. Handle separately imageio-ffmpeg extensions and # gif extension which doesn't accept the quality parameter. if path_obj.suffix in [ ".mov", ".avi", ".mpg", ".mpeg", ".mp4", ".mkv", ".wmv", ]: writer = imageio.get_writer( path, fps=fps, quality=quality, format=format, ) else: writer = imageio.get_writer(path, fps=fps, format=format) except ValueError as err: print(err) print("Your file will be saved as a series of PNG files") save_as_folder = True if save_as_folder: # if movie is saved as series of PNG, create a folder if folder_path.is_dir(): for f in folder_path.glob("*.png"): os.remove(f) else: folder_path.mkdir(exist_ok=True) # save frames for ind, frame in enumerate(frame_gen): if scale_factor is not None: frame = ndi.zoom(frame, (scale_factor, scale_factor, 1)) frame = frame.astype(np.uint8) if not save_as_folder: writer.append_data(frame) else: fname = folder_path / (path_obj.stem + "_" + str(ind) + ".png") imsave(fname, frame) if not save_as_folder: writer.close()