Skip to content

Commit

Permalink
Merge pull request #1819 from girder/openslide-background-color
Browse files Browse the repository at this point in the history
Apply background color information from openslide sources
  • Loading branch information
manthey authored Feb 11, 2025
2 parents 1b6e426 + 0a9c8fd commit 69ce537
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 1.31.2

### Improvements

- Apply background color information from openslide sources ([#1819](../../pull/1819))

## 1.31.1

### Improvements
Expand Down
11 changes: 11 additions & 0 deletions sources/openslide/large_image_source_openslide/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from importlib.metadata import PackageNotFoundError
from importlib.metadata import version as _importlib_version

import numpy as np
import openslide
import PIL
import tifftools
Expand Down Expand Up @@ -186,6 +187,12 @@ def __init__(self, path, **kwargs): # noqa
self._svslevels = self._svslevels[prevlevels - self.levels:]
except Exception:
pass
try:
self._background = tuple(int(
self._openslide.properties['openslide.background-color']
[i * 2:i * 2 + 2], 16) for i in range(3))
except Exception:
self._background = None
self._populatedLevels = len({l['svslevel'] for l in self._svslevels})

def _getTileSize(self):
Expand Down Expand Up @@ -356,6 +363,10 @@ def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False, **kwargs):
retries -= 1
if retries <= 0:
raise TileSourceError(msg)
if tile.mode == 'RGBA' and self._background:
tile = np.array(tile)
tile[tile[:, :, -1] == 0, :3] = self._background
tile = PIL.Image.fromarray(tile, 'RGBA')
# Always scale to the svs level 0 tile size.
if svslevel['scale'] != 1:
tile = tile.resize((self.tileWidth, self.tileHeight),
Expand Down

0 comments on commit 69ce537

Please sign in to comment.