- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to open PIL Image in Tkinter on Canvas?
Pillow package (or PIL) helps a lot to process and load images in Python projects. It is a free open-source library available in Python that adds support to load, process, and manipulate the images of different formats.
In order to open the Image in Tkinter canvas, we have to first import the library using from PIL import Image, ImageTk command. To display the image in our Tkinter application, we can use the Canvas widget by specifying the image file in the create_image(x,y,image) method.
Example
#Import the required Libraries from tkinter import * from PIL import Image,ImageTk #Create an instance of tkinter frame win = Tk() #Set the geometry of tkinter frame win.geometry("750x270") #Create a canvas canvas= Canvas(win, width= 600, height= 400) canvas.pack() #Load an image in the script img= ImageTk.PhotoImage(Image.open("download.png")) #Add image to the Canvas Items canvas.create_image(10,10,anchor=NW,image=img) win.mainloop()
Output
Running the above code will display a window with an image inside the canvas widget.
Advertisements