OSError: cannot write mode RGBA as JPEG

Hi @daviesian,

I tried to use your code, but I got this error:

OSError: cannot write mode RGBA as JPEG at /usr/local/lib/python3.7/site-packages/PIL/JpegImagePlugin.py, line 617 called from /usr/local/lib/python3.7/site-packages/PIL/Image.py, line 2102 called from [ServerModule1, line 16](javascript:void(0)) called from [Form1, line 14](javascript:void(0))

here is my app:

Can you please let me know what I am missing?
Thanks!

Moved to a new topic instead of resurrecting old thread that deals with a different issue. This appears to be related to a specific error.

OSError: cannot write mode RGBA as JPEG

Please see the Google results that discuss this error to see if it can help.

For example:

Let us know if the solutions there help you out. For example:

im = im.convert("RGB")

I tried it before (and I tried it again), I got this error:

ValueError: conversion from RGB to JPEG not supported at /usr/local/lib/python3.7/site-packages/PIL/Image.py, line 1000 called from [ServerModule1, line 13](javascript:void(0)) called from [Form1, line 14](javascript:void(0))

I search different websites, some of them I didn’t understand their solution and others I got different errors :slightly_frowning_face:

It is always good to describe initially what you have tried so that we can help from that point.

Did you also try the last solution in that thread by “Homm”? It has many votes.

Forgive my ignorance, I have limited programing experience, what I am trying to do is to simply resize images captured by the user camera before uploading it to the anvil tables, so I doesn’t take too long to upload the images. I tried to use the code in the previous post, so, what I really need is to have this code to work:

from PIL import Image
import io

@anvil.server.callable
def resize(file):
  # Convert the 'file' Media object into a Pillow Image
  img = Image.open(io.BytesIO(file.get_bytes()))
  
  # Resize the image to the required size
  img = img.resize((800,600))
  
  # Convert the Pillow Image into an Anvil Media object and return it
  bs = io.BytesIO()
  img.save(bs, format="JPEG")
  return anvil.BlobMedia("image/jpeg", bs.getvalue(), name=name)

Can you please help?

No problem at all. We’re here to help.

When you run this code, are you getting this error still?

OSError: cannot write mode RGBA as JPEG

Thank you! Yes, I still get the error, here is my app:

https://anvil.works/build#clone:JJ3LQZBZZFPZBRVG=7XY3JAAOW4J3SXOII56ECQQA

Please try this again:

img = img.convert("RGB")

It works perfectly but you will have to copy it exactly as it is written (I belive you had your variable names wrong and you used “JPEG” instead of “RGB”).

You will also want to define “name” in your app since it is undefined at the moment.

What is your code? When I tried this:

@anvil.server.callable
def resize(file):
  # Convert the 'file' Media object into a Pillow Image
  img = Image.open(io.BytesIO(file.get_bytes()))
  
  # Resize the image to the required size
  img = img.resize((800,600))
  img = img.convert("RGB")
  # Convert the Pillow Image into an Anvil Media object and return it
  bs = io.BytesIO()
  img.save(bs, format="RGB")
  return anvil.BlobMedia("image/RGB", bs.getvalue(), name="Name")

I got this error:

KeyError: ‘RGB’ at /usr/local/lib/python3.7/site-packages/PIL/Image.py, line 2091 called from [ServerModule1, line 19](javascript:void(0)) called from [Form1, line 14](javascript:void(0))

You still have to use format="JPEG" towards the end as it is in the clone you sent me.

And "image/jpeg" when creating the media object.

1 Like

Thank you, it worked now, I thought I have to change all of them.
Thank you for being patient :blush:

2 Likes