Exemplo n.º 1
0
    def test_composite_merge(self):
        # http://stackoverflow.com/questions/3374878

        img1 = Image.new("RGBA", size=(100, 100), color=(255, 0, 0, 255))
        draw = ImageDraw.Draw(img1)
        draw.rectangle((33, 0, 66, 100), fill=(255, 0, 0, 128))
        draw.rectangle((67, 0, 100, 100), fill=(255, 0, 0, 0))
        img1 = ImageSource(img1)
        img2 = Image.new("RGBA", size=(100, 100), color=(0, 255, 0, 255))
        draw = ImageDraw.Draw(img2)
        draw.rectangle((0, 33, 100, 66), fill=(0, 255, 0, 128))
        draw.rectangle((0, 67, 100, 100), fill=(0, 255, 0, 0))
        img2 = ImageSource(img2)

        result = merge_images([img2, img1], ImageOptions(transparent=True))
        img = result.as_image()
        assert img.mode == "RGBA"
        assert_img_colors_eq(
            img,
            [
                (1089, (0, 255, 0, 255)),
                (1089, (255, 255, 255, 0)),
                (1122, (0, 255, 0, 128)),
                (1122, (128, 126, 0, 255)),
                (1122, (255, 0, 0, 128)),
                (1156, (170, 84, 0, 191)),
                (3300, (255, 0, 0, 255)),
            ],
        )
Exemplo n.º 2
0
    def test_solid_merge(self):
        img1 = ImageSource(Image.new('RGB', (10, 10), (255, 0, 255)))
        img2 = ImageSource(Image.new('RGB', (10, 10), (0, 255, 255)))

        result = merge_images([img1, img2], ImageOptions(transparent=False))
        img = result.as_image()
        eq_(img.getpixel((0, 0)), (0, 255, 255))
Exemplo n.º 3
0
    def test_paletted_merge(self):
        if not hasattr(Image, 'FASTOCTREE'):
            raise SkipTest()

        # generate RGBA images with a transparent rectangle in the lower right
        img1 = ImageSource(Image.new('RGBA', (50, 50),
                                     (0, 255, 0, 255))).as_image()
        draw = ImageDraw.Draw(img1)
        draw.rectangle((25, 25, 49, 49), fill=(0, 0, 0, 0))
        paletted_img = quantize(img1, alpha=True)
        assert img_has_transparency(paletted_img)
        assert paletted_img.mode == 'P'

        rgba_img = Image.new('RGBA', (50, 50), (255, 0, 0, 255))
        draw = ImageDraw.Draw(rgba_img)
        draw.rectangle((25, 25, 49, 49), fill=(0, 0, 0, 0))

        img1 = ImageSource(paletted_img)
        img2 = ImageSource(rgba_img)

        # generate base image and merge the others above
        img3 = ImageSource(Image.new('RGBA', (50, 50), (0, 0, 255, 255)))
        result = merge_images([img3, img1, img2],
                              ImageOptions(transparent=True))
        img = result.as_image()

        assert img.mode == 'RGBA'
        eq_(img.getpixel((49, 49)), (0, 0, 255, 255))
        eq_(img.getpixel((0, 0)), (255, 0, 0, 255))
Exemplo n.º 4
0
    def test_composite_merge(self):
        # http://stackoverflow.com/questions/3374878

        if not hasattr(Image, 'alpha_composite'):
            raise SkipTest()

        img1 = Image.new('RGBA', size=(100, 100), color=(255, 0, 0, 255))
        draw = ImageDraw.Draw(img1)
        draw.rectangle((33, 0, 66, 100), fill=(255, 0, 0, 128))
        draw.rectangle((67, 0, 100, 100), fill=(255, 0, 0, 0))
        img1 = ImageSource(img1)
        img2 = Image.new('RGBA', size =(100, 100), color=(0, 255, 0, 255))
        draw = ImageDraw.Draw(img2)
        draw.rectangle((0, 33, 100, 66), fill=(0, 255, 0, 128))
        draw.rectangle((0, 67, 100, 100), fill=(0, 255, 0, 0))
        img2 = ImageSource(img2)

        result = merge_images([img2, img1], ImageOptions(transparent=True))
        img = result.as_image()
        eq_(img.mode, 'RGBA')
        assert_img_colors_eq(img, [
            (1089, (0, 255, 0, 255)),
            (1089, (255, 255, 255, 0)),
            (1122, (0, 255, 0, 128)),
            (1122, (128, 126, 0, 255)),
            (1122, (255, 0, 0, 128)),
            (1156, (170, 84, 0, 191)),
            (3300, (255, 0, 0, 255))])
