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: An image and label are not appearing. emont 7 404 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 3,975 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  tkinter.TclError: can't invoke "canvas" command cybertooth 8 5,776 Feb-23-2023, 06:58 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,301 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 1,557 Jul-22-2022, 10:26 AM
Last Post: menator01
  [Tkinter] canvas image problem DPaul 4 6,262 Nov-24-2021, 07:06 AM
Last Post: DPaul
  [PyQt] Can't get MDIarea to resize automatically with Main Window JayCee 4 3,395 Aug-02-2021, 08:47 PM
Last Post: JayCee
  How to redraw an existing image to a different image TkInter zazas321 6 5,745 Jul-08-2021, 07:44 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 5,520 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  [Tkinter] Clickable Rectangles Tkinter Canvas MrTim 4 8,666 May-11-2021, 10:01 PM
Last Post: MrTim

Forum Jump:

User Panel Messages

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