如何调整图像大小以适合标签尺寸? (Python)

2024-01-09

我的目标是创建一个随机国家生成器,并且将出现所选国家的国旗。然而,如果图像文件大于标签的预定尺寸,则仅显示图像的一部分。有没有办法调整图像大小以适合标签? (我见过的所有其他类似问题都已得到解答,提到了 PIL 或 Image 模块。我测试了它们,它们都出现了这个错误:

回溯(最近一次调用最后一次): 文件“C:\python\country.py”,第 6 行,位于 导入PIL 导入错误:没有名为“PIL”的模块

这是我的代码,如果有帮助的话:

import tkinter
from tkinter import *
import random

flags = ['England','Wales','Scotland','Northern Ireland','Republic of Ireland']

def newcountry():

    country = random.choice(flags)
    flagLabel.config(text=country)
    if country == "England":
        flagpicture.config(image=England)
    elif country == "Wales":
        flagpicture.config(image=Wales)
    elif country == "Scotland":
        flagpicture.config(image=Scotland)
    elif country == "Northern Ireland":
        flagpicture.config(image=NorthernIreland)
    else:
        flagpicture.config(image=Ireland)

root = tkinter.Tk()
root.title("Country Generator")

England = tkinter.PhotoImage(file="england.gif")
Wales = tkinter.PhotoImage(file="wales.gif")
Scotland = tkinter.PhotoImage(file="scotland.gif")
NorthernIreland = tkinter.PhotoImage(file="northern ireland.gif")
Ireland = tkinter.PhotoImage(file="republic of ireland.gif")
blackscreen = tkinter.PhotoImage(file="black screen.gif")

flagLabel = tkinter.Label(root, text="",font=('Helvetica',40))
flagLabel.pack()

flagpicture = tkinter.Label(root,image=blackscreen,height=150,width=150)
flagpicture.pack()

newflagButton = tkinter.Button(text="Next Country",command=newcountry)
newflagButton.pack()

除了仅显示图像的一部分之外,该代码运行得非常好。有没有办法在代码本身中调整图像大小?(我使用的是Python 3.5.1)


如果你没有安装PIL,首先需要安装

pip install pillow

安装完成后,您现在可以从 PIL 导入:

from PIL import Image, ImageTk

Tk 的 PhotoImage 只能显示 .gif,而 PIL 的 ImageTk 可以让我们在 tkinter 中显示各种图像格式,并且 PIL 的 Image 类有一个resize我们可以使用方法来调整图像大小。

我把你的代码删减了一些。

您可以调整图像大小,然后只需配置标签,标签将扩展到图像的大小。如果您给标签指定了特定的高度和宽度,可以说height=1 and width=1然后将图像大小调整为 500x500,然后配置小部件。由于您已显式设置这些属性,它仍会显示 1x1 标签。

在下面的代码中,修改字典,在迭代字典时修改它是不行的。 dict.items() 返回字典的副本。

有多种方法可以做到这一点,我只是觉得听写在这里很方便。

链接到超出高度/宽度限制的图像 https://media.giphy.com/media/fts60w6387eus/giphy.gif- 小猫.gif

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

WIDTH, HEIGHT = 150, 150
flags = {
    'England': 'england.gif',
    'Wales': 'wales.gif', 
    'Kitty': 'kitty.gif'
}

def batch_resize():

    for k, v in flags.items():
        v = Image.open(v).resize((WIDTH, HEIGHT), Image.ANTIALIAS)
        flags[k] = ImageTk.PhotoImage(v)

def newcountry():

    country = random.choice(list(flags.keys()))
    image = flags[country]
    flagLabel['text'] = country
    flagpicture.config(image=image)

if __name__ == '__main__':

    root = Tk()
    root.configure(bg='black')

    batch_resize()

    flagLabel = Label(root, text="", bg='black', fg='cyan', font=('Helvetica',40))
    flagLabel.pack()

    flagpicture = Label(root)
    flagpicture.pack()

    newflagButton = Button(root, text="Next Country", command=newcountry)
    newflagButton.pack()
    root.mainloop()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何调整图像大小以适合标签尺寸? (Python) 的相关文章