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

时间:2019-10-05 10:44:42

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

因此,我尝试使用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枕头调整它的大小,但是它不起作用。

这是错误:

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)

2 个答案:

答案 0 :(得分: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 :(得分:0)

实际上很简单

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

您会看到.resize也不适用于ImageTk图像对象。.resize需要宽度和高度的元组 试试这个

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