Search code examples
pythondiscordpython-imaging-libraryopencvbytesio

Discord.Py blend pictures and send them without dowload them


I have the following problem:

1.- I want to send a file in discord without dowloading. I don't know if this is posible but I want to send it for example with BytesIo.

2.- I have one picture saved on my Bot files and the other one comes from ctx.author.avatar

3.- I want to blend both images and send the result. With blend I mean like for example if I would be using cv2 I would use addWeighted().

The Code I have right know what does is dowload the picture of the member, using cv2 to read both pictures, resize them and use addWeighted. After that I save the blend picture and I send it as a message. When all It's done I delete the pictures (both, the avatar and the blend one). From my point of view this is really inefficient, thats why I want know if there is a way using PIL and BytesIo or something to use the dataArray to blend them and send it without dowload it.

So in short, I want to know if there is a way to blends both images without download the second one (member avatar picture) and send it without dowloading the blend image.

I can the code I already have if needed but as my code is download the picture I guess that won't help.


Solution

  • The way I found at the end to solve it, was using just Pillow doing the following:

    image1 = Image.open("img.jpg", mode='r')
    image2 = Image.open(requests.get(url, stream=True).raw)
    

    With I have both images on memory, so I just needed to resize both of them and blend. Finally I just used io.Bytes() to make into bytes and send it. How to blend -> https://pythontic.com/image-processing/pillow/blend