<tfoot id='6hCuO'></tfoot>
<i id='6hCuO'><tr id='6hCuO'><dt id='6hCuO'><q id='6hCuO'><span id='6hCuO'><b id='6hCuO'><form id='6hCuO'><ins id='6hCuO'></ins><ul id='6hCuO'></ul><sub id='6hCuO'></sub></form><legend id='6hCuO'></legend><bdo id='6hCuO'><pre id='6hCuO'><center id='6hCuO'></center></pre></bdo></b><th id='6hCuO'></th></span></q></dt></tr></i><div id='6hCuO'><tfoot id='6hCuO'></tfoot><dl id='6hCuO'><fieldset id='6hCuO'></fieldset></dl></div>

<small id='6hCuO'></small><noframes id='6hCuO'>

  • <legend id='6hCuO'><style id='6hCuO'><dir id='6hCuO'><q id='6hCuO'></q></dir></style></legend>

      <bdo id='6hCuO'></bdo><ul id='6hCuO'></ul>

        从内存中发送图像

        时间:2023-10-11

          <tfoot id='R5JhS'></tfoot>

          <small id='R5JhS'></small><noframes id='R5JhS'>

          <legend id='R5JhS'><style id='R5JhS'><dir id='R5JhS'><q id='R5JhS'></q></dir></style></legend>
        1. <i id='R5JhS'><tr id='R5JhS'><dt id='R5JhS'><q id='R5JhS'><span id='R5JhS'><b id='R5JhS'><form id='R5JhS'><ins id='R5JhS'></ins><ul id='R5JhS'></ul><sub id='R5JhS'></sub></form><legend id='R5JhS'></legend><bdo id='R5JhS'><pre id='R5JhS'><center id='R5JhS'></center></pre></bdo></b><th id='R5JhS'></th></span></q></dt></tr></i><div id='R5JhS'><tfoot id='R5JhS'></tfoot><dl id='R5JhS'><fieldset id='R5JhS'></fieldset></dl></div>
              <tbody id='R5JhS'></tbody>

              • <bdo id='R5JhS'></bdo><ul id='R5JhS'></ul>

                  本文介绍了从内存中发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试为 Discord 机器人实现一个系统,该系统可以动态修改图像并将它们发送给机器人用户.为此,我决定使用 Pillow (PIL) 库,因为它对我的目的来说看起来简单明了.

                  I am trying to implement a system for a Discord bot that dynamically modifies images and sends them to the bot users. To do that, I decided to use the Pillow (PIL) library, since it seemed simple and straightforward for my purposes.

                  这是我的工作代码示例.它加载一个示例图像,作为测试修改,在其上绘制两条对角线,并将图像作为 Discord 消息输出:

                  Here is an example of my working code. It loads an example image, as a test modification, draws two diagonal lines on it, and outputs the image as a Discord message:

                  # Open source image
                  img = Image.open('example_image.png')
                  
                  # Modify image
                  draw = ImageDraw.Draw(img)
                  draw.line((0, 0) + img.size, fill=128)
                  draw.line((0, img.size[1], img.size[0], 0), fill=128)
                  
                  # Save to disk and create discord file object
                  img.save('tmp.png', format='PNG')
                  file = discord.File(open('tmp.png', 'rb'))
                  
                  # Send picture as message
                  await message.channel.send("Test", file=file)
                  

                  这会导致我的机器人发出以下消息:

                  This results in the following message from my bot:

                  这行得通;但是,我想省略将图像保存到硬盘驱动器并再次加载它的步骤,因为这似乎相当低效且不必要.经过一番谷歌搜索后,我遇到了以下解决方案;但是,它似乎不起作用:

                  This works; however, I would like to omit the step of saving the image to the hard drive and loading it again, since that seems rather inefficient and unnecessary. After some googling I came across following solution; however, it doesn't seem to work:

                  # Save to disk and create discord file object
                  # img.save('tmp.png', format='PNG')
                  # file = discord.File(open('tmp.png', 'rb'))
                  
                  # Save to memory and create discord file object
                  arr = io.BytesIO()
                  img.save(arr, format='PNG')
                  file = discord.File(open(arr.getvalue(), 'rb'))
                  

                  这会导致以下错误消息:

                  This results in the following error message:

                  Traceback (most recent call last):
                      File "C:Users<username>AppDataLocalProgramsPythonPython38-32libsite-packagesdiscordclient.py", line 270, in _run_event
                          await coro(*args, **kwargs)
                      File "example_bot.py", line 48, in on_message
                          file = discord.File(open(arr.getvalue(), 'rb'))
                  UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
                  

                  推荐答案

                  discord.File 支持传递 io.BufferedIOBase 作为 fp 参数.
                  io.BytesIO 继承自 <代码>io.BufferedIOBase.
                  这意味着您可以直接将 io.BytesIO 的实例作为 fp 来初始化 discord.File,例如:

                  discord.File supports passing io.BufferedIOBase as the fp parameter.
                  io.BytesIO inherits from io.BufferedIOBase.
                  This means that you can directly pass the instance of io.BytesIO as fp to initialize discord.File, e.g.:

                  arr = io.BytesIO()
                  img.save(arr, format='PNG')
                  arr.seek(0)
                  file = discord.File(arr)
                  

                  另一个例子可以在 如何上传图片?discord.py 文档中的常见问题解答部分.

                  这篇关于从内存中发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 discord.py 中获取频道的最新消息? 下一篇:检查用户是否在 discord.py 中具有权限

                  相关文章

                  最新文章

                  <i id='bW1TS'><tr id='bW1TS'><dt id='bW1TS'><q id='bW1TS'><span id='bW1TS'><b id='bW1TS'><form id='bW1TS'><ins id='bW1TS'></ins><ul id='bW1TS'></ul><sub id='bW1TS'></sub></form><legend id='bW1TS'></legend><bdo id='bW1TS'><pre id='bW1TS'><center id='bW1TS'></center></pre></bdo></b><th id='bW1TS'></th></span></q></dt></tr></i><div id='bW1TS'><tfoot id='bW1TS'></tfoot><dl id='bW1TS'><fieldset id='bW1TS'></fieldset></dl></div>

                      <bdo id='bW1TS'></bdo><ul id='bW1TS'></ul>
                  1. <tfoot id='bW1TS'></tfoot>
                    1. <legend id='bW1TS'><style id='bW1TS'><dir id='bW1TS'><q id='bW1TS'></q></dir></style></legend>
                    2. <small id='bW1TS'></small><noframes id='bW1TS'>