Skip to content

Commit

Permalink
Merge pull request #33 from GenevieveBuckley/photometric-palette-bug
Browse files Browse the repository at this point in the history
Name custom colormaps for photometric PALETTE
  • Loading branch information
GenevieveBuckley authored Jul 12, 2024
2 parents 7dd6c3b + 42318a4 commit 142dab1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions napari_tiff/napari_tiff_colormaps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy

CUSTOM_COLORMAPS = {} # CUSTOM_COLORMAPS[colormap_hash] = colormap_name


def alpha_colormap(bitspersample=8, samples=4):
"""Return Alpha colormap."""
Expand Down
20 changes: 14 additions & 6 deletions napari_tiff/napari_tiff_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -145,12 +145,20 @@ 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
# 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
Expand Down

0 comments on commit 142dab1

Please sign in to comment.