Exemplo n.º 5
0
    def test_paletted_merge(self):
        if not hasattr(Image, 'FASTOCTREE'):
            raise SkipTest()

        # generate RGBA images with a transparent rectangle in the lower right
        img1 = ImageSource(Image.new('RGBA', (50, 50), (0, 255, 0, 255))).as_image()
        draw = ImageDraw.Draw(img1)
        draw.rectangle((25, 25, 49, 49), fill=(0, 0, 0, 0))
        paletted_img = quantize(img1, alpha=True)
        assert img_has_transparency(paletted_img)
        assert paletted_img.mode == 'P'

        rgba_img = Image.new('RGBA', (50, 50), (255, 0, 0, 255))
        draw = ImageDraw.Draw(rgba_img)
        draw.rectangle((25, 25, 49, 49), fill=(0, 0, 0, 0))

        img1 = ImageSource(paletted_img)
        img2 = ImageSource(rgba_img)

        # generate base image and merge the others above
        img3 = ImageSource(Image.new('RGBA', (50, 50), (0, 0, 255, 255)))
        result = merge_images([img3, img1, img2], ImageOptions(transparent=True))
        img = result.as_image()

        assert img.mode == 'RGBA'
        eq_(img.getpixel((49, 49)), (0, 0, 255, 255))
        eq_(img.getpixel((0, 0)), (255, 0, 0, 255))
Exemplo n.º 6
0
    def test_solid_merge(self):
        img1 = ImageSource(Image.new("RGB", (10, 10), (255, 0, 255)))
        img2 = ImageSource(Image.new("RGB", (10, 10), (0, 255, 255)))

        result = merge_images([img1, img2], ImageOptions(transparent=False))
        img = result.as_image()
        assert img.getpixel((0, 0)) == (0, 255, 255)
Exemplo n.º 7
0
    def test_merge_L(self):
        img1 = ImageSource(Image.new("RGBA", (10, 10), (255, 0, 255, 255)))
        img2 = ImageSource(Image.new("L", (10, 10), 100))

        # img2 overlays img1
        result = merge_images([img1, img2], ImageOptions(transparent=True))
        img = result.as_image()
        assert_img_colors_eq(img, [(10 * 10, (100, 100, 100, 255))])
Exemplo n.º 8
0
    def test_opacity_merge(self):
        img1 = ImageSource(Image.new('RGB', (10, 10), (255, 0, 255)))
        img2 = ImageSource(Image.new('RGB', (10, 10), (0, 255, 255)),
                           image_opts=ImageOptions(opacity=0.5))

        result = merge_images([img1, img2], ImageOptions(transparent=False))
        img = result.as_image()
        eq_(img.getpixel((0, 0)), (127, 127, 255))
Exemplo n.º 9
0
    def test_merge_rgb_with_transp(self):
        img1 = ImageSource(Image.new("RGB", (10, 10), (255, 0, 255)))
        raw = Image.new("RGB", (10, 10), (0, 255, 255))
        raw.info = {"transparency": (0, 255, 255)}  # make full transparent
        img2 = ImageSource(raw)

        result = merge_images([img1, img2], ImageOptions(transparent=False))
        img = result.as_image()
        assert img.getpixel((0, 0)) == (255, 0, 255)
Exemplo n.º 10
0
    def test_merge_rgb_with_transp(self):
        img1 = ImageSource(Image.new('RGB', (10, 10), (255, 0, 255)))
        raw = Image.new('RGB', (10, 10), (0, 255, 255))
        raw.info = {'transparency': (0, 255, 255)} # make full transparent
        img2 = ImageSource(raw)

        result = merge_images([img1, img2], ImageOptions(transparent=False))
        img = result.as_image()
        eq_(img.getpixel((0, 0)), (255, 0, 255))
Exemplo n.º 11
0
def create_debug_img(size, transparent=True):
    if transparent:
        img = Image.new("RGBA", size)
    else:
        img = Image.new("RGB", size, ImageColor.getrgb("#EEE"))

    draw = ImageDraw.Draw(img)
    draw_pattern(draw, size)
    return img
