调整PIL图像大小:ValueError:未知的重采样过滤器

3

我正在尝试使用Tkinter在Python中创建类似桌面的界面,并且我正在尝试设置壁纸,但是我不知道如何调整其大小。以下是代码:

from tkinter import *
import tkinter.messagebox as box
import webbrowser
from PIL import Image, ImageTk

window=Tk()
window.title('Label Example')
window.configure(background = 'gray44')

#---=Main_Frame=---#
main_frame = Frame(window)
main_frame.pack(padx = 600, pady=350)

#---=Wallpaper=---#
img_wallpaper = ImageTk.PhotoImage(Image.open('minecraft main picture.gif').resize(10, 10)) # the one-liner I used in my app
label_w = Label(window, image=img_wallpaper)
label_w.image = img_wallpaper # this feels redundant but the image didn't show up without it in my app
label_w.pack()
##wallpaper_image = PhotoImage(file = 'minecraft main picture.gif')
##wallpaper = Label(window, image= wallpaper_image, width=400, height = 400)
##wallpaper_image_big = PhotoImage.subsample(wallpaper_image, x=1, y=1)
##can_wallpaper = \
##Canvas(window, width = 1200, height = 700)
##can_wallpaper.create_image((100, 100), image = wallpaper_image)
##can_wallpaper.place(x=0, y =0)
window.mainloop() #Main loop

我尝试使用别人的代码在PIL pillow中调整大小,但它并没有起作用。
以下是错误信息:
Traceback (most recent call last):
  File "/Users/edwardandreilucaciu/Desktop/Desktop Interface Project/Desktop Interface.py", line 16, in <module>
    img_wallpaper = ImageTk.PhotoImage(Image.open('minecraft main picture.gif').resize(10, 10)) # the one-liner I used in my app
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/PIL/Image.py", line 1865, in resize
    message + " Use " + ", ".join(filters[:-1]) + " or " + filters[-1]
ValueError: Unknown resampling filter (10). Use Image.NEAREST (0), Image.LANCZOS (1), Image.BILINEAR (2), Image.BICUBIC (3), Image.BOX (4) or Image.HAMMING (5)

***label_w.image = img_wallpaper # this feels redundant***:tkinterOOP API,如果在一个继承了class ImageLabel(tk.Label):的类中使用它,就不必“分配两次”。阅读PIL.Image.Image.resize - stovfl
@stovfl:需要在某个地方保留图像的引用,否则图像将被垃圾回收。在实例化“Label”时将其分配给“image”属性是不够的。这是tkinter的一个怪癖。 - Bryan Oakley
当你说你尝试了别人的代码,但它没能运行时,"不工作"是什么意思?程序崩溃了吗?它将其调整为了错误的大小?还是其他原因造成的? - Bryan Oakley
3
值错误:未知的重采样滤波器(10):该参数必须是元组类型,请查看我的回答,并更改为resize((10, 10)) - stovfl
2个回答

0
问题:如何在Tkinter中调整图像大小。
import kinter as tk
from PIL import Image, ImageTk

class ImageLabel(tk.Label):
    def __init__(self, parent, **kwargs):
        path = kwargs.pop('path', None) 
        if path is not None:
            image  = Image.open(path) 

            resize = kwargs.pop('resize', None)
            if resize is not None:
                image = image.resize(resize, Image.LANCZOS) 

            # Keep a reference to prevent garbage collection
            self.photo = ImageTk.PhotoImage(image)
            kwargs['image'] = self.photo

        super().__init__(parent, **kwargs)

用法:

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        lab=ImageLabel(self, 
                       path="minecraft main picture.gif", 
                       resize=(400, 400))
        lab.grid()

if __name__ == '__main__':
    App().mainloop()

-1

这其实很容易

img_wallpaper = ImageTk.PhotoImage(Image.open('minecraft main picture.gif').resize(10, 10))

你看,对于ImageTk图像对象,.resize不可用,而且.resize需要一个宽度和高度的元组 试试这个

img_wallpaper = Image.open('minecraft main picture.gif').resize((10,10))
img_wallpaper = ImageTk.PhotoImage(img_wallpaper)

不起作用。ImageTk没有Resize函数。它会产生这个错误:ValueError: Unknown resampling filter (10)。请使用Image.NEAREST(0)、Image.LANCZOS(1)、Image.BILINEAR(2)、Image.BICUBIC(3)、Image.BOX(4)或Image.HAMMING(5)。 - i_shoot_photos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接