我已经为我的六年级计算类开发了一个基于控制台的冒险游戏,现在我想把它移植到Tkinter。主要原因是我可以使用图片,主要是来自game-icons.net的图片。
到目前为止还不错,但是这些图像质量很高,当我展示它们的时候,它们会显得很大。下面是一个示例:
代码的工作方式是使用for
循环迭代当前区域(播放机所在的)中的项列表。以下是代码:
if len(itemKeys) > 0:
l = Label(lookWindow, text="Looking around, you see the following items....\n").pack()
for x in range(0, len(itemKeys)):
icon = PhotoImage(file=("icons\\" + itemKeys[x] + ".png"))
l = Label(lookWindow, image=icon)
l.photo = icon
l.pack()
l = Label(lookWindow, text=("" + itemKeys[x].title())).pack()
l = Label(lookWindow, text=(" " + locations[position][2][itemKeys[x]][0] + "\n")).pack()
else:
l = Label(lookWindow, text="There's nothing at this location....").pack()
表示("icons\\" + itemKeys[x] + ".png")
的部分只是进入游戏目录中的icons
文件夹,并将文件名串在一起,在本例中,这将导致"key.png“,因为我们目前正在查看的项目是一个键。
然而,现在,我想调整图像的大小。我试过使用PIL (人们说PIL是不推荐的,但我安装得很好吗?)但到目前为止还没有运气。
任何帮助都很感激。杰克
编辑:问题已被标记为重复,但我已经试着用它了,但回答的人似乎打开了一个文件,将其保存为".ppm"(?)文件,然后显示它,但当我尝试,我得到一个巨大的错误,说我不能显示一个"PIL.Image.Image“。
编辑2:将其更改为:
im_temp = PILImage.open(("icons\\" + itemKeys[x] + ".png")).resize((250,250), PILImage.ANTIALIAS)
photo = PhotoImage(file=im_temp)
label = Label(lookWindow, image=photo)
label.photo = photo
label.pack()
现在拿着这个:
发布于 2015-10-26 10:33:48
而不是调整那些巨大的图像在飞行中,你可以预处理,然后用你的应用程序捆绑它们。我将“键”和“锁定的胸部”图像放在“图标”子目录中,然后运行以下代码:
from PIL import Image
import glob
for infn in glob.glob("icons/*.png"):
if "-small" in infn: continue
outfn = infn.replace(".png", "-small.png")
im = Image.open(infn)
im.thumbnail((50, 50))
im.save(outfn)
它创建了一个'key-small.png‘和’loct-box-Small.png‘,您可以在应用程序中使用它,而不是原始图像。
发布于 2018-11-06 13:27:16
对于python 2,您可以这样做,在导入的小更改之后,它也应该适用于python 3。
from tkinter import Tk, Label
from PIL import Image, ImageTk
root = Tk()
file = 'plant001.png'
image = Image.open(file)
zoom = 0.5
#multiple image zise by zoom
pixels_x, pixels_y = tuple([int(zoom * x) for x in image.size])
img = ImageTk.PhotoImage(image.resize((pixels_x, pixels_y))) # the one-liner I used in my app
label = Label(root, image=img)
label.image = img # this feels redundant but the image didn't show up without it in my app
label.pack()
root.mainloop()
https://stackoverflow.com/questions/33342545
复制相似问题