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

Name custom colormaps for photometric PALETTE #33

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading