]> git.ipfire.org Git - ipfire.org.git/commitdiff
accounts: Refactor avatar generation
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 11 Oct 2018 11:18:51 +0000 (12:18 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 11 Oct 2018 11:18:51 +0000 (12:18 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/accounts.py

index e1401f16edcd42264aee1552659bac4ac2f3413e..b35874c976c680d6cfb8aeca394665aa7dcc0567 100644 (file)
@@ -349,22 +349,19 @@ class Account(Object):
                return self._resize_avatar(avatar, size)
 
        def _resize_avatar(self, image, size):
-               image = io.BytesIO(image)
-               image = PIL.Image.open(image)
-
-               # Resize the image to the desired resolution
-               image.thumbnail((size, size), PIL.Image.ANTIALIAS)
-
-               f = io.BytesIO()
-
-               # 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()
+               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()
 
 
 if __name__ == "__main__":