0

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.

imsaiful
  • 1,556
  • 4
  • 26
  • 49
  • Take a look at https://stackoverflow.com/questions/48206051/image-conversion-cannot-write-mode-rgba-as-jpeg – artomason Oct 05 '18 at 22:46
  • I have fixed the issue from that answer. I am looking for why it is happening after uploading more than 10 pics and what about my view. Is this the right way to do? – imsaiful Oct 05 '18 at 22:48
  • That link should get JPEG's working for you again, however as to why it all of a sudden stopped working, that could be a multitude of reasons. I don't know enough about Django to really give you a straight answer, but I would suspect that a recent update might have depreciated the way you were accomplishing this. https://github.com/python-pillow/Pillow/issues/2609 – artomason Oct 05 '18 at 23:26

1 Answers1

0

Please explain to me why I got that error after uploading more than 10 pics

A: there is some image which is RGBA or P mode, which not support directly convert to JPG

Explanation:

summary 1 and 2:

  • backgroud
    • JPG not support alpha = transparency
    • RGBA, P has alpha = transparency
      • RGBA= Red Green Blue Alpha
  • result
    • cannot write mode RGBA as JPEG
    • cannot write mode P as JPEG
  • solution
    • before save to JPG, discard alpha = transparency
      • such as: convert Image to RGB
    • then save to JPG

-> your here: so you use convert to PNG then save

im.save(output, format='PNG', optimize=True, quality=95)

Is this the right way to do so? What if I want to save in jpeg format?

A: No.

Recommend to use:

rgba_or_p_im = original_im
if rgba_or_p_im.mode in ["RGBA", "P"]:
    rgb_im = rgba_or_p_im.convert("RGB")
    rgb_im.save("xxx.jpg")
crifan
  • 12,947
  • 1
  • 71
  • 56