I have upload more than 10 images with the help of the following code in my django application(using image pillow library).
def save(self):
im = Image.open(self.image)
output = BytesIO()
im = im.resize((500, 500))
im.save(output, format='JPEG', optimize=True, quality=95)
output.seek(0)
self.image = InMemoryUploadedFile(output, 'ImageField', "%s.jpg" % self.image.name.split('.')[0], 'image/jpeg',
sys.getsizeof(output), None)
super(Report_item, self).save()
But now my Django application is giving me following error.
Exception Type: OSError
Exception Value:
cannot write mode RGBA as JPEG
I got the solution from one of the answers at StackOverflow to change the image type to from any other to png.
Now my code looks like this but the process seems a bit slow as compared to it was earlier.
def save(self):
im = Image.open(self.image)
output = BytesIO()
im = im.resize((500, 500))
im.save(output, format='PNG', optimize=True, quality=95)
output.seek(0)
self.image = InMemoryUploadedFile(output, 'ImageField', "%s.png" % self.image.name.split('.')[0], 'image/jpeg',
sys.getsizeof(output), None)
super(Report_item, self).save()
Now the image is uploading without any issue.
Please explain to me why I got that error after uploading more than 10 pics. Is this the right way to do so. What if I want to save in jpeg format? My application is in production. So I want to push only the right code for the right job. Please help me to figure out.