Skip to content

Commit

Permalink
Merge pull request #75 from z4r/patch-1
Browse files Browse the repository at this point in the history
Preserve RGBA image.mode
  • Loading branch information
grantmcconnaughey committed Feb 9, 2016
2 parents cd23ae1 + 3b3bdd8 commit a6b4c2b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion avatar/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def create_thumbnail(self, size, quality=None):
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()
Expand Down
Binary file added tests/data/django.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/django_pony_cmyk.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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
Expand Down Expand Up @@ -164,3 +165,16 @@ def test_too_many_avatars(self):
# 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')

0 comments on commit a6b4c2b

Please sign in to comment.