【问题标题】:Tkinter - How to change text into an image?Tkinter - 如何将文本更改为图像?
【发布时间】:2020-08-06 09:31:47
【问题描述】:

让我们以此作为示例程序:

from tkinter import *
import random
win = Tk()
win.geometry('200x200')

alphabets = ["A", "B", "C"]
rand_alpha = random.choice(alphabets)

lbl = Label(win, font = 'Ariel 30',text = rand_alpha)
lbl.pack()

win.mainloop()

在上述程序中,我想将“rand_alpha”的文本更改为图像文件,以便可以使用 PIL 对其进行修改。 python可以吗!?

【问题讨论】:

  • 你能更清楚你想要什么吗?喜欢列表中单个字母的图像?下面的答案能解决你的问题吗?
  • 我想将列表中的字母更改为图像,以便我可以使用 PIL 修改从字母转换后的图像。更准确地说,也许我可以说将字符串更改为图像!?我希望将列表中的文本“A”“B”“C”转换为图像文件。
  • 好吧,实际上有一种方法包括制作屏幕、以大字体显示字母、截取该窗口并保留对该图像的引用。这会是你想要的吗?
  • 是的,没错!如果我得到所有字体的最高像素化图像,然后使用该字体转换为图像(可能通过截屏),那么我可以将该图像调整为不同的大小。我发现截取每个字母的屏幕截图很麻烦,并且想知道使用 python 代码的任何快捷方法。我正在使用它来制作验光师使用的视力表。所以我需要将字母调整为不同的大小(以毫米为单位)并将其显示在屏幕上。
  • 我猜下面使用ImageDraw的方法是你要找的

标签: python image tkinter text python-imaging-library


【解决方案1】:

您可以使用ImageDraw 来创建您想要的图像:

from tkinter import *
import random
from PIL import Image, ImageTk, ImageDraw, ImageFont

win = Tk()
win.geometry('200x200')

alphabets = ["A", "B", "C"]
rand_alpha = random.choice(alphabets)

image = Image.new('RGB', (200, 200), (255, 255, 255))  # adjust the size to what you want
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('arial.ttf', size=128) # adjust the font and size to what you want
w, h = draw.textsize(rand_alpha, font=font)
draw.text(((200-w)//2, (200-h)//2), font=font, text=rand_alpha, fill='black')
tkimage = ImageTk.PhotoImage(image)

lbl = Label(win, image=tkimage)
lbl.pack()

win.mainloop()

使用方法参考ImageDraw文档。

【讨论】:

    【解决方案2】:

    我用tkinterPIL 模块编写了一个示例。为了更好地理解,我在代码中添加了几条注释。

    代码:

    from tkinter import *
    from PIL import Image, ImageDraw, ImageTk
    import random
    
    win = Tk()
    win.geometry("200x200")
    
    alphabets = ["A", "B", "C"]
    rand_alpha = random.choice(alphabets)
    
    img = Image.new("RGB", (100, 100), color="white")  # Create a new 100x100 white image
    d = ImageDraw.Draw(img)  # Create Draw instance
    d.text((50, 50), rand_alpha, fill=(255, 0, 0))  # Render the text to the image at 50x50 position with red color.
    
    render = ImageTk.PhotoImage(img)  # Rendering picture to TK
    img = Label(win, image=render)  # Insert picture to Label widget.
    img.image = render
    img.place(x=0, y=0)  # Place the picture to the left-top corner
    
    
    win.mainloop()
    

    界面:

    【讨论】:

    • 如果我是正确的GUI中的W和h的图像是从imgs目录中随机选择的并放入img标签中吧!?如果我不想(或拥有)从目录中导入的图像,而是使用文本(随机生成的文本)并将文本转换为图像,该怎么办。
    • 好的,我明白你想要什么。我已根据您的评论更新了我的答案。现在代码执行您在评论中描述的操作!
    【解决方案3】:

    当然可以:

    from tkinter import *
    import random
    from PIL import Image, ImageTk
    
    img = ImageTk.PhotoImage(Image.open("Yourimage"))
    win = Tk()
    win.geometry('200x200')
    
    alphabets = ["A", "B", "C"]
    rand_alpha = random.choice(alphabets)
    
    lbl = Label(win, font='Ariel 30', text=rand_alpha, image=img)
    lbl.pack()
    
    win.mainloop()
    

    如果你不想让你的图片覆盖你的文字,你需要使用compound=[CENTER,BOTTOM,TOP,LEFT,RIGHT]

    【讨论】:

    • 很抱歉,答案告诉我要插入新图像,但我想将 rand_alpha 生成的文本转换为图像并将该文本用作图像。
    猜你喜欢
    • 1970-01-01
    • 2021-11-02
    • 2016-11-27
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    相关资源
    最近更新 更多