Exemplo n.º 12
0
def create_debug_img(size, transparent=True):
    if transparent:
        img = Image.new("RGBA", size)
    else:
        img = Image.new("RGB", size, ImageColor.getrgb("#EEE"))

    draw = ImageDraw.Draw(img)
    draw_pattern(draw, size)
    return img
Exemplo n.º 13
0
    def test_merge_L(self):
        img1 = ImageSource(Image.new('RGBA', (10, 10), (255, 0, 255, 255)))
        img2 = ImageSource(Image.new('L', (10, 10), 100))

        # img2 overlays img1
        result = merge_images([img1, img2], ImageOptions(transparent=True))
        img = result.as_image()
        assert_img_colors_eq(img, [
            (10*10, (100, 100, 100, 255)),
        ])
Exemplo n.º 14
0
    def test_opacity_merge_mixed_modes(self):
        img1 = ImageSource(Image.new('RGBA', (10, 10), (255, 0, 255, 255)))
        img2 = ImageSource(Image.new('RGB', (10, 10), (0, 255, 255)).convert('P'),
            image_opts=ImageOptions(opacity=0.5))

        result = merge_images([img1, img2], ImageOptions(transparent=True))
        img = result.as_image()
        assert_img_colors_eq(img, [
            (10*10, (127, 127, 255, 255)),
        ])
Exemplo n.º 15
0
    def test_opacity_merge_mixed_modes(self):
        img1 = ImageSource(Image.new("RGBA", (10, 10), (255, 0, 255, 255)))
        img2 = ImageSource(
            Image.new("RGB", (10, 10), (0, 255, 255)).convert("P"),
            image_opts=ImageOptions(opacity=0.5),
        )

        result = merge_images([img1, img2], ImageOptions(transparent=True))
        img = result.as_image()
        assert_img_colors_eq(img, [(10 * 10, (127, 127, 255, 255))])
Exemplo n.º 16
0
    def test_composite_merge_opacity(self):
        bg = Image.new("RGBA", size=(100, 100), color=(255, 0, 255, 255))
        bg = ImageSource(bg)
        fg = Image.new("RGBA", size=(100, 100), color=(0, 0, 0, 0))
        draw = ImageDraw.Draw(fg)
        draw.rectangle((10, 10, 89, 89), fill=(0, 255, 255, 255))
        fg = ImageSource(fg, image_opts=ImageOptions(opacity=0.5))

        result = merge_images([bg, fg], ImageOptions(transparent=True))
        img = result.as_image()
        assert img.mode == "RGBA"
        assert_img_colors_eq(img, [(3600, (255, 0, 255, 255)),
                                   (6400, (128, 127, 255, 255))])
Exemplo n.º 17
0
def create_image(size, color=None, mode=None):
    if color is not None:
        if isinstance(color, string_type):
            if mode is None:
                mode = 'RGB'
            img = Image.new(mode, size, color=color)
        else:
            if mode is None:
                mode = 'RGBA' if len(color) == 4 else 'RGB'
            img = Image.new(mode, size, color=tuple(color))
    else:
        img = create_debug_img(size)
    return img
Exemplo n.º 18
0
def create_image(size, color=None, mode=None):
    if color is not None:
        if isinstance(color, string_type):
            if mode is None:
                mode = 'RGB'
            img = Image.new(mode, size, color=color)
        else:
            if mode is None:
                mode = 'RGBA' if len(color) == 4 else 'RGB'
            img = Image.new(mode, size, color=tuple(color))
    else:
        img = create_debug_img(size)
    return img
