Ensure the blinged image doesn't protrude outside the mask.
authorCMDR furrycat <elite@furrycat.net>
Wed, 10 Jan 2018 14:53:09 +0000 (14:53 +0000)
committerCMDR furrycat <elite@furrycat.net>
Wed, 10 Jan 2018 14:53:09 +0000 (14:53 +0000)
plugin/bling/bling.py

index 09e2cda..37039d7 100644 (file)
@@ -153,7 +153,7 @@ class Bling(object):
     await bot.say(message.channel, text, attachment = bot.parse_attachment(data, parsed['filename'] + '.png'))
 
   def create_avatar(self, url, bgfile, fgfile):
-    # Get Delta images.
+    # Get bling images.
     bg = Image.open(bgfile)
     fg = Image.open(fgfile)
     if not bg or not fg:
@@ -202,6 +202,14 @@ class Bling(object):
     top = math.floor((padded.height - image.height) / 2)
     padded.paste(image, (left, top, left + image.width, top + image.height))
 
+    # Ensure the user's image doesn't extend outside the foreground.
+    try:
+      r, g, b, a = bg.split()
+      if a is not None:
+        padded.putalpha(a)
+    except ValueError:
+      pass
+
     # Merge them.
     result = Image.alpha_composite(Image.alpha_composite(bg, padded), fg)
     b = io.BytesIO()