Commits

Oliver Tonnhofer committed 738b6b9

set background to bgcolor/transparent when cropping from smaller images

Comments (0)

Files changed (2)

mapproxy/image/tile.py

     def __init__(self, meta_tile, image_opts):
         self.meta_img = meta_tile.as_image()
         self.image_opts = image_opts
-    
+
     def get_tile(self, crop_coord, tile_size):
         """
         Return the cropped tile.
         maxx = minx + tile_size[0]
         maxy = miny + tile_size[1]
         
-        crop = self.meta_img.crop((minx, miny, maxx, maxy))
+        if (minx < 0 or miny < 0 or maxx > self.meta_img.size[0]
+            or maxy > self.meta_img.size[1]):
+
+            crop = self.meta_img.crop((
+                max(minx, 0),
+                max(miny, 0),
+                min(maxx, self.meta_img.size[0]),
+                min(maxy, self.meta_img.size[1])))
+            result = create_image(tile_size, self.image_opts)
+            result.paste(crop, (0, 0))
+            crop = result
+        else:
+            crop = self.meta_img.crop((minx, miny, maxx, maxy))
         return ImageSource(crop, size=tile_size, image_opts=self.image_opts)
     
 

mapproxy/test/unit/test_image.py

 from mapproxy.image import ImageSource, ReadBufWrapper, is_single_color_image, merge_images
 from mapproxy.image import _make_transparent as make_transparent
 from mapproxy.image.opts import ImageOptions
-from mapproxy.image.tile import TileMerger
+from mapproxy.image.tile import TileMerger, TileSplitter
 from mapproxy.image.transform import ImageTransformer
 from mapproxy.test.image import is_png, is_jpeg, is_tiff, create_tmp_image_file, check_format, create_debug_img
 from mapproxy.srs import SRS
         colors = sorted(img.getcolors(), reverse=True)
         eq_(colors, [(1550, (130, 140, 120, 100)), (900, (130, 150, 120, 0)),
             (25, (130, 150, 120, 255)), (25, (130, 100, 120, 0))])
-    
+
+
+class TestTileSplitter(object):
+    def test_background_larger_crop(self):
+        img = ImageSource(Image.new('RGB', (356, 266), (130, 140, 120)))
+        img_opts = ImageOptions('RGB')
+        splitter = TileSplitter(img, img_opts)
+        
+        tile = splitter.get_tile((0, 0), (256, 256))
+        
+        eq_(tile.size, (256, 256))
+        colors = tile.as_image().getcolors()
+        eq_(colors, [(256*256, (130, 140, 120))])
+        
+        tile = splitter.get_tile((256, 256), (256, 256))
+        
+        eq_(tile.size, (256, 256))
+        colors = tile.as_image().getcolors()
+        eq_(sorted(colors), [(10*100, (130, 140, 120)), (256*256-10*100, (255, 255, 255))])
+        
+    def test_background_larger_crop_with_transparent(self):
+        img = ImageSource(Image.new('RGBA', (356, 266), (130, 140, 120, 255)))
+        img_opts = ImageOptions('RGBA', transparent=True)
+        splitter = TileSplitter(img, img_opts)
+        
+        tile = splitter.get_tile((0, 0), (256, 256))
+        
+        eq_(tile.size, (256, 256))
+        colors = tile.as_image().getcolors()
+        eq_(colors, [(256*256, (130, 140, 120, 255))])
+        
+        tile = splitter.get_tile((256, 256), (256, 256))
+        
+        eq_(tile.size, (256, 256))
+        colors = tile.as_image().getcolors()
+        eq_(sorted(colors), [(10*100, (130, 140, 120, 255)), (256*256-10*100, (255, 255, 255, 0))])
+