Home > Enterprise >  'cannot write mode RGBA as JPEG' when saving image
'cannot write mode RGBA as JPEG' when saving image

Time:01-03

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?

CodePudding user response:

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.

  • Related