11👍
✅
Depending on what your use case is I would suggesting using sorl-thumbnail which works with django-storages
and will handle the resizing for you, and means you don’t have to do the work of managing different sizes yourself.
Instead of defining specific file sizes to store the image for, you would define thumbnail sizes in the places you want to use them, e.g.,
{% thumbnail item.image "300" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
This would produce a thumbnail of maximum width 300px, which would get generated the first time and saved on s3.
The app has a low level API that you can use if you need to generate and fetch thumbnails outside of a template context:
from sorl.thumbnail import get_thumbnail
im = get_thumbnail(my_file, '100x100', crop='center', quality=99)
Source:stackexchange.com