From 99f176af0932f62b2004907ac82a483722a7155d Mon Sep 17 00:00:00 2001 From: Genevieve Buckley <30920819+GenevieveBuckley@users.noreply.github.com> Date: Tue, 9 Jul 2024 18:06:10 +1000 Subject: [PATCH 1/2] Name custom colormaps to avoid adding duplicates to the napari colormap dropdown --- napari_tiff/napari_tiff_metadata.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/napari_tiff/napari_tiff_metadata.py b/napari_tiff/napari_tiff_metadata.py index 5e3bd0b..01be30e 100644 --- a/napari_tiff/napari_tiff_metadata.py +++ b/napari_tiff/napari_tiff_metadata.py @@ -145,12 +145,14 @@ def get_tiff_metadata(tif: TiffFile) -> dict[str, Any]: if page.photometric == PHOTOMETRIC.PALETTE and page.colormap is not None: # PALETTE - colormap = page.colormap - if numpy.max(colormap) > 255: - colormap = colormap / 65535.0 + colormap_values = page.colormap + if numpy.max(colormap_values) > 255: + colormap_values = colormap_values / 65535.0 else: - colormap = colormap / 255.0 - colormap = colormap.astype("float32").T + colormap_values = colormap_values / 255.0 + colormap_values = colormap_values.astype("float32").T + colormap = {"name": "PALETTE", "colors": colormap_values} + if colormap is None and page.photometric == PHOTOMETRIC.MINISWHITE: # MINISWHITE From 42318a4ce7062555bbdbe3575341bf4a7ec7deb9 Mon Sep 17 00:00:00 2001 From: Genevieve Buckley <30920819+GenevieveBuckley@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:02:49 +1000 Subject: [PATCH 2/2] Different files may contain different colormaps, but we must also check for duplicates --- napari_tiff/napari_tiff_colormaps.py | 2 ++ napari_tiff/napari_tiff_metadata.py | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/napari_tiff/napari_tiff_colormaps.py b/napari_tiff/napari_tiff_colormaps.py index f289b89..8b37987 100644 --- a/napari_tiff/napari_tiff_colormaps.py +++ b/napari_tiff/napari_tiff_colormaps.py @@ -1,5 +1,7 @@ import numpy +CUSTOM_COLORMAPS = {} # CUSTOM_COLORMAPS[colormap_hash] = colormap_name + def alpha_colormap(bitspersample=8, samples=4): """Return Alpha colormap.""" diff --git a/napari_tiff/napari_tiff_metadata.py b/napari_tiff/napari_tiff_metadata.py index 01be30e..2e1f6fe 100644 --- a/napari_tiff/napari_tiff_metadata.py +++ b/napari_tiff/napari_tiff_metadata.py @@ -3,7 +3,7 @@ import numpy from tifffile import PHOTOMETRIC, TiffFile, xml2dict -from napari_tiff.napari_tiff_colormaps import alpha_colormap, int_to_rgba +from napari_tiff.napari_tiff_colormaps import alpha_colormap, int_to_rgba, CUSTOM_COLORMAPS def get_metadata(tif: TiffFile) -> dict[str, Any]: @@ -151,8 +151,14 @@ def get_tiff_metadata(tif: TiffFile) -> dict[str, Any]: else: colormap_values = colormap_values / 255.0 colormap_values = colormap_values.astype("float32").T - colormap = {"name": "PALETTE", "colors": colormap_values} - + # set up custom colormap + colormap_hash = hash(tuple(tuple(x) for x in colormap_values)) + if colormap_hash in CUSTOM_COLORMAPS: + colormap_name = CUSTOM_COLORMAPS[colormap_hash] + else: + colormap_name = "PALETTE: " + str(colormap_hash) + CUSTOM_COLORMAPS[colormap_hash] = colormap_name + colormap = {"name": colormap_name, "colors": colormap_values} if colormap is None and page.photometric == PHOTOMETRIC.MINISWHITE: # MINISWHITE