Skip to content

Commit 3fb155d

Browse files
committed
DeepZoomGenerator: Move coordinate calculations to a helper function
1 parent 6963622 commit 3fb155d

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

openslide/deepzoom.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,21 @@ def get_tile(self, level, address):
117117
address: the address of the tile within the level as a (col, row)
118118
tuple."""
119119

120-
t_location = address
120+
# Read tile
121+
args, z_size = self._get_tile_info(level, address)
122+
tile = self._osr.read_region(*args)
121123

124+
# Apply on solid background
125+
bg = Image.new('RGB', tile.size, self._bg_color)
126+
tile = Image.composite(tile, bg, tile)
127+
128+
# Scale to the correct size
129+
if tile.size != z_size:
130+
tile.thumbnail(z_size, Image.ANTIALIAS)
131+
132+
return tile
133+
134+
def _get_tile_info(self, level, t_location):
122135
# Check parameters
123136
if level < 0 or level >= self._levels:
124137
raise ValueError("Invalid level")
@@ -135,14 +148,14 @@ def get_tile(self, level, address):
135148
z_overlap_br = tuple(self._z_overlap * int(t != t_lim - 1)
136149
for t, t_lim in zip(t_location, self.level_tiles[level]))
137150

138-
# Get final size of this tile
151+
# Get final size of the tile
139152
z_size = tuple(min(self._z_t_downsample,
140153
z_lim - self._z_t_downsample * t) + z_tl + z_br
141154
for t, z_lim, z_tl, z_br in
142155
zip(t_location, self._z_dimensions[level], z_overlap_tl,
143156
z_overlap_br))
144157

145-
# Obtain the region
158+
# Obtain the region coordinates
146159
z_location = [self._z_from_t(t) for t in t_location]
147160
l_location = [self._l_from_z(level, z) - z_tl
148161
for z, z_tl in zip(z_location, z_overlap_tl)]
@@ -152,17 +165,9 @@ def get_tile(self, level, address):
152165
l_lim - math.ceil(l)))
153166
for l, dz, l_lim in
154167
zip(l_location, z_size, self._l_dimensions[layer])]
155-
tile = self._osr.read_region(l0_location, layer, l_size)
156168

157-
# Apply on solid background
158-
bg = Image.new('RGB', tile.size, self._bg_color)
159-
tile = Image.composite(tile, bg, tile)
160-
161-
# Scale to the correct size
162-
if tile.size != z_size:
163-
tile.thumbnail(z_size, Image.ANTIALIAS)
164-
165-
return tile
169+
# Return read_region() parameters plus tile size for final scaling
170+
return ((l0_location, layer, l_size), z_size)
166171

167172
def _l0_from_l(self, layer, l):
168173
return self._l0_l_downsamples[layer] * l

0 commit comments

Comments
 (0)