Изменить размер изображения PIL: ValueError: Неизвестный фильтр повторной выборки - PullRequest
0 голосов
/ 05 октября 2019

Итак, я пытаюсь создать настольный интерфейс в Python с Tkinter, и я пытаюсь установить обои, но я не знаю, как их изменить. Вот код:

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)

1 Ответ

0 голосов
/ 05 октября 2019

Вопрос : как изменить размер изображения в 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()
...