Skip to content

Instantly share code, notes, and snippets.

@walkermatt
Last active March 22, 2024 16:48
Show Gist options
  • Save walkermatt/1cae669defce037511ada4e0ed01b3cd to your computer and use it in GitHub Desktop.
Save walkermatt/1cae669defce037511ada4e0ed01b3cd to your computer and use it in GitHub Desktop.
Minimal MapProxy Middleware demonstrating the decorate_img API
""" Minimal MapProxy Middleware demonstrating the decorate_img API
To run:
1. Install MapProxy in a virtual enviroment together with Gunicorn
2. Create a basic MapProxy config and copy this file into the same directory as mapproxy.yaml
2. Activate virtual environment
3. Change to the directory containing this file
4. Run:
gunicorn -k eventlet --workers=1 --log-file=- mapproxy_decorate:application
"""
from PIL import ImageColor, ImageDraw, ImageFont
from mapproxy.image import ImageSource
from mapproxy.wsgiapp import make_wsgi_app
def annotate_img(image, service, layers, environ, query_extent, **kw):
# Get the PIL image and convert to RGBA to ensure we can use black
# for the text
img = image.as_image().convert('RGBA')
text = ['service: %s' % service]
text.append('layers: %s' % ', '.join(layers))
text.append('srs: %s' % query_extent[0])
text.append('bounds:')
for coord in query_extent[1]:
text.append(' %s' % coord)
draw = ImageDraw.Draw(img)
font = ImageFont.load_default()
fill = ImageColor.getrgb('black')
line_y = 10
for line in text:
line_w, line_h = font.getsize(line)
draw.text((10, line_y), line, font=font, fill=fill)
line_y = line_y + line_h
# Return a new ImageSource specifying the updated PIL image and
# the image options from the original ImageSource
return ImageSource(img, image.image_opts)
class RequestInfoFilter(object):
"""
Simple MapProxy decorate_img middleware.
Annotates map images with information about the request.
"""
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
# Add the callback to the WSGI environment
environ['mapproxy.decorate_img'] = annotate_img
return self.app(environ, start_response)
# Make an WSGI application and wrap it in the middleware
application = RequestInfoFilter(make_wsgi_app(r'mapproxy.yaml'))
""" Minimal MapProxy Middleware demonstrating the decorate_img API
To run:
1. Install MapProxy in a virtual enviroment together with Gunicorn
2. Create a basic MapProxy config and copy this file into the same directory as mapproxy.yaml
2. Activate virtual environment
3. Change to the directory containing this file
4. Run:
gunicorn -k eventlet --workers=1 --log-file=- mapproxy_greyscale:app
"""
from PIL import ImageEnhance
from mapproxy.image import ImageSource
from mapproxy.wsgiapp import make_wsgi_app
def img_to_greyscale(image, service, layers, environ, query_extent, **kw):
# Get the PIL image and convert to RGBA to ensure we can use black
# for the text
img = image.as_image().convert('RGBA')
# Convert to greyscale
img = img.convert('LA')
# Lighten the image slightly by reducing the contrast
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(0.6)
# Return a new ImageSource specifying the updated PIL image and
# the image options from the original ImageSource
return ImageSource(img, image.image_opts)
class GreyscaleFilter(object):
"""
Simple MapProxy decorate_img middleware.
Converts the image to greyscale
"""
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
# Add the callback to the WSGI environment
environ['mapproxy.decorate_img'] = img_to_greyscale
return self.app(environ, start_response)
# Make an WSGI application and wrap it in the middleware
app = GreyscaleFilter(make_wsgi_app(r'mapproxy.yaml'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment