From 4737d9514211b0b350235d7308dfe13b92026310 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Mon, 9 Dec 2024 12:21:57 -0500 Subject: [PATCH] Read magnification values from DICOM when reading them via bioformats Closes #1740 --- CHANGELOG.md | 1 + .../bioformats/large_image_source_bioformats/__init__.py | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b41697399..a16bc4a8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Improvements - When using the multisource to composite multiple images with alpha channels, use nearest neighbor for upper tiles ([#1736](../../pull/1736)) +- Read magnification values from DICOM when reading them via bioformats ([#1741](../../pull/1741)) ### Changes diff --git a/sources/bioformats/large_image_source_bioformats/__init__.py b/sources/bioformats/large_image_source_bioformats/__init__.py index d2e856d88..6b092ba29 100644 --- a/sources/bioformats/large_image_source_bioformats/__init__.py +++ b/sources/bioformats/large_image_source_bioformats/__init__.py @@ -509,13 +509,15 @@ def _computeLevels(self): def _computeMagnification(self): self._magnification = {} - metadata = self._metadata['metadata'] + metadata = self._metadata.get('seriesMetadata', {}).copy() + metadata.update(self._metadata['metadata']) valuekeys = { 'x': [('Scaling|Distance|Value #1', 1e3)], 'y': [('Scaling|Distance|Value #2', 1e3)], } tuplekeys = [ ('Physical pixel size', 1e-3), + ('0028,0030 Pixel Spacing', 1), ] magkeys = [ 'Information|Instrument|Objective|NominalMagnification #1', @@ -528,11 +530,11 @@ def _computeMagnification(self): if 'mm_x' not in self._magnification and 'mm_y' not in self._magnification: for key, units in tuplekeys: if metadata.get(key): - found = re.match(r'^\D*(\d+(|\.\d+))\D+(\d+(|\.\d+))\D*$', metadata[key]) + found = re.match(r'^[^0-9.]*(\d*\.?\d+)[^0-9.]+(\d*\.?\d+)\D*$', metadata[key]) if found: try: self._magnification['mm_x'], self._magnification['mm_y'] = ( - float(found.groups()[0]) * units, float(found.groups()[2]) * units) + float(found.groups()[0]) * units, float(found.groups()[1]) * units) except Exception: pass for key in magkeys: