From 9efb75def5b1729becd455a6c502bf6237c66121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20E=C3=9Fer?= Date: Wed, 4 Jul 2018 11:48:56 +0200 Subject: [PATCH] Allow transparent background color If `background_color` has an alpha component, create output image in mode `RGBA` instead of `RGB` --- staticmap/staticmap.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/staticmap/staticmap.py b/staticmap/staticmap.py index 839a906..a74548d 100644 --- a/staticmap/staticmap.py +++ b/staticmap/staticmap.py @@ -3,7 +3,7 @@ from math import sqrt, log, tan, pi, cos, ceil, floor, atan, sinh import requests -from PIL import Image, ImageDraw +from PIL import Image, ImageDraw, ImageColor from concurrent.futures import ThreadPoolExecutor @@ -274,7 +274,9 @@ def render(self, zoom=None, center=None): self.x_center = _lon_to_x(lon_center, self.zoom) self.y_center = _lat_to_y(lat_center, self.zoom) - image = Image.new('RGB', (self.width, self.height), self.background_color) + rgba = ImageColor.getrgb(self.background_color) + image_mode = 'RGB' if len(rgba) < 4 else 'RGBA' + image = Image.new(image_mode, (self.width, self.height), self.background_color) self._draw_base_layer(image) self._draw_features(image)