From 7eeaedfe8fdc835e1bfc9b83ace1b540faf15597 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Wed, 29 Jul 2020 16:02:54 -0400 Subject: [PATCH] Read more OME Tiff files. --- .../large_image_source_ometiff/__init__.py | 24 +++++++++++++------ .../large_image_source_tiff/tiff_reader.py | 3 ++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/sources/ometiff/large_image_source_ometiff/__init__.py b/sources/ometiff/large_image_source_ometiff/__init__.py index 1e1e29333..d93eff6f7 100644 --- a/sources/ometiff/large_image_source_ometiff/__init__.py +++ b/sources/ometiff/large_image_source_ometiff/__init__.py @@ -114,7 +114,7 @@ def __init__(self, path, **kwargs): self._omeLevels = [omebylevel.get(key) for key in range(max(omebylevel.keys()) + 1)] if base._tiffInfo.get('istiled'): self._tiffDirectories = [ - TiledTiffDirectory(largeImagePath, int(entry['TiffData'][0]['IFD'])) + TiledTiffDirectory(largeImagePath, int(entry['TiffData'][0].get('IFD', 0))) if entry else None for entry in self._omeLevels] else: @@ -182,7 +182,7 @@ def _checkForOMEZLoop(self, largeImagePath): info['Image']['Pixels']['PlanesFromZloop'] = 'true' info['Image']['Pixels']['SizeZ'] = str(zloop) - def _parseOMEInfo(self): + def _parseOMEInfo(self): # noqa if isinstance(self._omeinfo['Image'], dict): self._omeinfo['Image'] = [self._omeinfo['Image']] for img in self._omeinfo['Image']: @@ -190,20 +190,28 @@ def _parseOMEInfo(self): img['Pixels']['TiffData'] = [img['Pixels']['TiffData']] if isinstance(img['Pixels'].get('Plane'), dict): img['Pixels']['Plane'] = [img['Pixels']['Plane']] + if isinstance(img['Pixels'].get('Channels'), dict): + img['Pixels']['Channels'] = [img['Pixels']['Channels']] try: self._omebase = self._omeinfo['Image'][0]['Pixels'] + if isinstance(self._omebase.get('Plane'), dict): + self._omebase['Plane'] = [self._omebase['Plane']] if ((not len(self._omebase['TiffData']) or len(self._omebase['TiffData']) == 1) and - len(self._omebase['Plane'])): + (len(self._omebase.get('Plane', [])) or + len(self._omebase.get('Channel', [])))): if not len(self._omebase['TiffData']) or self._omebase['TiffData'][0] == {}: - self._omebase['TiffData'] = self._omebase['Plane'] + self._omebase['TiffData'] = self._omebase.get( + 'Plane', self._omebase.get('Channel')) elif (int(self._omebase['TiffData'][0].get('PlaneCount', 0)) == - len(self._omebase['Plane'])): - planes = copy.deepcopy(self._omebase['Plane']) + len(self._omebase.get('Plane', self._omebase.get('Channel', [])))): + planes = copy.deepcopy(self._omebase.get('Plane', self._omebase.get('Channel'))) for idx, plane in enumerate(planes): plane['IFD'] = plane.get( 'IFD', int(self._omebase['TiffData'][0].get('IFD', 0)) + idx) self._omebase['TiffData'] = planes + if isinstance(self._omebase['TiffData'], dict): + self._omebase['TiffData'] = [self._omebase['TiffData']] if len({entry.get('UUID', {}).get('FileName', '') for entry in self._omebase['TiffData']}) > 1: raise TileSourceException('OME Tiff references multiple files') @@ -211,8 +219,10 @@ def _parseOMEInfo(self): int(self._omebase['SizeT']) * int(self._omebase['SizeZ']) or len(self._omebase['TiffData']) != len( self._omebase.get('Plane', self._omebase['TiffData']))): - raise TileSourceException('OME Tiff contains frames that contain multiple planes') + raise TileSourceException( + 'OME Tiff contains frames that contain multiple planes') except (KeyError, ValueError, IndexError): + print('B') raise TileSourceException('OME Tiff does not contain an expected record') def getMetadata(self): diff --git a/sources/tiff/large_image_source_tiff/tiff_reader.py b/sources/tiff/large_image_source_tiff/tiff_reader.py index f11231571..2fbc3d747 100644 --- a/sources/tiff/large_image_source_tiff/tiff_reader.py +++ b/sources/tiff/large_image_source_tiff/tiff_reader.py @@ -133,7 +133,8 @@ def __init__(self, filePath, directoryNum, mustBeTiled=True, subDirectoryNum=0, self._open(filePath, directoryNum, subDirectoryNum) self._loadMetadata() config.getConfig('logger').debug( - 'TiffDirectory %d:%d Information %r', directoryNum, subDirectoryNum, self._tiffInfo) + 'TiffDirectory %d:%d Information %r', + directoryNum, subDirectoryNum or 0, self._tiffInfo) try: if validate: self._validate()