在 Tkinter 中調整 PIL 中的圖片大小


Python 提供用於影像處理的 Pillow 或 PIL 軟體包,用於在應用程式中載入、處理和自定義影像。它具有許多屬性,例如影像顏色、影像字型、調整影像大小、載入影像等。

為了調整應用程式中影像的大小,我們可以使用resize(width, height)方法。該方法可以在應用程式中載入影像後呼叫。為了在應用程式中開啟影像,我們必須在筆記本中匯入軟體包,例如:

from PIL import Image, ImageTk

示例

在以下示例中,我們將影像大小調整為“300x225”。

#Import tkinter library
from tkinter import *
from PIL import Image, ImageTk
#Create an instance of tkinter frame
win= Tk()
#Set the Geometry
win.geometry("750x250")
#Open a New Image
image= Image.open("tutorialspoint.png")
#Resize Image using resize function
resized_image= image.resize((300,225), Image.ANTIALIAS)
#Convert the image into PhotoImage
img = ImageTk.PhotoImage(resized_image)
#Create a label for the image
Label(win,image= img).pack(pady=20)
win.mainloop()

輸出

執行以上程式碼將顯示一個視窗,其中包含一張大小為“300x225”的圖片。

更新於:2021 年 4 月 16 日

676 次瀏覽

開啟您的職業

完成課程認證

入門
廣告