0

I have a grayscale command to turn an image into grayscale. I'm using bytes to save the image and send it.

Code snippet

#  attachment is a parameter in the command in previous lines that is an image
            temp = io.BytesIO()
            await attachment.save(temp)   # save attachment to temp

            img = Image.open(
                temp).convert(
                'RGBA')  # https://stackoverflow.com/questions/24996518/what-size-to-specify-to-pil-image-frombytes
            imgdata = img.getdata()
            # grayscale
            grayscale = []
            for x in imgdata:
                Y = int(x[0] * 0.299) + int(x[1] * 0.587) + int(x[
                                                                    2] * 0.114)  # Helpful website: https://www.dynamsoft.com/blog/insights/image-processing/image-processing-101-color-space-conversion/
                if inverse:
                    Y = 255 - Y  # create inverse
                grayscale.append((Y, Y, Y))  # append data to grayscale list of rgb

            img.putdata(grayscale)  # save grayscale list of rgb to image
            with io.BytesIO() as output:  # open iobytes
                img.save(output, format="PNG")  # save img into iobytes
                output.seek(0)
                await ctx.respond(file=discord.File(output, filename="grayscale.png"))  # send image

What I want to do

In the last line, I want to send the image in an embed. However, in the pycord docs: docs image

It shows that an image needs a URL. However, if I'm using bytes, how do I send the image in an embed?

Saving the image to a folder then sending is possible, however I don't find this efficient (I'm using a server with limited storage). Therefore, I want to use bytes to use RAM instead. However, if this is not possible, then that would be a sufficient answer.

Full code For MRE

    @command_group.command(name="grayscale", description="Converts image to grayscale")
    async def grayscale(self, ctx,
                        attachment: discord.Option(discord.Attachment, required=True,
                                                   description="Convert image to grayscale"),
                        inverse: discord.Option(bool, required=False, description="Invert image grayscale")):
        try:
            if not self.check_image_size(attachment):
                return await ctx.respond(
                    "Image too big. If you want image compression added, make a suggestion in the main server `/botinfo`",
                    ephemeral=True)
            await ctx.defer()
            temp = io.BytesIO()
            await attachment.save(temp)

            img = Image.open(
                temp).convert(
                'RGBA')  # https://stackoverflow.com/questions/24996518/what-size-to-specify-to-pil-image-frombytes
            imgdata = img.getdata()
            # grayscale
            grayscale = []
            for x in imgdata:
                Y = int(x[0] * 0.299) + int(x[1] * 0.587) + int(x[
                                                                    2] * 0.114)  # Helpful website: https://www.dynamsoft.com/blog/insights/image-processing/image-processing-101-color-space-conversion/
                if inverse:
                    Y = 255 - Y  # create inverse
                grayscale.append((Y, Y, Y))

            img.putdata(grayscale)
            with io.BytesIO() as output:
                img.save(output, format="PNG")
                output.seek(0)
                await ctx.respond(file=discord.File(output, filename="grayscale.png"))

        except PIL.UnidentifiedImageError:
            print(attachment.content_type)
            await ctx.respond("This is not a valid image!", ephemeral=True)
Blue Robin
  • 847
  • 2
  • 11
  • 31
  • I don't think it is possible, the [developer portal for discord](https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure) also uses urls for embedded images. – Shorn May 24 '23 at 02:37

0 Answers0