How to resize an image using tkinter?

by arnoldo.moen , in category: Third Party Scripts , a month ago

How to resize an image using tkinter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by deron , a month ago

@arnoldo.moen 

To resize an image using tkinter in Python, you can use the PIL (Pillow) library to open and resize images. Here is an example code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from PIL import Image, ImageTk
import tkinter as tk

# Create a tkinter window
root = tk.Tk()

# Open the image file
image = Image.open("image.jpg")

# Resize the image to a specific size (e.g. 200x200)
image = image.resize((200, 200), Image.ANTIALIAS)

# Convert the image for tkinter display
tk_image = ImageTk.PhotoImage(image)

# Create a label to display the resized image
label = tk.Label(root, image=tk_image)
label.pack()

# Run the tkinter main loop
root.mainloop()


Make sure to replace "image.jpg" with the file path of the image you want to resize. This code will open the image, resize it to 200x200 pixels using bilinear interpolation, and display the resized image in a tkinter window.