Imaging Resizing -- retain ratio, quality and color profile

from PIL import Image
from StringIO import StringIO
from django.core.files.uploadedfile import SimpleUploadedFile

def get_thumbnail(prod_img):
    THUMBNAIL_SIZE = (720, 720)

    with Image.open(prod_img.original.path) as image:
        # image.thumbnail(THUMBNAIL_SIZE, Image.BICUBIC)
        image.thumbnail(THUMBNAIL_SIZE, Image.LANCZOS)
        icc_profile = image.info.get("icc_profile")

        '''
        subsampling
        If present, sets the subsampling for the encoder.

        keep: Only valid for JPEG files, will retain the original image setting.
        4:4:4, 4:2:2, 4:1:1: Specific sampling values
        -1: equivalent to keep
        0: equivalent to 4:4:4
        1: equivalent to 4:2:2
        2: equivalent to 4:1:1
        '''

        '''
        icc_profile
        The ICC color profile for the image.
        (Ex. Current images have "Adobe RGB (1998)" profile)
        '''

        temp_handle = StringIO()
        image.save(temp_handle, 'jpeg', quality=95, optimize=True, subsampling=0, icc_profile=icc_profile)
        temp_handle.seek(0)

        try:
            filename = '%s_%s_imgid_%s' % (prod_img.product_id, prod_img.product.title.lower().replace(' ','_'), prod_img.id)
        except:
            filename = 'thumb'
        ext = '.jpg'
        print filename
        suf = SimpleUploadedFile(filename + ext, temp_handle.read(), content_type='image/jpeg')
        temp_handle.close()
        return suf

results matching ""

    No results matching ""