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
  [PyQt] Can't get MDIarea to resize automatically with Main Window JayCee 4 323 Aug-02-2021, 08:47 PM
Last Post: JayCee
  How to redraw an existing image to a different image TkInter zazas321 6 516 Jul-08-2021, 07:44 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 743 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  [Tkinter] Clickable Rectangles Tkinter Canvas MrTim 4 1,233 May-11-2021, 10:01 PM
Last Post: MrTim
  [Tkinter] Draw a grid of Tkinter Canvas Rectangles MrTim 5 1,001 May-09-2021, 01:48 PM
Last Post: joe_momma
Thumbs Up tkinter canvas; different page sizes on different platforms? philipbergwerf 4 858 Mar-27-2021, 05:04 AM
Last Post: deanhystad
  tkinter button image Nick_tkinter 4 1,133 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  Latex image too big on tkinter using sympy 4096 1 977 Nov-05-2020, 08:05 AM
Last Post: DPaul
Photo Tkinter TEXT background image _ShevaKadu 5 3,140 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  tkinter: Image to Label Maryan 10 1,589 Oct-29-2020, 01:48 PM
Last Post: joe_momma

Forum Jump:

User Panel Messages

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