Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PIL不在图像上第二次写入文本_Python_Python Imaging Library_Discord.py - Fatal编程技术网

Python PIL不在图像上第二次写入文本

Python PIL不在图像上第二次写入文本,python,python-imaging-library,discord.py,Python,Python Imaging Library,Discord.py,我正在尝试为discord机器人制作一款雄心勃勃的刽子手游戏,为此我需要PIL以文本形式编写。将一些文本写入图像后,然后尝试再次将文本写入同一图像,而不是发送添加了第二个文本的图像,它只发送包含第一个文本的图像。奇怪的是,这个函数通过了,它将它保存为一个具有不同名称的新文件,但没有文本(即第二组)。有什么好处?我做错了什么 import random, discord from requests import get as reGet from io import BytesIO from PI

我正在尝试为discord机器人制作一款雄心勃勃的刽子手游戏,为此我需要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"))

我无法运行它,但当我开始删除不重要的代码时,我看到您以错误的方式在类
img()
中使用image

您总是保存在
self中的图像。打开shell

在第一个函数中,发送与参数相同的图像

 z.tLeft(txt="tLeft", inp_img=z.open_shell)
 z.main_text(txt=word, inp_img=Image.open("tLeft.png"))
因此,在
z.open\u shell
中添加文本,然后保存
z.open\u shell

但在函数中,您发送不同的图像作为参数

 z.tLeft(txt="tLeft", inp_img=z.open_shell)
 z.main_text(txt=word, inp_img=Image.open("tLeft.png"))
因此,您将文本添加到新图像中,但再次保存
z.open_shell
,它具有较旧的版本

你需要

    self.open_shell = inp_img
像这样

def main_text(self, txt, inp_img):

    self.open_shell = inp_img

    image = ImageDraw.Draw(inp_img)
    image.text((20,20), txt, font=fnt, fill=(255, 255, 255))

    self.open_shell.save("mainText.png")

我无法运行它,但当我开始删除不重要的代码时,我看到您以错误的方式在类
img()
中使用image

您总是保存在
self中的图像。打开shell

在第一个函数中,发送与参数相同的图像

 z.tLeft(txt="tLeft", inp_img=z.open_shell)
 z.main_text(txt=word, inp_img=Image.open("tLeft.png"))
因此,在
z.open\u shell
中添加文本,然后保存
z.open\u shell

但在函数中,您发送不同的图像作为参数

 z.tLeft(txt="tLeft", inp_img=z.open_shell)
 z.main_text(txt=word, inp_img=Image.open("tLeft.png"))
因此,您将文本添加到新图像中,但再次保存
z.open_shell
,它具有较旧的版本

你需要

    self.open_shell = inp_img
像这样

def main_text(self, txt, inp_img):

    self.open_shell = inp_img

    image = ImageDraw.Draw(inp_img)
    image.text((20,20), txt, font=fnt, fill=(255, 255, 255))

    self.open_shell.save("mainText.png")

首先,您可以使用
print()
来查看变量中的值-也许您使用的值与您预期的不同。我已经在这样做了。如果我用一个映像来做,它不会返回任何有用的东西。我不能运行代码,但我会检查它是否在磁盘上保存了正确的文件,并在发送之前保存它-我不知道异步代码如何工作。我会检查不同的
fill
color`-有时候当图像不是RGB时,它可能会给出一个字符串结果。你删除了JS问题,所以我在这里评论:你有
getElementById
而没有
文档。
在它之前,你没有在你的testext()函数中加引号。这两个错误都可以通过检查浏览器控制台来解决,这始终是第一步;)啊,我在VSC中使用它,我当时可能应该使用浏览器。谢谢首先,您可以使用
print()
来查看变量中的值-也许您使用的值与您预期的不同。我已经在这样做了。如果我用一个映像来做,它不会返回任何有用的东西。我不能运行代码,但我会检查它是否在磁盘上保存了正确的文件,并在发送之前保存它-我不知道异步代码如何工作。我会检查不同的
fill
color`-有时候当图像不是RGB时,它可能会给出一个字符串结果。你删除了JS问题,所以我在这里评论:你有
getElementById
而没有
文档。
在它之前,你没有在你的testext()函数中加引号。这两个错误都可以通过检查浏览器控制台来解决,这始终是第一步;)啊,我在VSC中使用它,我当时可能应该使用浏览器。谢谢