Merge pull request #75 from z4r/patch-1

Preserve RGBA image.mode
This commit is contained in:
Grant McConnaughey 2016-02-09 09:17:52 -06:00
commit a6b4c2beea
4 changed files with 15 additions and 1 deletions

View file

@ -116,7 +116,7 @@ class Avatar(models.Model):
else:
diff = int((h - w) / 2)
image = image.crop((0, diff, w, h - diff))
if image.mode != "RGB":
if image.mode not in ("RGB", "RGBA"):
image = image.convert("RGB")
image = image.resize((size, size), settings.AVATAR_RESIZE_METHOD)
thumb = six.BytesIO()

BIN
tests/data/django.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View file

@ -3,6 +3,7 @@ import os.path
from django.contrib.admin.sites import AdminSite
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.test.utils import override_settings
from avatar.admin import AvatarAdmin
from avatar.conf import settings
@ -164,3 +165,16 @@ class AvatarTests(TestCase):
# def testChangePrimaryAvatar
# def testDeleteThumbnailAndRecreation
# def testAutomaticThumbnailCreation
@override_settings(AVATAR_THUMB_FORMAT='png')
def testAutomaticThumbnailCreationRGBA(self):
upload_helper(self, "django.png")
avatar = get_primary_avatar(self.user)
image = Image.open(avatar.avatar.storage.open(avatar.avatar_name(settings.AVATAR_DEFAULT_SIZE), 'rb'))
self.assertEqual(image.mode, 'RGBA')
def testAutomaticThumbnailCreationCMYK(self):
upload_helper(self, "django_pony_cmyk.jpg")
avatar = get_primary_avatar(self.user)
image = Image.open(avatar.avatar.storage.open(avatar.avatar_name(settings.AVATAR_DEFAULT_SIZE), 'rb'))
self.assertEqual(image.mode, 'RGB')