Python Forum
how to resize image in canvas tkinter
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to resize image in canvas tkinter
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("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"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))

buran write Feb-05-2021, 07:05 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
You have to resize the image itself, then redisplay

import tkinter as tk
import os


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)
here's an example where the author is using a label resizing an image dynamically

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 557 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 4,211 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  tkinter.TclError: can't invoke "canvas" command cybertooth 8 5,927 Feb-23-2023, 06:58 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,411 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 1,602 Jul-22-2022, 10:26 AM
Last Post: menator01
  [Tkinter] canvas image problem DPaul 4 6,420 Nov-24-2021, 07:06 AM
Last Post: DPaul
  [PyQt] Can't get MDIarea to resize automatically with Main Window JayCee 4 3,456 Aug-02-2021, 08:47 PM
Last Post: JayCee
  How to redraw an existing image to a different image TkInter zazas321 6 5,852 Jul-08-2021, 07:44 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 5,596 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  [Tkinter] Clickable Rectangles Tkinter Canvas MrTim 4 8,802 May-11-2021, 10:01 PM
Last Post: MrTim

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020