【发布时间】:2021-04-04 05:34:27
【问题描述】:
我正在尝试为一个不和谐的机器人制作一个有点雄心勃勃的刽子手游戏,为此我需要 PIL 以文本形式编写。在将一些文本写入图像后,然后尝试再次将文本写入同一图像,而不是发送添加了第二个文本的图像,它只发送带有第一个文本的图像。奇怪的是,函数通过,它将它保存为一个具有不同名称但没有文本的新文件(即第二组)。是什么赋予了?我在这里做错了什么?
import random, discord
from requests import get as reGet
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
# Just a random image I found off google, not actually using it
inp_shell = reGet("https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Circle_-_black_simple.svg/1200px-Circle_-_black_simple.svg.png")
# Yes, I know, probably not the best place to put the font, I'll change it later
fnt = ImageFont.truetype("modules/Roboto-Regular.ttf", size= 40)
# Opens a list of words that it can get from; The file here is just a stub
with open('words_alpha.txt') as words_file:
word_list = words_file.read().splitlines()
class img():
def __init__(self):
self.open_shell = Image.open(BytesIO(inp_shell.content))
# Text in the top left
async def tLeft(self, txt, inp_img):
image = ImageDraw.Draw(inp_img)
image.text((10,10), txt, font=fnt, fill=(0, 0, 0))
self.open_shell.save("tLeft.png")
async def main_text(self, txt, inp_img):
image = ImageDraw.Draw(inp_img)
# Used to position the text in the center but currently is not being used
x, y = inp_img.size
pos = x/2
# I've tried changing the fill and position, and still nothing.
# This is probably the source of the problem
image.text((20,20), txt, font=fnt, fill=(255, 255, 255))
print(txt)
self.open_shell.save("mainText.png")
# Creates a dictionary with the length of the words assigned as they keys,
# I think anyways, I didn't write this
by_length = {}
for word in word_list:
by_length.setdefault(len(word), []).append(word)
# Retrieves a random word with a certain length
async def word_finder(wordlength):
global word
word = random.choice(by_length[wordlength])
print(word)
# Main function
async def hanggMan(message): #double g in hang is intentional
content = message.clean_content[11:]
print(content) # Just used to make sure it's going through
# For now I'm using a word length of 5
if content.lower() == "5":
z = img() # Calls an instance of the img class
# Puts the image in an embed; ignore t his
embed = discord.Embed(title="testtest testtesttesttest")
embed.type = "rich"
embed.colour = discord.Color.gold()
await word_finder(5) # Tells the word_finder function to find a random word with a length of 5
await z.tLeft(txt="tLeft", inp_img= z.open_shell) # Calls the tLeft function and tells it to write "tLeft"
# Calls the main_text function and tells it to write the word on top of
# "tLeft.png". There is a print statement in the function and it actually
# does print the word, so this is not likely to be the source of the problem
await z.main_text(txt=word, inp_img=Image.open("tLeft.png"))
embed.set_image(url="attachment://mainText.png")
# The interesting thing about this is that it actually does save it as "mainText.png"
# and sends the file properly, but the word is nowhere to be found
await message.channel.send(embed=embed, file=discord.File("mainText.png"))
【问题讨论】:
-
首先您可以使用
print()来查看变量中的值 - 也许您使用的值与您的预期不同。 -
我已经在这样做了。如果我用图像来做,它不会返回任何有用的东西。
-
我无法运行代码,但我会检查它是否将正确的文件保存在磁盘上,并在发送之前保存它 - 我不知道如何处理异步代码。我会检查不同的
fillcolor` - 当图像不是RGB时,有时它可能会给出strnage结果。 -
您删除了 JS 问题,所以我在这里评论:您之前有
getElementById没有document.,并且您没有在 tempText() 函数中将 id 放在引号内。这两个错误都可以通过检查浏览器控制台来解决,这始终是第一步;) -
啊,我在VSC中使用它,我应该使用浏览器。谢谢!
标签: python python-imaging-library discord.py