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
#1
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()
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.
Reply
#2
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()
Reply
#3
here's an example where the author is using a label resizing an image dynamically
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter showing image in button rwahdan 3 107 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  [Tkinter] Clickable Rectangles Tkinter Canvas MrTim 4 362 May-11-2021, 10:01 PM
Last Post: MrTim
  [Tkinter] Draw a grid of Tkinter Canvas Rectangles MrTim 5 318 May-09-2021, 01:48 PM
Last Post: joe_momma
Thumbs Up tkinter canvas; different page sizes on different platforms? philipbergwerf 4 468 Mar-27-2021, 05:04 AM
Last Post: deanhystad
  tkinter button image Nick_tkinter 4 559 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  Latex image too big on tkinter using sympy 4096 1 591 Nov-05-2020, 08:05 AM
Last Post: DPaul
Photo Tkinter TEXT background image _ShevaKadu 5 2,185 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  tkinter: Image to Label Maryan 10 1,093 Oct-29-2020, 01:48 PM
Last Post: joe_momma
  how to rotate lines in tkinter canvas helpmewithpython 1 788 Oct-06-2020, 06:56 PM
Last Post: deanhystad
  Tkinter function to clear old canvas and start a fresh "operation" zazas321 5 1,210 Oct-01-2020, 04:16 AM
Last Post: zazas321

Forum Jump:

User Panel Messages

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