def assert_image_mode(img, mode): pos = img.tell() try: img = Image.open(img) assert img.mode == mode finally: img.seek(pos)
def test_get_legendgraphic_111(self): self.common_lg_req_111.params['scale'] = '5.0' with tmp_image((256, 256), format='png') as img: img_data = img.read() expected_req1 = ({ 'path': r'/service?LAYER=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetLegendGraphic&SCALE=5.0&' '&VERSION=1.1.1&SLD_VERSION=1.1.0' }, { 'body': img_data, 'headers': { 'content-type': 'image/png' } }) expected_req2 = ({ 'path': r'/service?LAYER=bar&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetLegendGraphic&SCALE=5.0&' '&VERSION=1.1.1&SLD_VERSION=1.1.0' }, { 'body': img_data, 'headers': { 'content-type': 'image/png' } }) with mock_httpd(('localhost', 42423), [expected_req1, expected_req2]): resp = self.app.get(self.common_lg_req_111) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) assert Image.open(data).size == (256, 512)
def test_combined_transp_color(self): # merged to one request because both layers share the same transparent_color common_params = (r'?SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=200&SRS=EPSG%3A4326&styles=' '&VERSION=1.1.1&BBOX=9.0,50.0,10.0,51.0' '&WIDTH=200&transparent=True') with tmp_image((200, 200), color=(255, 0, 0), format='png') as img: img = img.read() expected_req = [ ({ 'path': '/service_a' + common_params + '&layers=a_iopts_one,a_iopts_two' }, { 'body': img, 'headers': { 'content-type': 'image/png' } }), ] with mock_httpd(('localhost', 42423), expected_req): self.common_map_req.params.layers = 'layer_image_opts1,layer_image_opts2' self.common_map_req.params.transparent = True resp = self.app.get(self.common_map_req) resp.content_type = 'image/png' data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) eq_(img.getcolors()[0], ((200 * 200), (255, 0, 0, 0)))
def assert_image_mode(img, mode): pos = img.tell() try: img = Image.open(img) eq_(img.mode, mode) finally: img.seek(pos)
def test_get_map_intersection(self, app, cache_dir): with tmp_image((256, 256), format="jpeg") as img: expected_req = ( { "path": r"/service?LAYERs=foo,bar&SERVICE=WMS&FORMAT=image%2Fjpeg" "&REQUEST=GetMap&HEIGHT=91&SRS=EPSG%3A4326&styles=" "&VERSION=1.1.1&BBOX=10,15,30,31" "&WIDTH=114" }, { "body": img.read(), "headers": { "content-type": "image/jpeg" } }, ) with mock_httpd(("localhost", 42423), [expected_req]): self.common_map_req.params.bbox = 0, 0, 40, 40 self.common_map_req.params.transparent = True resp = app.get(self.common_map_req) assert resp.content_type == "image/png" data = BytesIO(resp.body) assert is_png(data) assert Image.open(data).mode == "RGBA" assert cache_dir.join( "wms_cache_EPSG4326/03/000/000/004/000/000/002.jpeg").check()
def test_transparent_watermark_tile(self): with tmp_image((256, 256), format='png', color=(0, 0, 0, 0), mode='RGBA') as img: expected_req = ({ 'path': r'/service?LAYERs=blank&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=256&SRS=EPSG%3A4326&styles=' '&VERSION=1.1.1&BBOX=-180.0,-90.0,0.0,90.0' '&WIDTH=256' }, { 'body': img.read(), 'headers': { 'content-type': 'image/jpeg' } }) with mock_httpd(('localhost', 42423), [expected_req]): resp = self.app.get( '/tms/1.0.0/watermark_transp/EPSG4326/0/0/0.png') eq_(resp.content_type, 'image/png') img = Image.open(BytesIO(resp.body)) colors = img.getcolors() assert len(colors) >= 2 eq_(sorted(colors)[-1][1], (0, 0, 0, 0))
def test_layers_with_opacity(self): # overlay with opacity -> request should not be combined common_params = (r'?SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=200&SRS=EPSG%3A4326&styles=' '&VERSION=1.1.1&BBOX=9.0,50.0,10.0,51.0' '&WIDTH=200') img_bg = create_tmp_image((200, 200), color=(0, 0, 0)) img_fg = create_tmp_image((200, 200), color=(255, 0, 128)) expected_req = [ ({'path': '/service_a' + common_params + '&layers=a_one'}, {'body': img_bg, 'headers': {'content-type': 'image/png'}}), ({'path': '/service_a' + common_params + '&layers=a_two'}, {'body': img_fg, 'headers': {'content-type': 'image/png'}}), ] with mock_httpd(('localhost', 42423), expected_req): self.common_map_req.params.layers = 'opacity_base,opacity_overlay' resp = self.app.get(self.common_map_req) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) eq_(img.getcolors()[0], ((200*200),(127, 0, 64)))
def test_transparent_watermark_tile(self, app): with tmp_image((256, 256), format="png", color=(0, 0, 0, 0), mode="RGBA") as img: expected_req = ( { "path": r"/service?LAYERs=blank&SERVICE=WMS&FORMAT=image%2Fpng" "&REQUEST=GetMap&HEIGHT=256&SRS=EPSG%3A4326&styles=" "&VERSION=1.1.1&BBOX=-180.0,-90.0,0.0,90.0" "&WIDTH=256" }, { "body": img.read(), "headers": { "content-type": "image/jpeg" } }, ) with mock_httpd(("localhost", 42423), [expected_req]): resp = app.get( "/tms/1.0.0/watermark_transp/EPSG4326/0/0/0.png") assert resp.content_type == "image/png" img = Image.open(BytesIO(resp.body)) colors = img.getcolors() assert len(colors) >= 2 assert sorted(colors)[-1][1] == (0, 0, 0, 0)
def test_combined_transp_color(self, app): # merged to one request because both layers share the same transparent_color common_params = (r"?SERVICE=WMS&FORMAT=image%2Fpng" "&REQUEST=GetMap&HEIGHT=200&SRS=EPSG%3A4326&styles=" "&VERSION=1.1.1&BBOX=9.0,50.0,10.0,51.0" "&WIDTH=200&transparent=True") with tmp_image((200, 200), color=(255, 0, 0), format="png") as img: img = img.read() expected_req = [( { "path": "/service_a" + common_params + "&layers=a_iopts_one,a_iopts_two" }, { "body": img, "headers": { "content-type": "image/png" } }, )] with mock_httpd(("localhost", 42423), expected_req): self.common_map_req.params.layers = ( "layer_image_opts1,layer_image_opts2") self.common_map_req.params.transparent = True resp = app.get(self.common_map_req) resp.content_type = "image/png" data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) assert img.getcolors()[0] == ((200 * 200), (255, 0, 0, 0))
def test_get_tile_uncached(self): resp = self.app.get('/tms/1.0.0/wms_cache/0/0/0.jpeg') eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) eq_(img.mode, 'RGBA') eq_(img.getcolors(), [(256 * 256, (255, 255, 255, 0))])
def test_get_map(self, app): resp = app.get(self.common_map_req) assert resp.content_type == "image/png" data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) img = img.convert("RGB") assert img.getcolors() == [(200 * 200, (255, 0, 0))]
def test_get_tile_uncached(self): resp = self.app.get('/tms/1.0.0/wms_cache/0/0/0.jpeg') eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) eq_(img.mode, 'RGBA') eq_(img.getcolors(), [(256*256, (255, 255, 255, 0))])
def test_get_tile_uncached(self, app): resp = app.get("/tms/1.0.0/wms_cache/0/0/0.jpeg") assert resp.content_type == "image/png" data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) assert img.mode == "RGBA" assert img.getcolors() == [(256 * 256, (255, 255, 255, 0))]
def test_output_formats_png8(self): img = Image.new("RGBA", (100, 100)) ir = ImageSource(img, image_opts=PNG_FORMAT) img = Image.open( ir.as_buffer(ImageOptions(colors=256, transparent=True, format="image/png")) ) assert img.mode == "P" assert img.getpixel((0, 0)) == 255
def test_get_map(self): resp = self.app.get(self.common_map_req) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) img = img.convert('RGB') eq_(img.getcolors(), [(200*200, (255, 0, 0))])
def test_output_formats_png24(self): img = Image.new('RGBA', (100, 100)) image_opts = PNG_FORMAT.copy() image_opts.colors = 0 # TODO image_opts ir = ImageSource(img, image_opts=image_opts) img = Image.open(ir.as_buffer()) eq_(img.mode, 'RGBA') assert img.getpixel((0, 0)) == (0, 0, 0, 0)
def test_output_formats_png24(self): img = Image.new("RGBA", (100, 100)) image_opts = PNG_FORMAT.copy() image_opts.colors = 0 # TODO image_opts ir = ImageSource(img, image_opts=image_opts) img = Image.open(ir.as_buffer()) assert img.mode == "RGBA" assert img.getpixel((0, 0)) == (0, 0, 0, 0)
def test_get_map_cached(self): resp = self.app.get(self.common_map_req) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) eq_(img.mode, 'RGB') # cached image has more that 256 colors, getcolors -> None eq_(img.getcolors(), None)
def test_output_formats_greyscale_png(self): img = Image.new('L', (100, 100)) ir = ImageSource(img, image_opts=PNG_FORMAT) img = Image.open( ir.as_buffer( ImageOptions(colors=256, transparent=True, format='image/png'))) assert img.mode == 'P' assert img.getpixel((0, 0)) == 255
def test_get_map_uncached(self): self.common_map_req.params['bbox'] = '10,10,20,20' resp = self.app.get(self.common_map_req) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) eq_(img.mode, 'RGBA') eq_(img.getcolors(), [(200 * 200, (255, 255, 255, 0))])
def test_get_tile_cached(self): resp = self.app.get('/tms/1.0.0/wms_cache/0/0/1.jpeg') eq_(resp.content_type, 'image/jpeg') data = BytesIO(resp.body) assert is_jpeg(data) img = Image.open(data) eq_(img.mode, 'RGB') # cached image has more that 256 colors, getcolors -> None eq_(img.getcolors(), None)
def test_get_tile_cached(self): resp = self.app.get('/tms/1.0.0/wms_cache/0/0/1.jpeg') eq_(resp.content_type, 'image/jpeg') data = BytesIO(resp.body) assert is_jpeg(data) img = Image.open(data) eq_(img.mode, 'RGB') # cached image has more that 256 colors, getcolors -> None eq_(img.getcolors(), None)
def test_get_map_cached(self, app, fixture_cache_data): resp = app.get(self.common_map_req) assert resp.content_type == "image/png" data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) assert img.mode == "RGB" # cached image has more that 256 colors, getcolors -> None assert img.getcolors() == None
def test_get_map_uncached(self, app): self.common_map_req.params["bbox"] = "10,10,20,20" resp = app.get(self.common_map_req) assert resp.content_type == "image/png" data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) assert img.mode == "RGBA" assert img.getcolors() == [(200 * 200, (255, 255, 255, 0))]
def test_get_map_cached(self): resp = self.app.get(self.common_map_req) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) eq_(img.mode, 'RGB') # cached image has more that 256 colors, getcolors -> None eq_(img.getcolors(), None)
def test_get_map_uncached(self): self.common_map_req.params['bbox'] = '10,10,20,20' resp = self.app.get(self.common_map_req) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) eq_(img.mode, 'RGBA') eq_(img.getcolors(), [(200*200, (255, 255, 255, 0))])
def test_get_tile_cached(self, app, fixture_cache_data): resp = app.get("/tms/1.0.0/wms_cache/0/0/1.jpeg") assert resp.content_type == "image/jpeg" data = BytesIO(resp.body) assert is_jpeg(data) img = Image.open(data) assert img.mode == "RGB" # cached image has more that 256 colors, getcolors -> None assert img.getcolors() == None
def test_save_with_unsupported_transparency(self): # check if encoding of non-RGB image with tuple as transparency # works. workaround for Pillow #2633 img = Image.new("P", (100, 100)) img.info["transparency"] = (0, 0, 0) image_opts = PNG_FORMAT.copy() ir = ImageSource(img, image_opts=image_opts) img = Image.open(ir.as_buffer()) assert img.mode == "P"
def test_save_with_unsupported_transparency(self): # check if encoding of non-RGB image with tuple as transparency # works. workaround for Pillow #2633 img = Image.new('P', (100, 100)) img.info['transparency'] = (0, 0, 0) image_opts = PNG_FORMAT.copy() ir = ImageSource(img, image_opts=image_opts) img = Image.open(ir.as_buffer()) eq_(img.mode, 'P')
def test_get_map_outside(self): self.common_map_req.params.bbox = -90, 0, 0, 90 self.common_map_req.params['bgcolor'] = '0xff0005' resp = self.app.get(self.common_map_req) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) eq_(img.mode, 'RGB') eq_(img.getcolors(), [(200*200, (255, 0, 5))])
def test_get_map_outside(self, app): self.common_map_req.params.bbox = -90, 0, 0, 90 self.common_map_req.params["bgcolor"] = "0xff0005" resp = app.get(self.common_map_req) assert resp.content_type == "image/png" data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) assert img.mode == "RGB" assert img.getcolors() == [(200 * 200, (255, 0, 5))]
def is_transparent(img_data): data = BytesIO(img_data) img = Image.open(data) if img.mode == 'P': img = img.convert('RGBA') if img.mode == 'RGBA': return any(img.histogram()[-256:-1]) raise NotImplementedError( 'assert_is_transparent works only for RGBA images, got %s image' % img.mode)
def is_transparent(img_data): data = BytesIO(img_data) img = Image.open(data) if img.mode == 'P': img = img.convert('RGBA') if img.mode == 'RGBA': return any(img.histogram()[-256:-1]) raise NotImplementedError( 'assert_is_transparent works only for RGBA images, got %s image' % img.mode)
def test_get_map_outside_transparent(self, app): self.common_map_req.params.bbox = -90, 0, 0, 90 self.common_map_req.params.transparent = True resp = app.get(self.common_map_req) assert resp.content_type == "image/png" data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) assert img.mode == "RGBA" assert img.getcolors()[0][0] == 200 * 200 assert img.getcolors()[0][1][3] == 0 # transparent
def bgcolor_ratio(img_data): """ Return the ratio of the primary/bg color. 1 == only bg color. """ data = BytesIO(img_data) img = Image.open(data) total_colors = img.size[0] * img.size[1] colors = img.getcolors() colors.sort() bgcolor = colors[-1][0] return bgcolor/total_colors
def test_get_tile_with_watermark_cache(self): with tmp_image((256, 256), format='png', color=(0, 0, 0)) as img: expected_req = ({'path': r'/tiles/01/000/000/000/000/000/000.png'}, {'body': img.read(), 'headers': {'content-type': 'image/png'}}) with mock_httpd(('localhost', 42423), [expected_req]): resp = self.app.get('/tms/1.0.0/watermark_cache/0/0/0.png') eq_(resp.content_type, 'image/png') img = Image.open(BytesIO(resp.body)) colors = img.getcolors() assert len(colors) >= 2 eq_(sorted(colors)[-1][1], (0, 0, 0))
def test_get_map_outside_transparent(self): self.common_map_req.params.bbox = -90, 0, 0, 90 self.common_map_req.params.transparent = True resp = self.app.get(self.common_map_req) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) eq_(img.mode, 'RGBA') eq_(img.getcolors()[0][0], 200*200) eq_(img.getcolors()[0][1][3], 0) # transparent
def bgcolor_ratio(img_data): """ Return the ratio of the primary/bg color. 1 == only bg color. """ data = BytesIO(img_data) img = Image.open(data) total_colors = img.size[0] * img.size[1] colors = img.getcolors() colors.sort() bgcolor = colors[-1][0] return bgcolor/total_colors
def test_get_map_transparent(self): req = (r'/service?LAYERs=mapnik_transparent&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=200&SRS=EPSG%3A4326' '&VERSION=1.1.1&BBOX=-90,-90,0,0&styles=' '&WIDTH=200&transparent=True') resp = self.app.get(req) data = BytesIO(resp.body) img = Image.open(data) colors = sorted(img.getcolors(), reverse=True) eq_(colors[0], (40000, (0, 0, 0, 0)))
def test_get_map_transparent(self): req = (r'/service?LAYERs=mapnik_transparent&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=200&SRS=EPSG%3A4326' '&VERSION=1.1.1&BBOX=-90,-90,0,0&styles=' '&WIDTH=200&transparent=True') resp = self.app.get(req) data = BytesIO(resp.body) img = Image.open(data) colors = sorted(img.getcolors(), reverse=True) eq_(colors[0], (40000, (0, 0, 0, 0)))
def test_02_get_legendgraphic_layer_static_url(self): self.common_lg_req_111.params['layer'] = 'wms_layer_static_url' with tmp_image((256, 256), format='png') as img: img_data = img.read() expected_req1 = ({'path': r'/staticlegend_layer.png'}, {'body': img_data, 'headers': {'content-type': 'image/png'}}) with mock_httpd(('localhost', 42423), [expected_req1]): resp = self.app.get(self.common_lg_req_111) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) assert Image.open(data).size == (256,256)
def test_get_map_outside_coverage(self, app): req = (r"/service?LAYERs=mapnik&SERVICE=WMS&FORMAT=image%2Fpng" "&REQUEST=GetMap&HEIGHT=200&SRS=EPSG%3A4326" "&VERSION=1.1.1&BBOX=-175,-85,-172,-82&styles=" "&WIDTH=200&&BGCOLOR=0x00ff00") resp = app.get(req) data = BytesIO(resp.body) img = Image.open(data) colors = sorted(img.getcolors(), reverse=True) # wms request bg color assert colors[0] == (40000, (0, 255, 0))
def test_get_map_outside_coverage(self): req = (r'/service?LAYERs=mapnik&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=200&SRS=EPSG%3A4326' '&VERSION=1.1.1&BBOX=-175,-85,-172,-82&styles=' '&WIDTH=200&&BGCOLOR=0x00ff00') resp = self.app.get(req) data = BytesIO(resp.body) img = Image.open(data) colors = sorted(img.getcolors(), reverse=True) # wms request bg color eq_(colors[0], (40000, (0, 255, 0)))
def test_get_map(self): req = ( r"/service?LAYERs=mapnik&SERVICE=WMS&FORMAT=image%2Fpng" "&REQUEST=GetMap&HEIGHT=200&SRS=EPSG%3A4326" "&VERSION=1.1.1&BBOX=-90,-90,0,0&styles=" "&WIDTH=200&" ) resp = self.app.get(req) data = BytesIO(resp.body) img = Image.open(data) colors = img.getcolors(1) # map bg color eq_(colors[0], (40000, (255, 0, 0, 255)))
def test_transparent_watermark_tile(self): with tmp_image((256, 256), format='png', color=(0, 0, 0, 0), mode='RGBA') as img: expected_req = ({'path': r'/service?LAYERs=blank&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=256&SRS=EPSG%3A4326&styles=' '&VERSION=1.1.1&BBOX=-180.0,-90.0,0.0,90.0' '&WIDTH=256'}, {'body': img.read(), 'headers': {'content-type': 'image/jpeg'}}) with mock_httpd(('localhost', 42423), [expected_req]): resp = self.app.get('/tms/1.0.0/watermark_transp/EPSG4326/0/0/0.png') eq_(resp.content_type, 'image/png') img = Image.open(BytesIO(resp.body)) colors = img.getcolors() assert len(colors) >= 2 eq_(sorted(colors)[-1][1], (0, 0, 0, 0))
def test_get_map(self): req = (r'/service?LAYERs=mapnik&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=200&SRS=EPSG%3A4326' '&VERSION=1.1.1&BBOX=-90,-90,0,0&styles=' '&WIDTH=200&') resp = self.app.get(req) data = BytesIO(resp.body) img = Image.open(data) colors = sorted(img.getcolors(), reverse=True) # map bg color + black marker assert 39700 < colors[0][0] < 39900, colors[0][0] eq_(colors[0][1], (255, 0, 0, 255)) assert 50 < colors[1][0] < 150, colors[1][0] eq_(colors[1][1], (0, 0, 0, 255))
def test_get_map_hq(self): req = (r'/service?LAYERs=mapnik_hq&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=200&SRS=EPSG%3A4326' '&VERSION=1.1.1&BBOX=-90,-90,0,0&styles=' '&WIDTH=200&') resp = self.app.get(req) data = BytesIO(resp.body) open('/vagrant/out-mapnik-hq.png', 'wb').write(resp.body) img = Image.open(data) colors = sorted(img.getcolors(), reverse=True) # map bg color + black marker (like above, but marker is scaled up) assert 39500 < colors[0][0] < 39600, colors[0][0] eq_(colors[0][1], (255, 0, 0, 255)) assert 250 < colors[1][0] < 350, colors[1][0] eq_(colors[1][1], (0, 0, 0, 255))
def test_get_map_intersection(self): self.created_tiles.append('wms_cache_EPSG4326/03/000/000/004/000/000/002.jpeg') with tmp_image((256, 256), format='jpeg') as img: expected_req = ({'path': r'/service?LAYERs=foo,bar&SERVICE=WMS&FORMAT=image%2Fjpeg' '&REQUEST=GetMap&HEIGHT=91&SRS=EPSG%3A4326&styles=' '&VERSION=1.1.1&BBOX=10,15,30,31' '&WIDTH=114'}, {'body': img.read(), 'headers': {'content-type': 'image/jpeg'}}) with mock_httpd(('localhost', 42423), [expected_req]): self.common_map_req.params.bbox = 0, 0, 40, 40 self.common_map_req.params.transparent = True resp = self.app.get(self.common_map_req) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) eq_(Image.open(data).mode, 'RGBA')
def test_wms_transparent(self): req = WMS111MapRequest( url='/service?', param=dict( service='WMS', version='1.1.1', bbox='-180,0,0,80', width='200', height='200', layers='wms_cache_transparent', srs='EPSG:4326', format='image/png', styles='', request='GetMap', transparent='True' ) ) resp = self.app.get( req, extra_environ={'mapproxy.decorate_img': to_greyscale} ) data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) eq_(img.mode, 'RGBA')
def test_get_legendgraphic_111(self): self.common_lg_req_111.params['scale'] = '5.0' with tmp_image((256, 256), format='png') as img: img_data = img.read() expected_req1 = ({'path': r'/service?LAYER=foo&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetLegendGraphic&SCALE=5.0&' '&VERSION=1.1.1&SLD_VERSION=1.1.0'}, {'body': img_data, 'headers': {'content-type': 'image/png'}}) expected_req2 = ({'path': r'/service?LAYER=bar&SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetLegendGraphic&SCALE=5.0&' '&VERSION=1.1.1&SLD_VERSION=1.1.0'}, {'body': img_data, 'headers': {'content-type': 'image/png'}}) with mock_httpd(('localhost', 42423), [expected_req1, expected_req2]): resp = self.app.get(self.common_lg_req_111) eq_(resp.content_type, 'image/png') data = BytesIO(resp.body) assert is_png(data) assert Image.open(data).size == (256,512)
def as_image(self): """ Returns the image or the loaded image. :rtype: PIL `Image` """ if not self._img: self._make_seekable_buf() log.debug('file(%s) -> image', self._fname or self._buf) try: img = Image.open(self._buf) except Exception: self.close_buffers() raise self._img = img if self.image_opts and self.image_opts.transparent and self._img.mode == 'P': self._img = self._img.convert('RGBA') return self._img
def test_p_merge(self): """ Check merge bands to paletted image """ merger = BandMerger(mode='RGB') merger.add_ops(dst_band=1, src_img=0, src_band=0, factor=0.5) merger.add_ops(dst_band=1, src_img=3, src_band=1, factor=0.5) merger.add_ops(dst_band=0, src_img=2, src_band=1) merger.add_ops(dst_band=2, src_img=1, src_band=2) img_opts = ImageOptions('P', format='image/png', encoding_options={'quantizer': 'mediancut'}) result = merger.merge([self.img0, self.img1, self.img2, self.img3], img_opts) # need to encode to get conversion to P img = Image.open(result.as_buffer()) eq_(img.mode, 'P') img = img.convert('RGB') eq_(img.getpixel((0, 0)), (210, 127, 120))
def test_combined_transp_color(self): # merged to one request because both layers share the same transparent_color common_params = (r'?SERVICE=WMS&FORMAT=image%2Fpng' '&REQUEST=GetMap&HEIGHT=200&SRS=EPSG%3A4326&styles=' '&VERSION=1.1.1&BBOX=9.0,50.0,10.0,51.0' '&WIDTH=200&transparent=True') with tmp_image((200, 200), color=(255, 0, 0), format='png') as img: img = img.read() expected_req = [({'path': '/service_a' + common_params + '&layers=a_iopts_one,a_iopts_two'}, {'body': img, 'headers': {'content-type': 'image/png'}}), ] with mock_httpd(('localhost', 42423), expected_req): self.common_map_req.params.layers = 'layer_image_opts1,layer_image_opts2' self.common_map_req.params.transparent = True resp = self.app.get(self.common_map_req) resp.content_type = 'image/png' data = BytesIO(resp.body) assert is_png(data) img = Image.open(data) eq_(img.getcolors()[0], ((200*200),(255, 0, 0, 0)))
def img_from_buf(buf): data = BytesIO(buf) return Image.open(data)
def test_output_formats_png8(self): img = Image.new('RGBA', (100, 100)) ir = ImageSource(img, image_opts=PNG_FORMAT) img = Image.open(ir.as_buffer(ImageOptions(colors=256, transparent=True, format='image/png'))) assert img.mode == 'P' assert img.getpixel((0, 0)) == 255