Skip to content Skip to sidebar Skip to footer

Sorl-thumnail Resizing And Saving

I'm trying to use sorl-thumnail to resize the image in the views then saving it and getting IOError while calling get_thumnail(). Also I need to know how to save the resized image.

Solution 1:

You can't use request.FILES['photo'] here because uploaded file can be in memory or somewhere else. Save this file to filesystem first, then use get_thumbnail. For example, you can call it on your objects after it's returned by form.save().

Solution 2:

If you want to access the uploaded file directly then first you will have to get the path of temp memory where it is uploaded.

import os
import tempfile

def_file_path(uploaded_file):
    '''  Converts InMemoryUploadedFile to on-disk file so it will have path. '''try:
        return uploaded_file.temporary_file_path()
    except AttributeError:
        fileno, path = tempfile.mkstemp()
        temp_file = os.fdopen(fileno,'w+b')
        for chunk in uploaded_file.chunks():
            temp_file.write(chunk)
        temp_file.close()
        return path

Then you can access the uploaded file at path:

path = _file_path(request.FILES['photo'])

Post a Comment for "Sorl-thumnail Resizing And Saving"