Exemplo n.º 19
0
def concat_legends(legends,
                   format='png',
                   size=None,
                   bgcolor='#ffffff',
                   transparent=True):
    """
    Merge multiple legends into one
    :param images: list of `ImageSource`, bottom image first
    :param format: the format of the output `ImageSource`
    :param size: size of the merged image, if ``None`` the size
                 will be calculated
    :rtype: `ImageSource`
    """
    if not legends:
        return BlankImageSource(size=(1, 1),
                                image_opts=ImageOptions(
                                    bgcolor=bgcolor, transparent=transparent))
    if len(legends) == 1:
        return legends[0]

    legends = legends[:]
    legends.reverse()
    if size is None:
        legend_width = 0
        legend_height = 0
        legend_position_y = []
        #iterate through all legends, last to first, calc img size and remember the y-position
        for legend in legends:
            legend_position_y.append(legend_height)
            tmp_img = legend.as_image()
            legend_width = max(legend_width, tmp_img.size[0])
            legend_height += tmp_img.size[
                1]  #images shall not overlap themselfs

        size = [legend_width, legend_height]
    bgcolor = ImageColor.getrgb(bgcolor)

    if transparent:
        img = Image.new('RGBA', size, bgcolor + (0, ))
    else:
        img = Image.new('RGB', size, bgcolor)
    for i in range(len(legends)):
        legend_img = legends[i].as_image()
        if legend_img.mode == 'RGBA':
            # paste w transparency mask from layer
            img.paste(legend_img, (0, legend_position_y[i]), legend_img)
        else:
            img.paste(legend_img, (0, legend_position_y[i]))
    return ImageSource(img, image_opts=ImageOptions(format=format))
Exemplo n.º 20
0
 def test_concatenation(self):
     legends = []
     img_1 = Image.new(mode='RGBA', size=(30,10), color="red")
     img_2 = Image.new(mode='RGBA', size=(10,10), color="black")
     img_3 = Image.new(mode='RGBA', size=(50,80), color="blue")
     legends.append(ImageSource(img_1))
     legends.append(ImageSource(img_2))
     legends.append(ImageSource(img_3))
     source = concat_legends(legends)
     src_img = source.as_image()
     assert src_img.getpixel((0,90)) == (255,0,0,255)
     assert src_img.getpixel((0,80)) == (0,0,0,255)
     assert src_img.getpixel((0,0)) == (0,0,255,255)
     assert src_img.getpixel((49,99)) == (255,255,255,0)
     assert is_png(source.as_buffer())
Exemplo n.º 21
0
    def test_rbga(self):
        img = Image.new("RGBA", (10, 10), (100, 200, 50, 255))
        img.paste((255, 50, 50, 0), (3, 3, 7, 7))
        assert img_has_transparency(img)

        img = quantize(img, alpha=True)
        assert img_has_transparency(img)
Exemplo n.º 22
0
def create_image(size, image_opts=None):
    """
    Create a new image that is compatible with the given `image_opts`.
    Takes into account mode, transparent, bgcolor.
    """
    from mapproxy.compat.image import Image, ImageColor

    if image_opts is None:
        mode = 'RGB'
        bgcolor = (255, 255, 255)
    else:
        mode = image_opts.mode
        if mode in (None, 'P'):
            if image_opts.transparent:
                mode = 'RGBA'
            else:
                mode = 'RGB'

        bgcolor = image_opts.bgcolor or (255, 255, 255)

        if isinstance(bgcolor, string_type):
            bgcolor = ImageColor.getrgb(bgcolor)

        if image_opts.transparent and len(bgcolor) == 3:
            bgcolor = bgcolor + (0, )

        if image_opts.mode == 'I':
            bgcolor = bgcolor[0]

    return Image.new(mode, size, bgcolor)
Exemplo n.º 23
0
    def test_one_point(self):
        img = Image.new("RGB", (100, 100), color="#ff0000")
        draw = ImageDraw.Draw(img)
        draw.point((99, 99))
        del draw

        assert not is_single_color_image(img)
Exemplo n.º 24
0
 def test_mask_outside_of_image_transparent(self):
     img = ImageSource(Image.new('RGB', (100, 100), color=(100, 0, 200)),
                       image_opts=ImageOptions(transparent=True))
     result = mask_image_source_from_coverage(img, [0, 0, 10, 10],
                                              SRS(4326),
                                              coverage([20, 20, 30, 30]))
     eq_(result.as_image().getcolors(), [((100 * 100), (255, 255, 255, 0))])
Exemplo n.º 25
0
    def test_composite_merge_opacity(self):
        if not hasattr(Image, 'alpha_composite'):
            raise SkipTest()

        bg = Image.new('RGBA', size=(100, 100), color=(255, 0, 255, 255))
        bg = ImageSource(bg)
        fg = Image.new('RGBA', size=(100, 100), color=(0, 0, 0, 0))
        draw = ImageDraw.Draw(fg)
        draw.rectangle((10, 10, 89, 89), fill=(0, 255, 255, 255))
        fg = ImageSource(fg, image_opts=ImageOptions(opacity=0.5))

        result = merge_images([bg, fg], ImageOptions(transparent=True))
        img = result.as_image()
        eq_(img.mode, 'RGBA')
        assert_img_colors_eq(img, [(3600, (255, 0, 255, 255)),
                                   (6400, (128, 127, 255, 255))])
