久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <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 class="qwawimqqmiuu" id='R5JhS'><tfoot id='R5JhS'></tfoot><dl id='R5JhS'><fieldset id='R5JhS'></fieldset></dl></div>

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

      從內存中發送圖像

      Send image from memory(從內存中發送圖像)

      <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 class="qwawimqqmiuu" 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>

                  <tbody id='6hCuO'></tbody>
                本文介紹了從內存中發送圖像的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我正在嘗試為 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模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                How to make a discord bot that gives roles in Python?(如何制作一個在 Python 中提供角色的不和諧機器人?)
                Discord bot isn#39;t responding to commands(Discord 機器人沒有響應命令)
                Can you Get the quot;About mequot; feature on Discord bot#39;s? (Discord.py)(你能得到“關于我嗎?Discord 機器人的功能?(不和諧.py))
                message.channel.id Discord PY(message.channel.id Discord PY)
                How do I host my discord.py bot on heroku?(如何在 heroku 上托管我的 discord.py 機器人?)
                discord.py - Automaticaly Change an Role Color(discord.py - 自動更改角色顏色)
                  <tbody id='bW1TS'></tbody>
                  <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 class="qwawimqqmiuu" id='bW1TS'><tfoot id='bW1TS'></tfoot><dl id='bW1TS'><fieldset id='bW1TS'></fieldset></dl></div>

                        <bdo id='bW1TS'></bdo><ul id='bW1TS'></ul>
                      • <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'>

                          主站蜘蛛池模板: 欧美日韩免费一区二区三区 | 亚洲精品久久久久久久久久久 | 国产精品视频在线观看 | 婷婷久久五月天 | 91欧美激情一区二区三区成人 | 国产成人三级一区二区在线观看一 | 国产一区二区在线播放 | 天堂av影院 | 伊人春色av| 黄色录像免费看 | 日韩精品影视 | 精品视频网站 | 中文字幕在线一区二区三区 | 91福利区 | 国产欧美日本 | 欧美精品区 | 久久久久久中文字幕 | 欧美日韩在线视频观看 | 伊人国产女 | 成人一区二区三区 | 女人黄网站 | 国产精品自在线 | 精品视频在线播放 | 国产成人福利 | 免费一级a毛片夜夜看 | 精品日韩在线 | 国产精品视频久久久 | 国产美女网站 | 黄色网页免费 | 特一级黄色片 | 神马午夜嘿嘿 | 免费av在线网站 | 香蕉视频导航 | 99爱视频 | 视频在线观看一区 | 黄色免费网站视频 | 噜噜噜在线 | 美女视频福利 | 日韩黄色网址 | 四虎免费在线观看 | 又黄又爽的免费视频 |