- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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
Embedding an Image in a Tkinter Canvas widget using PIL
The Pillow library in Python contains all the basic image processing functionality. It is an open-source library available in Python that adds support to load, process, and manipulate the images of different formats.
Let's take a simple example and see how to embed an Image in a Tkinter canvas using Pillow package (PIL). Follow the steps given below −
- Import the required libraries and create an instance of tkinter frame.
from tkinter import * from PIL import Image, ImageTk
Set the size of the frame using root.geometry method.
Next, create a Canvas widget using canvas() function and set its height and width.
Open an image using Image.open() and then convert it to an PIL image using ImageTk.PhotoImage(). Save the PIL image in a variable "img".
Next, add the PIL image to the Canvas using canvas.create_image().
Finally, run the mainloop of the application window.
# 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()
When you run this code, it will produce the following output window −
- How to open PIL Image in Tkinter on Canvas?
- How to make a Button using the Tkinter Canvas widget?
- How to update an image in a Tkinter Canvas?
- How to insert an image in a Tkinter canvas item?
- How to center an image in canvas Python Tkinter
- How to Move an Image in Tkinter canvas with Arrow Keys?
- Resizing pictures in PIL in Tkinter
- How do I get the background color of a Tkinter Canvas widget?
- How to update the image of a Tkinter Label widget?
- Embedding a matplotlib animation into a tkinter frame
- How to draw a png image on a Python tkinter canvas?
- How to resize an image using Tkinter?
- How do I use PIL with Tkinter?
- How to disable an Entry widget in Tkinter?