Skip to content

Commit

Permalink
Implement ICC profile in the pyvips backend (#206)
Browse files Browse the repository at this point in the history
- Partially addresses #184
  • Loading branch information
jonasteuwen authored Dec 9, 2023
1 parent 1aeb58d commit f4e192a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
8 changes: 1 addition & 7 deletions dlup/cli/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@
import shapely

from dlup._image import Resampling
from dlup.annotations import (
AnnotationClass,
AnnotationType,
Polygon,
SingleAnnotationWrapper,
WsiAnnotations,
)
from dlup.annotations import AnnotationClass, AnnotationType, Polygon, SingleAnnotationWrapper, WsiAnnotations
from dlup.cli import file_path
from dlup.data.dataset import TiledWsiDataset
from dlup.experimental_backends import ImageBackend # type: ignore
Expand Down
30 changes: 30 additions & 0 deletions dlup/experimental_backends/pyvips_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
# Copyright (c) dlup contributors
from __future__ import annotations

import io
import warnings
from distutils.version import LooseVersion
from typing import Any

import numpy as np
import openslide
import PIL.Image
import PIL.ImageCms
import pyvips
from PIL.ImageCms import ImageCmsProfile

from dlup import UnsupportedSlideError
from dlup.backends.common import AbstractSlideBackend, numpy_to_pil
Expand Down Expand Up @@ -171,6 +175,32 @@ def properties(self):
keys = self._images[0].get_fields()
return {key: self._images[0].get_value(key) for key in keys}

@property
def color_profile(self) -> ImageCmsProfile | None:
"""
Returns the color profile of the image if available. Otherwise returns None.
Returns
-------
ImageCmsProfile, optional
The color profile of the image.
"""
if LooseVersion(openslide.__library_version__) < LooseVersion("4.0.0"):
warnings.warn(
"Color profile support is only available for openslide >= 4.0.0. "
f"You have version {openslide.__library_version__}. "
"Please update your openslide installation if you want to use this feature (recommended)."
)
return None

if self._loader == "tiffload":
raise NotImplementedError("ICC profile is not implemented for tiff loader.")

if "icc-profile-data" not in self.properties:
return None

return PIL.ImageCms.getOpenProfile(io.BytesIO(self.properties["icc-profile-data"]))

@property
def magnification(self) -> float | None:
"""Returns the objective power at which the WSI was sampled."""
Expand Down

0 comments on commit f4e192a

Please sign in to comment.