Skip to content

Commit

Permalink
Update imagewriter.py improved coorindate checks
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeySpronck authored Aug 30, 2024
1 parent 6f9d639 commit 39a6fbe
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions wholeslidedata/interoperability/asap/imagewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,26 @@ def _tile_and_coordinate_checks_and_corrections(self, tile, coordinates):
f"Invalid tile shape initialized: {self._tile_shape}, tile shape should contain at 2 or 3 dimensions"
)

# Check if coordinates are aligned with the predefined tile grid
if coordinates[0] % self._tile_shape[0] != 0 or coordinates[1] % self._tile_shape[1] != 0:
raise CoordinateError(
f"Coordinates {coordinates} are not multiples of the tile size {self._tile_shape[:2]}"
)

# Check if coordinates are within the dimensions
x, y = coordinates
if x >= self._dimensions[0] or y >= self._dimensions[1]:
print(f"Tile coordinates {coordinates} are completely outside the dimensions {self._dimensions}... Skipping tile...")
return None

x_end = x + self._tile_shape[0]
y_end = y + self._tile_shape[1]
if x_end > self._dimensions[0] or y_end > self._dimensions[1]:
print(f"Tile outer coordinates {x_end, y_end} are exeeding the dimensions {self._dimensions}... Cropping tile to fit inside the dimensions...")
tile_max_x = self._dimensions[0] - x
tile_max_y = self._dimensions[1] - y
tile = tile[:tile_max_x, :tile_max_y]
if coordinates:
x, y = coordinates
# Check if coordinates are aligned with the predefined tile grid
if x % self._tile_shape[0] != 0 or y % self._tile_shape[1] != 0:
raise CoordinateError(
f"Coordinates {coordinates} are not multiples of the tile size {self._tile_shape[:2]}"
)

# Check if coordinates are within the dimensions
if x >= self._dimensions[0] or y >= self._dimensions[1]:
print(f"Tile's upper left coordinates {coordinates} are completely outside the dimensions {self._dimensions}... Skipping tile...")
return None

x_end = x + self._tile_shape[0]
y_end = y + self._tile_shape[1]
if x_end > self._dimensions[0] or y_end > self._dimensions[1]:
print(f"Tile's lower right coordinates {x_end, y_end} are exeeding the dimensions {self._dimensions}... Cropping tile to fit inside the dimensions...")
tile_max_x = self._dimensions[0] - x
tile_max_y = self._dimensions[1] - y
tile = tile[:tile_max_x, :tile_max_y]
return tile

def write_tile(self, tile, coordinates=None, mask=None):
Expand Down

0 comments on commit 39a6fbe

Please sign in to comment.