पायथन में Pillow लाइब्रेरी में सभी बुनियादी इमेज प्रोसेसिंग कार्यक्षमता होती है। यह पायथन में उपलब्ध एक ओपन-सोर्स लाइब्रेरी है जो विभिन्न फॉर्मेट की इमेज को लोड, प्रोसेस और मैनिपुलेट करने के लिए सपोर्ट जोड़ती है।
आइए एक सरल उदाहरण लेते हैं और देखते हैं कि Pillow पैकेज (PIL) का उपयोग करके Tkinter Canvas में इमेज कैसे एम्बेड करें। नीचे दिए गए चरणों का पालन करें:
चरण
- आवश्यक लाइब्रेरी इम्पोर्ट करें और Tkinter फ्रेम का एक इंस्टेंस बनाएं।
from tkinter import * from PIL import Image, ImageTk
root.geometry मेथड का उपयोग करके फ्रेम का आकार सेट करें।
अगला, canvas() फ़ंक्शन का उपयोग करके Canvas विजेट बनाएं और इसकी height और width सेट करें।
Image.open() का उपयोग करके इमेज खोलें और फिर ImageTk.PhotoImage() का उपयोग करके इसे PIL इमेज में बदलें। PIL इमेज को वेरिएबल "img" में सेव करें।
अगला, canvas.create_image() का उपयोग करके PIL इमेज को Canvas में जोड़ें।
अंत में, एप्लिकेशन विंडो का mainloop चलाएं।
उदाहरण
# Import the required Libraries from tkinter import * from PIL import Image, ImageTk # Create an instance of tkinter frame root = Tk() # Set the geometry of tkinter frame root.geometry("700x450") # Create a canvas widget canvas= Canvas(root, width=600, height=400) canvas.pack() # Load an image img=ImageTk.PhotoImage(Image.open("camels.jpg")) # Add image to the Canvas Items canvas.create_image(250, 250, anchor=CENTER, image=img) root.mainloop()आउटपुट
जब आप यह कोड चलाते हैं, तो यह निम्नलिखित आउटपुट विंडो देगा:
