Source

mediatr / filters / thumbnail.py

Full commit
"""
Make thumbnail for jpeg images.
"""
from PIL import Image
from StringIO import StringIO
from django.core.files.base import ContentFile


def enabled():
    """
    Various cheks goes here (settings, imports, etc)
    """
    return True

def filter_obj(objekt, params={'max_size': 500, 'default': True}):
    """
    Main filter body. objekt is instance of mediatr.models.Item, params is a filter specific tunings.
    """
    if objekt.mime.name != 'image/jpeg':
        return
    size = (params.get('max_size'), params.get('max_size'))
    tmpfile = StringIO()
    im = Image.open(objekt.file.path)
    im.thumbnail(size, Image.ANTIALIAS)
    im.save(tmpfile, im.format)
    variant = objekt.variant_set.model()
    variant.parent = objekt
    variant.name="thumb_%spx" % (params.get('max_size'))
    variant.default = params.get('default')
    variant.file.save("variants/%s_%s.jpg" % (objekt.slug, variant.name), ContentFile(tmpfile.getvalue()))
    variant.save()