I have a an HTML form that allows images upload. I want to save the original image to S3 storage and then convert it to thumbnail and save the thumbnail to the same storage.
I could only save the original image but after converting it to thumbnail using PIL when I try to save it I get "Server error 500"
My views code is as follow,
from django.core.files.storage import default_storage as storage
class upload(View):
def post(self, request):
image = request.FILES['pic']
storage.save(image.name, image)
thisfile = storage.open(image.name)
newimg = Image.open(thisfile)
thumb = newimg.resize((128,128), Image.ANTIALIAS)
storage.save("newimagename", newimg)
#Trying to save it this way doesn't work either
#thisobj = userProfile.objects.get(user= request.user)
#thisobj.image = newimg
#thisobj.save()
I tried some print statements to make sure that it was converting the file with no problem and it was but it saved it to memory and it prints like,
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=600x600 at 0x105C1DEF0>
I tried overwriting the save method in models.py but I get the same error
def save(self, *args, **kwargs):
super(userProfile, self).save(*args, **kwargs)
if self.image:
self.image.name = "y.JPG"
image = Image.open(self.image.path)
image = image.resize((128,128), Image.ANTIALIAS)
image.save(self.image.path)