Skip to content

Commit

Permalink
Eliminate PIL warning from why.read() (#1544)
Browse files Browse the repository at this point in the history
## Description

Don't warn if PIL is missing when importing `ImageMetric` for
`why.read()`.

## Changes

- Don't issue warning on import, just `ImageMetric` construction.
- Silence the warning when setting up for `why.read()`

## Related

Relates to #1490
Closes #1490
  • Loading branch information
richard-rogers committed Jul 2, 2024
1 parent f7643ec commit d86f268
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions python/whylogs/extras/image_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@

logger = logging.getLogger(__name__)


try:
import numpy as np # type: ignore
from PIL import Image
from PIL.Image import Image as ImageType # type: ignore
from PIL.ImageStat import Stat # type: ignore
from PIL.TiffImagePlugin import IFDRational # type: ignore
from PIL.TiffTags import TAGS # type: ignore
except ImportError as e:
except ImportError:
ImageType = None # type: ignore
logger.warning(str(e))
logger.warning("Unable to load PIL; install Pillow for image support")


DEFAULT_IMAGE_FEATURES: List[str] = []

Expand Down Expand Up @@ -147,6 +147,10 @@ def resolve(self, name: str, why_type: DataType, fi_disabled: bool = False) -> D
return metrics


def _no_pil_msg():
logger.error("Unable to import PIL; install Pillow for image support")


@dataclass(frozen=True)
class ImageMetricConfig(MetricConfig):
allowed_exif_tags: Set[str] = field(default_factory=set)
Expand All @@ -164,7 +168,7 @@ def __init__(
fi_disabled: bool = False,
):
if ImageType is None:
logger.error("Install Pillow for image support")
_no_pil_msg()
super(ImageMetric, self).__init__(submetrics)
self._allowed_exif_tags = allowed_exif_tags or set()
self._forbidden_exif_tags = forbidden_exif_tags or set()
Expand Down Expand Up @@ -303,4 +307,7 @@ def resolve(self, name: str, why_type: DataType, column_schema: ColumnSchema) ->


# Register it so Multimetric and ProfileView can deserialize
tmp = _no_pil_msg
_no_pil_msg = lambda: None # noqa
register_metric(ImageMetric)
_no_pil_msg = tmp

0 comments on commit d86f268

Please sign in to comment.