How to put a cropped image on a Tkinter Canvas in Python

后端 未结 1 368
太阳男子
太阳男子 2020-12-19 23:52

I found a lot of similar questions, but not quite the solution to this case : I want to

  1. Load an image file from disk
  2. Crop it (lazy or not)
  3. P
相关标签:
1条回答
  • 2020-12-20 00:04

    There is ImageTk module in 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()
    
    0 讨论(0)
提交回复
热议问题