]> git.ipfire.org Git - ipfire.org.git/commitdiff
accounts: Convert images into RGB when they use an alpha channel
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 11 Oct 2018 11:25:45 +0000 (12:25 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 11 Oct 2018 11:25:45 +0000 (12:25 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/accounts.py

index b35874c976c680d6cfb8aeca394665aa7dcc0567..1b06d6628502e1f3d65c61f2fe2ba2435911c9ed 100644 (file)
@@ -349,19 +349,24 @@ class Account(Object):
                return self._resize_avatar(avatar, size)
 
        def _resize_avatar(self, image, size):
-               with PIL.Image.open(io.BytesIO(image)) as image:
-                       # Resize the image to the desired resolution
-                       image.thumbnail((size, size), PIL.Image.ANTIALIAS)
-
-                       with io.BytesIO() as f:
-                               # If writing out the image does not work with optimization,
-                               # we try to write it out without any optimization.
-                               try:
-                                       image.save(f, "JPEG", optimize=True, quality=98)
-                               except:
-                                       image.save(f, "JPEG", quality=98)
-
-                               return f.getvalue()
+               image = PIL.Image.open(io.BytesIO(image))
+
+               # Convert RGBA images into RGB because JPEG doesn't support alpha-channels
+               if image.mode == "RGBA":
+                       image = image.convert("RGB")
+
+               # Resize the image to the desired resolution
+               image.thumbnail((size, size), PIL.Image.ANTIALIAS)
+
+               with io.BytesIO() as f:
+                       # If writing out the image does not work with optimization,
+                       # we try to write it out without any optimization.
+                       try:
+                               image.save(f, "JPEG", optimize=True, quality=98)
+                       except:
+                               image.save(f, "JPEG", quality=98)
+
+                       return f.getvalue()
 
 
 if __name__ == "__main__":