Exemplo n.º 26
0
def create_image(size, image_opts=None):
    """
    Create a new image that is compatible with the given `image_opts`.
    Takes into account mode, transparent, bgcolor.
    """
    from mapproxy.compat.image import Image, ImageColor

    if image_opts is None:
        mode = 'RGB'
        bgcolor = (255, 255, 255)
    else:
        mode = image_opts.mode
        if mode in (None, 'P'):
            if image_opts.transparent:
                mode = 'RGBA'
            else:
                mode = 'RGB'

        bgcolor = image_opts.bgcolor or (255, 255, 255)

        if isinstance(bgcolor, string_type):
            bgcolor = ImageColor.getrgb(bgcolor)

        if image_opts.transparent and len(bgcolor) == 3:
            bgcolor = bgcolor + (0, )

        if image_opts.mode == 'I':
            bgcolor = bgcolor[0]

    return Image.new(mode, size, bgcolor)
Exemplo n.º 27
0
 def test_solid_paletted_image(self):
     img = Image.new("P", (100, 100), color=20)
     palette = []
     for i in range(256):
         palette.extend((i, i // 2, i % 3))
     img.putpalette(palette)
     assert is_single_color_image(img) == (20, 10, 2)
Exemplo n.º 28
0
    def test_mask_partial_image_transparent(self):
        img = ImageSource(Image.new('RGB', (100, 100), color=(100, 0, 200)),
            image_opts=ImageOptions(transparent=True))

        result = mask_image_source_from_coverage(img, [0, 0, 10, 10], SRS(4326), coverage([5, 5, 30, 30]))
        assert_img_colors_eq(result.as_image().getcolors(),
            [(7500, (255, 255, 255, 0)), (2500, (100, 0, 200, 255))])
Exemplo n.º 29
0
    def test_one_point(self):
        img = Image.new('RGB', (100, 100), color='#ff0000')
        draw = ImageDraw.Draw(img)
        draw.point((99, 99))
        del draw

        assert not is_single_color_image(img)
Exemplo n.º 30
0
 def test_solid_paletted_image(self):
     img = Image.new('P', (100, 100), color=20)
     palette = []
     for i in range(256):
         palette.extend((i, i//2, i%3))
     img.putpalette(palette)
     eq_(is_single_color_image(img), (20, 10, 2))
Exemplo n.º 31
0
    def test_mask_partial_image_transparent(self):
        img = ImageSource(Image.new('RGB', (100, 100), color=(100, 0, 200)),
            image_opts=ImageOptions(transparent=True))

        result = mask_image_source_from_coverage(img, [0, 0, 10, 10], SRS(4326), coverage([5, 5, 30, 30]))
        assert_img_colors_eq(result.as_image().getcolors(),
            [(7500, (255, 255, 255, 0)), (2500, (100, 0, 200, 255))])
Exemplo n.º 32
0
def image_mask_from_geom(size, bbox, polygons):
    mask = Image.new('L', size, 255)
    if len(polygons) == 0:
        return mask

    transf = make_lin_transf(bbox, (0, 0) + size)

    # use negative ~.1 pixel buffer
    buffer = -0.1 * min((bbox[2] - bbox[0]) / size[0],
                        (bbox[3] - bbox[1]) / size[1])

    draw = ImageDraw.Draw(mask)

    def draw_polygon(p):
        draw.polygon([transf(coord) for coord in p.exterior.coords], fill=0)
        for ring in p.interiors:
            draw.polygon([transf(coord) for coord in ring.coords], fill=255)

    for p in polygons:
        # little bit smaller polygon does not include touched pixels outside coverage
        buffered = p.buffer(buffer, resolution=1, join_style=2)

        if buffered.is_empty:  # can be empty after negative buffer
            continue

        if buffered.type == 'MultiPolygon':
            # negative buffer can turn polygon into multipolygon
            for p in buffered:
                draw_polygon(p)
        else:
            draw_polygon(buffered)

    return mask
Exemplo n.º 33
0
def image_mask_from_geom(size, bbox, polygons):
    mask = Image.new('L', size, 255)
    if len(polygons) == 0:
        return mask

    transf = make_lin_transf(bbox, (0, 0) + size)

    # use negative ~.1 pixel buffer
    buffer = -0.1 * min((bbox[2] - bbox[0]) / size[0], (bbox[3] - bbox[1]) / size[1])

    draw = ImageDraw.Draw(mask)

    def draw_polygon(p):
        draw.polygon([transf(coord) for coord in p.exterior.coords], fill=0)
        for ring in p.interiors:
            draw.polygon([transf(coord) for coord in ring.coords], fill=255)

    for p in polygons:
        # little bit smaller polygon does not include touched pixels outside coverage
        buffered = p.buffer(buffer, resolution=1, join_style=2)

        if buffered.is_empty: # can be empty after negative buffer
            continue

        if buffered.type == 'MultiPolygon':
            # negative buffer can turn polygon into multipolygon
            for p in buffered:
                draw_polygon(p)
        else:
            draw_polygon(buffered)

    return mask
Exemplo n.º 34
0
    def test_mask_partial_image_bgcolor(self):
        img = ImageSource(Image.new('RGB', (100, 100), color=(100, 0, 200)),
            image_opts=ImageOptions(bgcolor=(200, 30, 120)))

        result = mask_image_source_from_coverage(img, [0, 0, 10, 10], SRS(4326), coverage([5, 5, 30, 30]))
        eq_(result.as_image().getcolors(),
            [(7500, (200, 30, 120)), (2500, (100, 0, 200))])
Exemplo n.º 35
0
 def test_multiline_ul(self):
     font = ImageFont.load_default()
     td = TextDraw('Hello\nWorld', font)
     img = Image.new('RGB', (100, 100))
     draw = ImageDraw.Draw(img)
     total_box, boxes = td.text_boxes(draw, (100, 100))
     eq_(total_box, (5, 5, 35, 30))
     eq_(boxes, [(5, 5, 35, 16), (5, 19, 35, 30)])
Exemplo n.º 36
0
 def test_merge_single_coverage(self):
     merger = LayerMerger()
     merger.add(ImageSource(Image.new('RGB', (10, 10), (255, 255, 255))), self.coverage1)
     result = merger.merge(image_opts=ImageOptions(transparent=True), bbox=(5, 0, 15, 10), bbox_srs=3857)
     img = result.as_image()
     eq_(img.mode, 'RGBA')
     eq_(img.getpixel((4, 0)), (255, 255, 255, 255))
     eq_(img.getpixel((6, 0)), (255, 255, 255, 0))
Exemplo n.º 37
0
 def test_ul(self):
     font = ImageFont.load_default()
     td = TextDraw('Hello', font)
     img = Image.new('RGB', (100, 100))
     draw = ImageDraw.Draw(img)
     total_box, boxes = td.text_boxes(draw, (100, 100))
     eq_(total_box, boxes[0])
     eq_(len(boxes), 1)
Exemplo n.º 38
0
 def test_multiline_ul(self):
     font = ImageFont.load_default()
     td = TextDraw('Hello\nWorld', font)
     img = Image.new('RGB', (100, 100))
     draw = ImageDraw.Draw(img)
     total_box, boxes = td.text_boxes(draw, (100, 100))
     eq_(total_box, (5, 5, 35, 30))
     eq_(boxes, [(5, 5, 35, 16), (5, 19, 35, 30)])
Exemplo n.º 39
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)
Exemplo n.º 40
0
 def check_placement(self, x, y):
     font = ImageFont.load_default()
     td = TextDraw('Hello\nWorld\n%s %s' % (x, y), font, placement=x+y,
         padding=5, linespacing=2)
     img = Image.new('RGB', (100, 100))
     draw = ImageDraw.Draw(img)
     td.draw(draw, img.size)
     img.show()
Exemplo n.º 41
0
    def test_composite_merge_opacity(self):
        if not hasattr(Image, 'alpha_composite'):
            raise SkipTest()

        bg = Image.new('RGBA', size=(100, 100), color=(255, 0, 255, 255))
        bg = ImageSource(bg)
        fg = Image.new('RGBA', size =(100, 100), color=(0, 0, 0, 0))
        draw = ImageDraw.Draw(fg)
        draw.rectangle((10, 10, 89, 89), fill=(0, 255, 255, 255))
        fg = ImageSource(fg, image_opts=ImageOptions(opacity=0.5))

        result = merge_images([bg, fg], ImageOptions(transparent=True))
        img = result.as_image()
        eq_(img.mode, 'RGBA')
        assert_img_colors_eq(img, [
            (3600, (255, 0, 255, 255)),
            (6400, (128, 127, 255, 255))])
Exemplo n.º 42
0
 def test_ul(self):
     font = ImageFont.load_default()
     td = TextDraw('Hello', font)
     img = Image.new('RGB', (100, 100))
     draw = ImageDraw.Draw(img)
     total_box, boxes = td.text_boxes(draw, (100, 100))
     eq_(total_box, boxes[0])
     eq_(len(boxes), 1)
Exemplo n.º 43
0
 def test_multiline_lr(self):
     font = ImageFont.load_default()
     td = TextDraw('Hello\nWorld', font, placement='lr')
     img = Image.new('RGB', (100, 100))
     draw = ImageDraw.Draw(img)
     total_box, boxes = td.text_boxes(draw, (100, 100))
     eq_(total_box, (65, 70, 95, 95))
     eq_(boxes, [(65, 70, 95, 81), (65, 84, 95, 95)])
Exemplo n.º 44
0
 def test_multiline_center(self):
     font = ImageFont.load_default()
     td = TextDraw('Hello\nWorld', font, placement='cc')
     img = Image.new('RGB', (100, 100))
     draw = ImageDraw.Draw(img)
     total_box, boxes = td.text_boxes(draw, (100, 100))
     eq_(total_box, (35, 38, 65, 63))
     eq_(boxes, [(35, 38, 65, 49), (35, 52, 65, 63)])
Exemplo n.º 45
0
 def test_returns_imagesource(self):
     img_src1 = ImageSource(Image.new('RGBA', (100, 100)))
     env = make_wsgi_env('', extra_environ={})
     img_src2 = self.server.decorate_img(
         img_src1, 'wms.map', ['layer1'],
         env, self.query_extent
     )
     assert isinstance(img_src2, ImageSource)
Exemplo n.º 46
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
Exemplo n.º 47
0
 def test_multiline_center(self):
     font = ImageFont.load_default()
     td = TextDraw('Hello\nWorld', font, placement='cc')
     img = Image.new('RGB', (100, 100))
     draw = ImageDraw.Draw(img)
     total_box, boxes = td.text_boxes(draw, (100, 100))
     eq_(total_box, (35, 38, 65, 63))
     eq_(boxes, [(35, 38, 65, 49), (35, 52, 65, 63)])
Exemplo n.º 48
0
 def test_multiline_lr(self):
     font = ImageFont.load_default()
     td = TextDraw('Hello\nWorld', font, placement='lr')
     img = Image.new('RGB', (100, 100))
     draw = ImageDraw.Draw(img)
     total_box, boxes = td.text_boxes(draw, (100, 100))
     eq_(total_box, (65, 70, 95, 95))
     eq_(boxes, [(65, 70, 95, 81), (65, 84, 95, 95)])
Exemplo n.º 49
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)
Exemplo n.º 50
0
 def test_original_imagesource_returned_when_no_callback(self):
     img_src1 = ImageSource(Image.new('RGBA', (100, 100)))
     env = make_wsgi_env('', extra_environ={})
     img_src2 = self.server.decorate_img(
         img_src1, 'wms.map', ['layer1'],
         env, self.query_extent
     )
     eq_(img_src1, img_src2)
Exemplo n.º 51
0
    def merge(self, sources, image_opts, size=None, bbox=None, bbox_srs=None, coverage=None):
        if not sources:
            return BlankImageSource(size=size, image_opts=image_opts, cacheable=True)

        if size is None:
            size = sources[0].size

        # load src bands
        src_img_bands = []
        for i, layer_img in enumerate(sources):
            img = layer_img.as_image()

            if i not in self.max_band:
                # do not split img if not requested by any op
                src_img_bands.append(None)
                continue

            if self.max_band[i] == 3 and img.mode != 'RGBA':
                # convert to RGBA if band idx 3 is requestd (e.g. P or RGB src)
                img = img.convert('RGBA')
            elif img.mode == 'P':
                img = img.convert('RGB')
            src_img_bands.append(img.split())

        tmp_mode = self.mode

        if tmp_mode == 'RGBA':
            result_bands = [None, None, None, None]
        elif tmp_mode == 'RGB':
            result_bands = [None, None, None]
        elif tmp_mode == 'L':
            result_bands = [None]
        else:
            raise ValueError("unsupported destination mode %s", image_opts.mode)

        for op in self.ops:
            chan = src_img_bands[op.src_img][op.src_band]
            if op.factor != 1.0:
                chan = ImageMath.eval("convert(int(float(a) * %f), 'L')" % op.factor, a=chan)
                if result_bands[op.dst_band] is None:
                    result_bands[op.dst_band] = chan
                else:
                    result_bands[op.dst_band] = ImageChops.add(
                        result_bands[op.dst_band],
                        chan,
                    )
            else:
                result_bands[op.dst_band] = chan

        for i, b in enumerate(result_bands):
            if b is None:
                # band not set
                b = Image.new("L", size, 255 if i == 3 else 0)
                result_bands[i] = b

        result = Image.merge(tmp_mode, result_bands)
        return ImageSource(result, size=size, image_opts=image_opts)
Exemplo n.º 52
0
 def test_calls_callback(self):
     img_src1 = ImageSource(Image.new('RGBA', (100, 100)))
     self.called = False
     env = make_wsgi_env(
         '',
         extra_environ={'mapproxy.decorate_img': self.set_called_callback})
     img_src2 = self.server.decorate_img(img_src1, 'wms.map', ['layer1'],
                                         env, self.query_extent)
     assert self.called == True
Exemplo n.º 53
0
    def test_mask_outside_of_image_bgcolor(self):
        img = ImageSource(Image.new('RGB', (100, 100), color=(100, 0, 200)),
                          image_opts=ImageOptions(bgcolor=(200, 30, 120)))

        result = mask_image_source_from_coverage(img, [0, 0, 10, 10],
                                                 SRS(4326),
                                                 coverage([20, 20, 30, 30]))
        assert_img_colors_eq(result.as_image().getcolors(),
                             [((100 * 100), (200, 30, 120))])
Exemplo n.º 54
0
    def test_merge_overlapping_coverage(self):
        color1 = (255, 255, 0)
        color2 = (0, 255, 255)
        merger = LayerMerger()
        merger.add(ImageSource(Image.new('RGB', (10, 10), color1)), self.coverage1)
        merger.add(ImageSource(Image.new('RGB', (10, 10), color2)), self.coverage2)

        result = merger.merge(image_opts=ImageOptions(), bbox=(0, 0, 10, 10), bbox_srs=3857)
        img = result.as_image()
        eq_(img.mode, 'RGB')

        expected = create_image((10, 10), color1, 'RGB')
        draw = ImageDraw.Draw(expected)
        draw.polygon([(2, 2), (7, 2), (7, 7), (2, 7)], fill=color2)

        for x in range(0, 9):
            for y in range(0, 9):
                eq_(img.getpixel((x, y)), expected.getpixel((x, y)))
Exemplo n.º 55
0
def tmp_image(size, format='png', color=None, mode='RGB'):
    if color is not None:
        img = Image.new(mode, size, color=color)
    else:
        img = create_debug_img(size)
    data = BytesIO()
    img.save(data, format)
    data.seek(0)
    yield data
Exemplo n.º 56
0
 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
Exemplo n.º 57
0
 def test_transparent(self):
     font = ImageFont.load_default()
     td = TextDraw('Hello\nWorld', font, placement='cc')
     img = Image.new('RGBA', (100, 100), (0, 0, 0, 0))
     draw = ImageDraw.Draw(img)
     td.draw(draw, img.size)
     eq_(len(img.getcolors()), 2)
     # top color (bg) is transparent
     eq_(sorted(img.getcolors())[1][1], (0, 0, 0, 0))
Exemplo n.º 58
0
    def test_rgb(self):
        if not hasattr(Image, 'FASTOCTREE'):
            raise SkipTest()

        img = Image.new('RGB', (10, 10))
        assert not img_has_transparency(img)

        img = quantize(img, alpha=False)
        assert not img_has_transparency(img)