Created
May 12, 2015 10:02
-
-
Save mitio/135d3126bfb50806c242 to your computer and use it in GitHub Desktop.
A script to test why thumbnail creation fails on the live CodeWeek website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from avatar.models import Avatar | |
import datetime | |
import os | |
import hashlib | |
from PIL import Image | |
from django.db import models | |
from django.core.files import File | |
from django.core.files.base import ContentFile | |
from django.core.files.storage import get_storage_class | |
from django.utils.translation import ugettext as _ | |
from django.utils import six | |
from django.db.models import signals | |
from avatar.conf import settings | |
from avatar.util import get_username, force_bytes, invalidate_cache | |
def test_create_thumbnail(self, size, quality=None): | |
# invalidate the cache of the thumbnail with the given size first | |
invalidate_cache(self.user, size) | |
orig = self.avatar.storage.open(self.avatar.name, 'rb') | |
image = Image.open(orig) | |
quality = quality or settings.AVATAR_THUMB_QUALITY | |
w, h = image.size | |
if w != size or h != size: | |
if w > h: | |
diff = int((w - h) / 2) | |
image = image.crop((diff, 0, w - diff, h)) | |
else: | |
diff = int((h - w) / 2) | |
image = image.crop((0, diff, w, h - diff)) | |
if image.mode != "RGB": | |
image = image.convert("RGB") | |
image = image.resize((size, size), settings.AVATAR_RESIZE_METHOD) | |
thumb = six.BytesIO() | |
image.save(thumb, settings.AVATAR_THUMB_FORMAT, quality=quality) | |
thumb_file = ContentFile(thumb.getvalue()) | |
else: | |
thumb_file = File(orig) | |
thumb = self.avatar.storage.save(self.avatar_name(size), thumb_file) | |
Avatar.create_thumbnail = test_create_thumbnail | |
a = Avatar.objects.get(pk=106) | |
a.create_thumbnail(80) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment