แพ็คเกจ PIL หรือ Pillow ใน Python มีวิธีการประมวลผลภาพในโปรแกรม เราสามารถเปิดรูปภาพ ปรับแต่งรูปภาพเพื่อการใช้งานที่แตกต่างกัน และสามารถใช้รูปภาพเพื่อแสดงข้อมูลได้เช่นกัน ในการใช้แพ็คเกจ PIL ใน Tkinter คุณต้องติดตั้งไลบรารี Python Pillow ในสภาพแวดล้อมของคุณ
วิธีติดตั้งหมอน เพียงพิมพ์ pip install pillow . เมื่อติดตั้งสำเร็จแล้ว คุณสามารถนำเข้าโมดูลในโปรเจ็กต์ของคุณและนำไปใช้เพื่อการใช้งานต่อไปได้
ตัวอย่าง
ในตัวอย่างนี้ เราได้แสดงรูปภาพในวิดเจ็ต canvas โดยใช้แพ็คเกจ Python Pillow
# 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 window
win.geometry("700x350")
# Create a canvas widget
canvas=Canvas(win, width=700, height=350)
canvas.pack()
# Load the image
img=ImageTk.PhotoImage(file="opera.jpg")
# Add the image in the canvas
canvas.create_image(350, 200, image=img, anchor="center")
win.mainloop() ผลลัพธ์
หากเรารันโค้ดข้างต้น มันจะแสดงรูปภาพในหน้าต่าง
