我一直在开发一个不和谐的机器人,它将获取发布的任何图像并自动与 mrbeast 交换该图像,但我似乎无法弄清楚如何从面部交换器 api 获取输出并将其转换为机器人可以使用的图像可以发帖
我尝试使用
guh.channel.send(file = theimage)
它返回了一个很长的错误。
我尝试将图像保存到我的设备,但脚本缺乏权限,我不想这样做,因为我不想将数百张兽脸交换图像保存到我的设备。
以前,当我使用 print("response.content") 代替发送不和谐消息时,它会输出一串由斜杠分隔的超长字母和数字
import discord
import requests
import base64
from PIL import Image
from io import BytesIO
intents = discord.Intents.all()
api_key = "**********"
url = "https://api.segmind.com/v1/faceswap-v2"
def image_file_to_base64(image_path):
with open(image_path, 'rb') as f:
image_data = f.read()
return base64.b64encode(image_data).decode('utf-8')
def image_url_to_base64(image_url):
response = requests.get(image_url)
image_data = response.content
return base64.b64encode(image_data).decode('utf-8')
class Client(discord.Client):
async def on_message(self, guh):
if guh.author.bot:
return
try:
image = guh.attachments[0].url
data = {
"source_img": image_file_to_base64(r"C:\Users\ajoes\Downloads\discord bot\MrBeast.jpg"),
"target_img": image_url_to_base64(image),
"source_faces_index": 0,
"face_restore": "codeformer-v0.1.0.pth",
"base64": False
}
headers = {'x-api-key': api_key}
response = requests.post(url, json=data, headers=headers)
theimage = Image.open(BytesIO(response.content))
await guh.channel.send(theimage)
except IndexError:
return
client = Client(intents=intents)
client.run("MTI5OTEyNTc2MjA2NTk1Njg3NQ.Gud8YH.7-uOy3t0j_K9miAZ3AiyF6Vhhmy_xrRiQP2eVw")
在当前状态下,当我发送图像时,机器人只会回复
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=803x920 at 0x239294F5DF0\>
如果你想发送图像到Discord,你需要使用数据类
discord.File
:
class Client(discord.Client):
async def on_message(self, guh):
if guh.author.bot:
return
try:
image = guh.attachments[0].url
data = {
"source_img": image_file_to_base64(r"C:\Users\ajoes\Downloads\discord bot\MrBeast.jpg"),
"target_img": image_url_to_base64(image),
"source_faces_index": 0,
"face_restore": "codeformer-v0.1.0.pth",
"base64": False
}
headers = {'x-api-key': api_key}
response = requests.post(url, json=data, headers=headers)
bytes_img = BytesIO(response.content)
file = discord.File(fp=bytes_img, filename='MrBeast.jpg')
await guh.channel.send(file=file)
except IndexError:
return