From 8d14b2603d0e1ed8b40df81bf53a34f7ed6c1e56 Mon Sep 17 00:00:00 2001
From: David Manthey <david.manthey@kitware.com>
Date: Tue, 3 Mar 2020 09:48:16 -0500
Subject: [PATCH] Fix an issue where retiling some tile sources could fail.

---
 CHANGELOG.md                   | 5 +++++
 large_image/tilesource/base.py | 5 +++--
 test/test_source_gdal.py       | 9 +++++++++
 3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index b9bb90a5b..03cd0728f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,11 @@
 
 ## Unreleased
 
+## Version 1.0.4
+
+### Bug Fixes
+- Fixed an issue where retiling some tile sources could fail (#427)
+
 ## Version 1.0.3
 
 ### Features
diff --git a/large_image/tilesource/base.py b/large_image/tilesource/base.py
index b862f8a23..e2cc1cdc2 100644
--- a/large_image/tilesource/base.py
+++ b/large_image/tilesource/base.py
@@ -303,8 +303,8 @@ def setFormat(self, format, resample=False, imageKwargs=None):
 
     def _retileTile(self):
         """
-        Given the tile information, create a PIL image and merge multiple tiles
-        together to form a tile of a different size.
+        Given the tile information, create a numpy array and merge multiple
+        tiles together to form a tile of a different size.
         """
         retile = None
         xmin = int(max(0, self['x'] // self.metadata['tileWidth']))
@@ -316,6 +316,7 @@ def _retileTile(self):
                 tileData = self.source.getTile(
                     x, y, self.level,
                     numpyAllowed='always', sparseFallback=True, frame=self.frame)
+                tileData, _ = _imageToNumpy(tileData)
                 if retile is None:
                     retile = numpy.zeros(
                         (self.height, self.width) if len(tileData.shape) == 2 else
diff --git a/test/test_source_gdal.py b/test/test_source_gdal.py
index 27158754a..7c89edfb7 100644
--- a/test/test_source_gdal.py
+++ b/test/test_source_gdal.py
@@ -324,3 +324,12 @@ def testPalettizedGeotiff():
     image = numpy.asarray(image)
     assert list(image[0, 0, :]) == [0, 0, 0, 0]
     assert list(image[255, 0, :]) == [221, 201, 201, 255]
+
+
+def testRetileProjection():
+    imagePath = utilities.externaldata('data/landcover_sample_1000.tif.sha512')
+    ts = large_image_source_gdal.GDALFileTileSource(imagePath, projection='EPSG:3857')
+    ti = ts.getSingleTile(tile_size=dict(width=1000, height=1000), tile_position=1000)
+    assert ti['tile'].size == 3000000
+    tile = ts.getTile(1178, 1507, 12)
+    assert len(tile) > 1000