【问题标题】:PIL not writing text a second time onto imagePIL不会第二次在图像上写入文本
【发布时间】: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() 来查看变量中的值 - 也许您使用的值与您的预期不同。
  • 我已经在这样做了。如果我用图像来做,它不会返回任何有用的东西。
  • 我无法运行代码,但我会检查它是否将正确的文件保存在磁盘上,并在发送之前保存它 - 我不知道如何处理异步代码。我会检查不同的fill color` - 当图像不是RGB时,有时它可能会给出strnage结果。
  • 您删除了 JS 问题,所以我在这里评论:您之前有 getElementById 没有 document.,并且您没有在 tempText() 函数中将 id 放在引号内。这两个错误都可以通过检查浏览器控制台来解决,这始终是第一步;)
  • 啊,我在VSC中使用它,我应该使用浏览器。谢谢!

标签: python python-imaging-library discord.py


【解决方案1】:

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

你总是保存你在self.open_shell中的图像

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

 z.tLeft(txt="tLeft", inp_img=z.open_shell)

所以将文本添加到z.open_shell 并保存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")

【讨论】:

    猜你喜欢
    • 2019-01-17
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2011-12-03
    • 2013-04-28
    • 2010-09-26
    • 2016-09-29
    相关资源
    最近更新 更多