我正在嘗試調整我在 Tkinter 視窗中插入的影像的大小,但一直收到此錯誤訊息:“AttributeError: 'PhotoImage' object has no attribute 'resize'” 這是我調整影像大小的代碼:
self.path = 'S:/?ffentliche Ordner/Logos/Core Solution/Logo/CoreSolution_Logo_RGB.jpg'
self.img = ImageTk.PhotoImage(Image.open(self.path))
self.resized = self.img.resize(50,50)
self.new_img = ImageTk.PhotoImage(self.resized)
self.label = Label(master, image = self.new_img)
self.label.pack()
self.Main = Frame(self.master)
如何解決此錯誤?歡迎并感謝所有幫助。
uj5u.com熱心網友回復:
與本教程一樣,將檔案作為影像匯入似乎更容易。然后調整它的大小,然后將其轉換為 PhotoImage。你可以試一試嗎?
# Import the required libraries
from tkinter import *
from PIL import Image, ImageTk
# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the tkinter window
win.geometry("700x350")
# Load the image
image=Image.open('download.png')
# Resize the image in the given (width, height)
img=image.resize((450, 350))
# Conver the image in TkImage
my_img=ImageTk.PhotoImage(img)
# Display the image with label
label=Label(win, image=my_img)
label.pack()
win.mainloop()
https://www.tutorialspoint.com/resizing-images-with-imagetk-photoimage-with-tkinter
uj5u.com熱心網友回復:
據我所知,Pillow Image.PhotoImage 類用于在 tkinter 中顯示,但沒有 tkinter.PhotoImage 類的所有方法。
最簡單的方法是在轉換為 Pillow Image.PhotoImage 之前調整 Pillow.Image 的大小。
from tkinter import *
from PIL import Image, ImageTk
master = Tk()
path = 'images/cleese.png'
img = Image.open(path)
img.thumbnail((50,50)) # Resize Pillow Image
new_img = ImageTk.PhotoImage(img) # Convert
label = Label(master, image=new_img)
label.pack()
master.mainloop()
uj5u.com熱心網友回復:
嘗試這個。我沒有測驗。
path = Image.open('S:/?ffentliche Ordner/Logos/Core Solution/Logo/CoreSolution_Logo_RGB.jpg'
self.resized = path.resize{(50,50),Image.ANTIALIAS)
self.img = ImageTk.PhotoImage(self.resized)
self.label = Label(master, image = self.new_img)
self.label.pack()
self.Main = Frame(self.master)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/490117.html
標籤:Python tkinter python成像库 图像大小调整
上一篇:更改函式中的物件
