0

I was able to get the URL from the attachment and send back the image to the discord server, but when it came to displaying the image through the Pycharm IDE, I was not able to do so.

I tried using matplotlib and skimage to display the image but to no avail.

import os
import discord
from discord.ext import commands
import requests
from discord import Embed
import matplotlib.pyplot as plt
from skimage import io


client = commands.Bot(command_prefix = '.')

@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))


@client.event
async def on_message(message):
  if message.author == client.user:
    return
  if message.content.startswith('$hello'):
    await message.channel.send('Hello')
  if message.attachments[0].url.endswith('png') or message.attachments[0].url.endswith('jpg') or message.attachments[0].url.endswith('jpeg'):
    await message.channel.send('This is a picture') #check if the attachment is a image
    url = message.attachments[0].url  # get the url to the attached image
    e = discord.Embed()
    e.set_image(url=message.attachments[0].url)
    await message.channel.send(embed=e) # embed the image and send it back to the discord channel
    #try to display the image
    image = io.imread(url)
    plt.imshow(image)
    plt.show()

Re-Upload Image

1 Answers1

-1

We can directly use requests to load the url.

import requests
from matplotlib.pyplot import imshow
import numpy as np
from PIL import Image


url = "https://i.stack.imgur.com/5yS9j.jpg"
im = Image.open(requests.get(url, stream=True).raw)
imshow(np.asarray(im))

References:

Ceres
  • 2,498
  • 1
  • 10
  • 28