Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

from_tiff convenience function #51

Merged
merged 5 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Switch from tifffile to inline TIFF-parsing code (#52)
Also ignore Flake E203 since Black imposes some patterns that are incompatible
with it (and Black forces formatting for all cases that E203 covers, anyway).

Co-authored-by: Christoph Gohlke <cgohlke@uci.edu>

Co-authored-by: Christoph Gohlke <cgohlke@uci.edu>
  • Loading branch information
jmuhlich and cgohlke authored Sep 15, 2020
commit e401c50bb40816a40e0b30cf5804dee09d4a9329
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ tiff = tifffile>=2019.2.22
where=src

[flake8]
ignore = W503, C901, E501
ignore = W503, C901, E501, E203
max-line-length = 88

[mypy]
Expand Down
43 changes: 27 additions & 16 deletions src/ome_types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from pathlib import Path
from struct import unpack
from typing import Union

try:
Expand Down Expand Up @@ -42,9 +43,7 @@ def from_xml(xml: Union[Path, str]) -> OME: # type: ignore


def from_tiff(path: Union[Path, str]) -> OME:
"""Generate OME metadata object from OME TIFF.

Requires tifffile.
"""Generate OME metadata object from OME-TIFF path.

Parameters
----------
Expand All @@ -58,19 +57,31 @@ def from_tiff(path: Union[Path, str]) -> OME:

Raises
------
ImportError
If `tifffile` is not installed
ValueError
If the TIFF file has no OME metadata.
"""
try:
import tifffile
except ImportError:
raise ImportError(
"Please `pip install tifffile` to extract OME metadata from a TIFF."
) from None

with tifffile.TiffFile(os.fspath(path)) as tf:
if not tf.ome_metadata:
raise ValueError(f"No OME metadata found in {path}")
return from_xml(tf.ome_metadata)
"""Return value of first ImageDescription tag from open TIFF file."""
fh = Path(path).open(mode="rb")
offsetsize, offsetformat, tagnosize, tagnoformat, tagsize, codeformat = {
b"II*\0": (4, "<I", 2, "<H", 12, "<H"),
b"MM\0*": (4, ">I", 2, ">H", 12, ">H"),
b"II+\0": (8, "<Q", 8, "<Q", 20, "<H"),
b"MM\0+": (8, ">Q", 8, ">Q", 20, ">H"),
}[fh.read(4)]
fh.read(4 if offsetsize == 8 else 0)
fh.seek(unpack(offsetformat, fh.read(offsetsize))[0])
for _ in range(unpack(tagnoformat, fh.read(tagnosize))[0]):
tagstruct = fh.read(tagsize)
if unpack(codeformat, tagstruct[:2])[0] == 270:
size = unpack(offsetformat, tagstruct[4 : 4 + offsetsize])[0]
if size <= offsetsize:
desc = tagstruct[4 + offsetsize : 4 + offsetsize + size]
break
fh.seek(unpack(offsetformat, tagstruct[-offsetsize:])[0])
desc = fh.read(size)
break
else:
raise ValueError(f"No OME metadata found in file: {path}")
if desc[-1] == 0:
desc = desc[:-1]
return from_xml(desc.decode("utf-8"))