transparency and opacity fix

deduikertjes avatardeduikertjes created an issue

This proposal for enhancement is relative to mapproxy 1.2.0.

Mapproxy does not work well when adding opacity to layers which also have transparent area's. The transparent area's become either semi-transparent black or semi-transparent-white. The problem is described in: http://lists.osgeo.org/pipermail/mapproxy/2012-January/000913.html and http://lists.osgeo.org/pipermail/mapproxy/2012-January/000914.html.

I've created a patch which solves the problem:

mapproxy/image/__init__.py:
    layer_img = layer.as_image()
    if (layer.image_opts and layer.image_opts.opacity is not None
        and layer.image_opts.opacity < 1.0):
-        layer_img = layer_img.convert(img.mode)
-        img = Image.blend(img, layer_img, layer.image_opts.opacity)
+        if layer_img.mode == 'RGBA':
+            alpha = layer_img.split()[3]
+            alpha = ImageEnhance.Brightness(alpha).enhance(layer.image_opts.opacity)
+            layer_img.putalpha(alpha)
+            img.paste(layer_img, (0, 0), layer_img)
+        else:
+            layer_img = layer_img.convert(img.mode)
+            img = Image.blend(img, layer_img, layer.image_opts.opacity)
    else:
        if layer_img.mode == 'RGBA':
            # paste w transparency mask from layer

mapproxy/platform/image.py:
Everywhere I've substituted:
Image, ImageColor, ImageDraw, ImageFont, ImagePalette, ImageChops
by:
Image, ImageColor, ImageDraw, ImageFont, ImagePalette, ImageChops, ImageEnhance 

I propose to add this to mapproxy.

Comments (0)

  1. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.