From ba055f61579b471d4879b18b6fafbc1ce59fb968 Mon Sep 17 00:00:00 2001 From: Simon Humpohl <simon.humpohl@rwth-aachen.de> Date: Wed, 8 Mar 2023 11:44:07 +0100 Subject: [PATCH] Use tifffile to read the image --- pyproject.toml | 1 + qutil/image.py | 24 ++++++++---------------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0a2f1657..178a43f4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -61,6 +61,7 @@ qcodes = [ ] image = [ "moviepy", + "tifffile[all]" ] doc = [ "sphinx", diff --git a/qutil/image.py b/qutil/image.py index 2f9daeac..d3627159 100644 --- a/qutil/image.py +++ b/qutil/image.py @@ -3,12 +3,9 @@ import pathlib from typing import Optional, Union, Literal import numpy as np -from PIL import Image, ImageSequence +import tifffile from moviepy.editor import ImageSequenceClip -from qutil.misc import filter_warnings -from qutil.itertools import ignore_exceptions - try: from numpy.typing import NDArray except ImportError: @@ -17,15 +14,6 @@ except ImportError: _pathT = Union[str, os.PathLike] -def read_tiff(path: _pathT) -> NDArray: - """Read a (multipage) .tif image and return it as an array.""" - # Catch the following error for files generated by the Thorlabs Camera: - # OSError: Corrupt EXIF data. Expecting to read 2 bytes but only got 0. - with Image.open(path) as im, filter_warnings('error', UserWarning): - frames = list(ignore_exceptions(ImageSequence.Iterator(im), UserWarning)) - return np.array(frames) - - def convert_tiff(file: _pathT, fps: float, format: str = '.mp4', threads: Optional[int] = None, out: Optional[_pathT] = None, logger: Optional[Literal['bar']] = None): """Converts a multipage .tif file to `format` and writes it to disk. @@ -63,7 +51,11 @@ def convert_tiff(file: _pathT, fps: float, format: str = '.mp4', threads: Option threads=os.cpu_count()) """ - file = pathlib.Path(file).with_suffix('.tif') - clip = ImageSequenceClip(read_tiff(file), fps=fps) - clip.write_videofile((out or file).with_suffix(format), fps, audio=False, threads=threads, + in_name = pathlib.Path(file).with_suffix('.tif') + out_name = (out or in_name).with_suffix(format) + + data = tifffile.imread(str(in_name)) + clip = ImageSequenceClip(list(data), fps=fps) + + clip.write_videofile(out_name, fps, audio=False, threads=threads, logger=logger) -- GitLab