【问题标题】:Struggling to resize an image within Tkinter努力在 Tkinter 中调整图像大小
【发布时间】:2016-01-25 08:43:07
【问题描述】:

我为我的 Sixth Form Computing 课程开发了一个基于控制台的冒险游戏,现在想将它迁移到 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(人们说它已被弃用,但我设法安装得很好?)但到目前为止还没有运气。

任何帮助表示赞赏。 杰克

编辑: 该问题已标记为重复,但我已经tried to use it,但回答的人似乎打开了一个文件,将其保存为“.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()

现在得到这个:

【问题讨论】:

  • 原始 PIL 不再维护(并且与 Python 3、IIRC 不兼容)。但是,有一个名为Pillow 的 PIL 分支,这可能是您安装的。您的代码中的 PhotoImage() 函数需要 PIL/Pillow 才能运行,因此如果您的代码在 Python 3 上运行,您可以放心,您已经安装了有效的 Pillow。 :) 您可以使用 Pillow Image.resize 方法调整图像大小。
  • @PM2Ring 我正在努力寻找在哪里添加Image.resize 位。这似乎是我想要的,但我将如何调整代码?我尝试将.resize(0.5) 作为测试添加到我打开文件的行的末尾,但我被告知PhotoImage object has no attribute resize。显然这是因为我需要对 Image 对象执行此操作,但是如何操作?
  • 您必须分阶段进行,如链接duplicate target 中接受的答案所示。首先将文件加载为 PIL Image。然后调整它的大小。然后将调整大小的 PIL Image 转换为 PhotoImage。一旦您查看了我在之前的评论中链接到的 PIL / Pillow 文档,这可能会更有意义。这些文档并不出色,但您至少应该大致了解一下它们,以了解 PIL 可以做什么。
  • @PM2Ring 尝试调整我的代码,请参阅上面的编辑 2。
  • 如果您尝试过某事但没有成功,您需要说明您这样做了,以及您尝试了什么。最初的问题只是解释了您的程序,并在“如何在 PIL 中调整大小?”末尾添加了一点注释。上面链接的重复目标解释了如何做到这一点。

标签: python python-3.x tkinter python-imaging-library


【解决方案1】:

对于 python 2,你可以做这样的事情,它应该也适用于 python 3 在 import 的小变化之后

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()

【讨论】:

  • 我们可以通过zoom = zX, zYimage.resize(tuple([x*y for (x,y) in zip([x for x in image.size], zoom)]))的代码更改将缩放从一维扩展到二维
【解决方案2】:

您可以在将它们与您的应用程序绑定之前对它们进行预处理,而不是即时调整这些巨大图像的大小。我把“钥匙”和“锁胸”的图片放在“图标”子目录中,然后运行以下代码:

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”和“locked-chest-small.png”,您可以在应用程序中使用它们来代替原始图像。

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多