0

I have made this code that displays an image after resizing it

import tkinter as tk
from PIL import Image, ImageTk

def resize(picname, ratio):
     image = Image.open(picname)
     image = image.resize((int(image.size[0]*ratio), int(image.size[1]*ratio)), Image.ANTIALIAS)
     image = ImageTk.PhotoImage(image)
     return image

swidth, sheight = 1366, 900

root = tk.Tk()
root.geometry(str(swidth)+'x'+str(sheight)+'+0+0')

cv = tk.Canvas(root, width = swidth, height = sheight)
cv.pack()

tempimage = resize('man.gif', 1.5)
imaj = cv.create_image(0, 0, image = tempimage, tag ='man')

while True:
    cv.coords('man', 500, 500)
    print(cv.coords(imaj))
    cv.update()

tk.mainloop()

But this code does not display the picture Even when I check the output I get [500.0, 500.0]

This code works without the 'resize', any idea why?

EDIT:

I got the resizing code from this link, from the answer by Joshkunz. This code works when I have one image object. But not when I have multiple, for some reason. Could someone please explain!

  • 2
    You need to keep a reference to the image returned by `resize`. `image = resize('man.gif', 1.5)`. Then `imaj = cv.create_image(0, 0, image=image, tag='man')` will work. – unutbu Jul 31 '18 at 15:30
  • I tried doing it but it still doesn't work – ѕняєє ѕιиgнι Aug 01 '18 at 15:48
  • I notice you've updated your question with "This code works when I have one image object. But not when I have multiple". Please update your code to show this as well. That will help us identify the problem. – unutbu Aug 01 '18 at 18:06
  • 1
    Nevermind, I was actually rendering the other objects on top of this image. Thanks a lot, and sorry for wasting your time – ѕняєє ѕιиgнι Aug 02 '18 at 12:19

0 Answers0