Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

'cannot write mode RGBA as JPEG' when saving image

I’m getting:

OSError at /admin/blog/post/add/
cannot write mode RGBA as JPEG

error when uploading an image file other than ‘jpeg’ with Pillow. This is the function I’m using to resize the image:

def resize_image(image, size):
    """Resizes image"""

    im = Image.open(image)

    im.convert('RGB')

    im.thumbnail(size)

    thumb_io = BytesIO()

    im.save(thumb_io, 'JPEG', quality=85)

    thumbnail = File(thumb_io, name=image.name)

    return thumbnail

Most solutions to this same error seem to be solved by converting to 'RGB', but I’m already doing that in my function except it still keeps giving error when uploading, for example, a .png image. How can I fix it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to assign the result of im.convert:

im = Image.open(image).convert('RGB')

As it is, you’re converting to RGB and throwing the result away.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading