छवियों के साथ काम करने के लिए, पायथन लाइब्रेरी पिलो या पीआईएल पैकेज प्रदान करती है जो अनुप्रयोगों को छवियों को आयात करने और उन पर विभिन्न संचालन करने में सक्षम बनाता है।
मान लीजिए कि हम एक छवि को गतिशील रूप से उसकी विंडो में बदलना चाहते हैं। ऐसे मामले में, हमें इन चरणों का पालन करना होगा -
-
टिंकर एप्लिकेशन में छवि खोलें।
-
कैनवास विजेट बनाएं और create_image(**options) . का उपयोग करें लोड की गई छवि को कैनवास में रखने के लिए।
-
लोड की गई छवि का आकार बदलने के लिए एक फ़ंक्शन परिभाषित करें।
-
फ़ंक्शन को पैरेंट विंडो कॉन्फ़िगरेशन के साथ बाइंड करें।
उदाहरण
# Import the required libraries
from tkinter import *
from PIL import ImageTk, Image
# Create an instance of Tkinter Frame
win = Tk()
# Set the geometry of Tkinter Frame
win.geometry("700x450")
# Open the Image File
bg = ImageTk.PhotoImage(file="tutorialspoint.png")
# Create a Canvas
canvas = Canvas(win, width=700, height=3500)
canvas.pack(fill=BOTH, expand=True)
# Add Image inside the Canvas
canvas.create_image(0, 0, image=bg, anchor='nw')
# Function to resize the window
def resize_image(e):
global image, resized, image2
# open image to resize it
image = Image.open("tutorialspoint.png")
# resize the image with width and height of root
resized = image.resize((e.width, e.height), Image.ANTIALIAS)
image2 = ImageTk.PhotoImage(resized)
canvas.create_image(0, 0, image=image2, anchor='nw')
# Bind the function to configure the parent window
win.bind("<Configure>", resize_image)
win.mainloop() को कॉन्फ़िगर करने के लिए फंक्शन को बाइंड करें
आउटपुट
उपरोक्त कोड को चलाने से एक विंडो प्रदर्शित होगी जिसमें एक छवि होगी जिसे गतिशील रूप से आकार दिया जा सकता है।
