ไลบรารี PIL หรือ Pillow ใน Python ใช้สำหรับประมวลผลรูปภาพในแอปพลิเคชัน Tkinter เราสามารถใช้ Pillow เปิดภาพ ปรับขนาด และแสดงในหน้าต่างได้ ในการปรับขนาดรูปภาพ เราสามารถใช้ image_resize((width, height) **options) กระบวนการ. รูปภาพที่ปรับขนาดแล้วสามารถประมวลผลและแสดงได้ในภายหลังผ่านวิดเจ็ตป้ายกำกับ
ตัวอย่าง
มาดูตัวอย่างกันว่าเราจะเปิดรูปภาพและปรับขนาดภาพเพื่อแสดงในหน้าต่างผ่านวิดเจ็ตป้ายกำกับที่ใด
# 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() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะแสดงภาพที่ปรับขนาดในหน้าต่าง
