Skip to content
Snippets Groups Projects
Commit ba055f61 authored by Simon Sebastian Humpohl's avatar Simon Sebastian Humpohl
Browse files

Use tifffile to read the image

parent 406123e5
Branches Sprint/2020-20
Tags
1 merge request!72Add image.py with function to convert multipage .tif to video
......@@ -61,6 +61,7 @@ qcodes = [
]
image = [
"moviepy",
"tifffile[all]"
]
doc = [
"sphinx",
......
......@@ -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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment