4

I'm trying to invert an image downloaded from telegram with PIL and then send it back:

import PIL, PIL.Image, PIL.ImageFont, PIL.ImageOps
from io import BytesIO

foto = msg["reply_to_message"]["photo"][0]["file_id"]
path = '/home/****/mysite/'+str(secrets.randbelow(10000))
bot.download_file(foto, path) #download the photo from telegram (it works)
img = PIL.Image.open(path).convert('RGBA')
img = PIL.ImageOps.invert(img)
final = BytesIO()
img.save(final, 'png')
final.seek(0)
bot.sendPhoto(chat_id, final) #send the photo in the telegram chat

But the exception handler sends me this message:

Not supported for this image mode.

What's the problem?

Sarfo
  • 125
  • 1
  • 10
  • 1
    Possible duplicate of [Python: invert image with transparent background (PIL, Gimp,...)](https://stackoverflow.com/questions/11484204/python-invert-image-with-transparent-background-pil-gimp) – ForceBru Jun 24 '18 at 13:14

1 Answers1

1

You cannot invert an image with transparency, at least in this way.

Easiest option would be to change RGBA to RGB in your convert statement.

If you need to deal with transparent images you can split image into r,g,b and alpha(a) channels, merge r,g,b into one image and then invert that. You then reconstruct image merge of resultant channels r', g', b' and original a.

cdplayer
  • 495
  • 5
  • 15