def test_background_image_and_background_color(): m = mapnik.Map(8, 8) m.background = mapnik.Color('rgba(255,255,255,.5)') m.background_image = '../data/images/stripes_pattern.png' im = mapnik.Image(m.width, m.height) mapnik.render(m, im) eq_(get_unique_colors(im), ['rgba(255,255,255,128)', 'rgba(74,74,74,255)'])
def test_background_image_with_alpha_and_background_color(): m = mapnik.Map(10,10) m.background = mapnik.Color('rgba(255,255,255,.5)') m.background_image = '../data/images/yellow_half_trans.png' im = mapnik.Image(m.width,m.height) mapnik.render(m,im) eq_(get_unique_colors(im),['rgba(255,255,85,191)'])
def test_background_image_with_alpha_and_background_color(): m = mapnik.Map(10, 10) m.background = mapnik.Color('rgba(255,255,255,.5)') m.background_image = '../data/images/yellow_half_trans.png' im = mapnik.Image(m.width, m.height) mapnik.render(m, im) eq_(get_unique_colors(im), ['rgba(255,255,85,191)'])
def test_background_image_and_background_color(): m = mapnik.Map(8,8) m.background = mapnik.Color('rgba(255,255,255,.5)') m.background_image = '../data/images/stripes_pattern.png' im = mapnik.Image(m.width,m.height) mapnik.render(m,im) eq_(get_unique_colors(im),['rgba(255,255,255,128)', 'rgba(74,74,74,255)'])
def test_raster_with_alpha_blends_correctly_with_background(): WIDTH = 500 HEIGHT = 500 map = mapnik.Map(WIDTH, HEIGHT) WHITE = mapnik.Color(255, 255, 255) map.background = WHITE style = mapnik.Style() rule = mapnik.Rule() symbolizer = mapnik.RasterSymbolizer() symbolizer.scaling = mapnik.scaling_method.BILINEAR rule.symbols.append(symbolizer) style.rules.append(rule) map.append_style('raster_style', style) map_layer = mapnik.Layer('test_layer') filepath = '../data/raster/white-alpha.png' if 'gdal' in mapnik.DatasourceCache.plugin_names(): map_layer.datasource = mapnik.Gdal(file=filepath) map_layer.styles.append('raster_style') map.layers.append(map_layer) map.zoom_all() mim = mapnik.Image(WIDTH, HEIGHT) mapnik.render(map, mim) imdata = mim.tostring() # All white is expected eq_(get_unique_colors(mim),['rgba(254,254,254,255)'])
def test_background_image_with_alpha_and_background_color_against_composited_control(): m = mapnik.Map(10,10) m.background = mapnik.Color('rgba(255,255,255,.5)') m.background_image = '../data/images/yellow_half_trans.png' im = mapnik.Image(m.width,m.height) mapnik.render(m,im) # create and composite the expected result im1 = mapnik.Image(10,10) im1.background = mapnik.Color('rgba(255,255,255,.5)') im1.premultiply() im2 = mapnik.Image(10,10) im2.background = mapnik.Color('rgba(255,255,0,.5)') im2.premultiply() im1.composite(im2) im1.demultiply() # compare image rendered (compositing in `agg_renderer<T>::setup`) # vs image composited via python bindings raise Todo("looks like we need to investigate PNG rounding when saving") eq_(get_unique_colors(im),get_unique_colors(im1))
def test_background_image_and_background_color(): m = mapnik.Map(8,8) m.background = mapnik.Color('rgba(255,255,255,.5)') m.background_image = '../data/images/stripes_pattern.png' im = mapnik.Image(m.width,m.height) mapnik.render(m,im) # note: data loss due to rounding as per https://github.com/mapnik/mapnik/issues/1519 # means that background will roundtrip to 253 not 255 #eq_(get_unique_colors(im),['rgba(255,255,255,128)', 'rgba(74,74,74,255)']) eq_(get_unique_colors(im),['rgba(253,253,253,128)', 'rgba(74,74,74,255)'])
def test_background_image_with_alpha_and_background_color_against_composited_control( ): m = mapnik.Map(10, 10) m.background = mapnik.Color('rgba(255,255,255,.5)') m.background_image = '../data/images/yellow_half_trans.png' im = mapnik.Image(m.width, m.height) mapnik.render(m, im) # create and composite the expected result im1 = mapnik.Image(10, 10) im1.background = mapnik.Color('rgba(255,255,255,.5)') im1.premultiply() im2 = mapnik.Image(10, 10) im2.background = mapnik.Color('rgba(255,255,0,.5)') im2.premultiply() im1.composite(im2) im1.demultiply() # compare image rendered (compositing in `agg_renderer<T>::setup`) # vs image composited via python bindings raise Todo("looks like we need to investigate PNG rounding when saving") eq_(get_unique_colors(im), get_unique_colors(im1))
def test_rounding_and_color_expectations(): m = mapnik.Map(1, 1) m.background = mapnik.Color('rgba(255,255,255,.4999999)') im = mapnik.Image(m.width, m.height) mapnik.render(m, im) eq_(get_unique_colors(im), ['rgba(255,255,255,127)']) m = mapnik.Map(1, 1) m.background = mapnik.Color('rgba(255,255,255,.5)') im = mapnik.Image(m.width, m.height) mapnik.render(m, im) eq_(get_unique_colors(im), ['rgba(255,255,255,128)']) im_file = mapnik.Image.open('../data/images/stripes_pattern.png') eq_(get_unique_colors(im_file), ['rgba(0,0,0,0)', 'rgba(74,74,74,255)']) # should have no effect im_file.premultiply() eq_(get_unique_colors(im_file), ['rgba(0,0,0,0)', 'rgba(74,74,74,255)']) im_file.set_alpha(.5) # should have effect now that image has transparency im_file.premultiply() eq_(get_unique_colors(im_file), ['rgba(0,0,0,0)', 'rgba(37,37,37,127)']) # should restore to original nonpremultiplied colors im_file.demultiply() eq_(get_unique_colors(im_file), ['rgba(0,0,0,0)', 'rgba(74,74,74,127)'])
def test_rounding_and_color_expectations(): m = mapnik.Map(1,1) m.background = mapnik.Color('rgba(255,255,255,.4999999)') im = mapnik.Image(m.width,m.height) mapnik.render(m,im) eq_(get_unique_colors(im),['rgba(255,255,255,127)']) m = mapnik.Map(1,1) m.background = mapnik.Color('rgba(255,255,255,.5)') im = mapnik.Image(m.width,m.height) mapnik.render(m,im) eq_(get_unique_colors(im),['rgba(255,255,255,128)']) im_file = mapnik.Image.open('../data/images/stripes_pattern.png') eq_(get_unique_colors(im_file),['rgba(0,0,0,0)', 'rgba(74,74,74,255)']) # should have no effect im_file.premultiply() eq_(get_unique_colors(im_file),['rgba(0,0,0,0)', 'rgba(74,74,74,255)']) im_file.apply_opacity(.5) # should have effect now that image has transparency im_file.premultiply() eq_(get_unique_colors(im_file),['rgba(0,0,0,0)', 'rgba(37,37,37,127)']) # should restore to original nonpremultiplied colors im_file.demultiply() eq_(get_unique_colors(im_file),['rgba(0,0,0,0)', 'rgba(74,74,74,127)'])
def test_set_color_to_alpha(): im = mapnik.Image(256,256) im.fill(mapnik.Color('rgba(12,12,12,255)')) eq_(get_unique_colors(im), ['rgba(12,12,12,255)']) im.set_color_to_alpha(mapnik.Color('rgba(12,12,12,0)')) eq_(get_unique_colors(im), ['rgba(0,0,0,0)'])
def test_set_color_to_alpha(): im = mapnik.Image(256, 256) im.fill(mapnik.Color('rgba(12,12,12,255)')) eq_(get_unique_colors(im), ['rgba(12,12,12,255)']) im.set_color_to_alpha(mapnik.Color('rgba(12,12,12,0)')) eq_(get_unique_colors(im), ['rgba(0,0,0,0)'])