How to put cropped image on Tkinter canvas in Python

I found many similar questions, but not quite a solution to this case: I want

  • Download image file from disk
  • Trim (lazy or not)
  • Put it on TKinter canvas

And yes, it would be better if at the first stage there were no gif file, but even if it should be, I will be happy. What is it..

I can upload the file, I can crop it (in PIL), I can place it on the canvas (in TKinter), but I cannot combine it. (So ​​maybe a simple PIL translation for TKinter is enough?) Of course, I'm new to TKinter.

+2
source share
1 answer

There is a ImageTkmodule PIL.

from Tkinter import *
from PIL import Image, ImageTk

root = Tk()
canvas = Canvas(root, width=500, height=500)
canvas.pack()

im = Image.open("image.png")
cropped = im.crop((0, 0, 200, 200))
tk_im = ImageTk.PhotoImage(cropped)
canvas.create_image(250, 250, image=tk_im)

root.mainloop()
+4
source

All Articles