Python Forum
how to resize image in canvas tkinter - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: how to resize image in canvas tkinter (/thread-32376.html)



how to resize image in canvas tkinter - samuelmv30 - Feb-05-2021

i am trying to resize the image to fit the window as i adjust the size. i dont know where im going wrong please help


from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk

root = Tk()


canvas_1 = Canvas(root)
canvas_1.pack(expand = True, fill = BOTH)
photo_1 = ImageTk.PhotoImage(Image.open("apps.20660.14434946597921362.9f02a0b5-6441-476c-9378-148e87e39b74.png"))
canvas_1_img = canvas_1.create_image(0,0,image = photo_1)
                  
                             
def resize(event):
    photo_2 =Image.open("apps.20660.14434946597921362.9f02a0b5-6441-476c-9378-148e87e39b74.png").resize((event.width,event.height), Image.ANTIALIAS)
    photo_can_2 = ImageTk.PhotoImage(photo_2)
    canvas_1.itemconfig(canvas_1_img, image = photo_can_2)
    
canvas_1.bind("<Configure>",lambda event: resize(event))

root.mainloop()



RE: how to resize image in canvas tkinter - Larz60+ - Feb-05-2021

You have to resize the image itself, then redisplay

import tkinter as tk
import os


os.chdir(os.path.abspath(os.path.dirname(__file__)))

root = tk.Tk()
canvas = tk.Canvas(root, width=300, height=200, bg='black')
canvas.pack(expand = tk.YES)

pimg = tk.PhotoImage(file='Peter.png')

canvas.create_image(50, 10, image=pimg)

input("press enter to enlarge image")
pimg = pimg.zoom(2, 2)
canvas.create_image(50, 10, image=pimg)

input("press enter to shrink image")
pimg = pimg.subsample(2, 2)
canvas.create_image(50, 10, image=pimg)
root.mainloop()



RE: how to resize image in canvas tkinter - joe_momma - Feb-06-2021

here's an example where the author is using a label resizing an image dynamically