plebo.blogg.se

Django pillow resize image on upload
Django pillow resize image on upload









save ( *args, **kwargs ) app/utils.py from django. ImageField (default = "profile-default.png", upload_to = "profile_pics" )

django pillow resize image on upload

Here is what actually worked for me using Python 3.8, Django 3.1.3 and Pillow 8.0.1: user/models.py from app. The examples of this that I found were either incomplete or used old versions of python. I couldn’t figure out how to get that setup working with django-storages.Ī second approach I found mentioned using a buffer to save the resized image into, and then saving that to AWS. I initially tried that approach, but according to the AWS docs you shouldn’t use the same bucket for input and output, meaning I had to create a second S3 bucket just for resized images. Most of what I found online suggested removing the Pillow resize and writing an AWS Lambda function to handle the resize on upload. I get aĮxception Value: This backend doesn't support absolute paths. width > 512 :īut it become a bit more complicated when using S3 buckets. So I want to resize the image on save, before uploading it to AWS.įor storing images locally, I just have to install Pillow and override the save method in my Profile model like so: from PIL import Image If a users uploads a massive 4K image, that is never going to be displayed larger than 256px wide, I don’t want to have to store that.

django pillow resize image on upload

I have a users app with a simple Profile model, and I’m using django-storages to manage uploading my images and other static assets to Amazon S3.

django pillow resize image on upload

I've been leveling-up on my Django and Python skills lately, and I ran into a fairly common situation, that I could’t find a definitive solution for.











Django pillow resize image on upload