- 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
Resizing images with ImageTk.PhotoImage with Tkinter
The PIL or Pillow library in Python is used for processing images in a Tkinter application. We can use Pillow to open the images, resize them and display in the window. To resize the image, we can use image_resize((width, height) **options) method. The resized image can later be processed and displayed through the label widget.
Example
Let us have a look at the example where we will open an image and resize it to display in the window through the label widget.
# 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 tkinter window win.geometry("700x350") # Load the image image=Image.open('download.png') # Resize the image in the given (width, height) img=image.resize((450, 350)) # Conver the image in TkImage my_img=ImageTk.PhotoImage(img) # Display the image with label label=Label(win, image=my_img) label.pack() win.mainloop()
Output
Running the above code will display a resized image in the window.
Advertisements