How to open PIL Image in Tkinter on Canvas

后端 未结 4 1150
面向向阳花
面向向阳花 2021-02-14 18:08

I can\'t seem to get my PIL Image to work on canvas. Code:

from Tkinter import*
import Image, ImageTk
root = Tk()
root.geometry(\'1000x1000\')
canvas = Canvas(ro         


        
4条回答
  •  情深已故
    2021-02-14 18:47

    You can import multiple image formats, and resize with this code. "basewidth" sets the width of your image.

    from Tkinter import *
    import PIL
    from PIL import ImageTk, Image
    
    root=Tk()
    image = Image.open("/path/to/your/image.jpg")
    canvas=Canvas(root, height=200, width=200)
    basewidth = 150
    wpercent = (basewidth / float(image.size[0]))
    hsize = int((float(image.size[1]) * float(wpercent)))
    image = image.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(image)
    item4 = canvas.create_image(100, 80, image=photo)
    
    canvas.pack(side = TOP, expand=True, fill=BOTH)
    root.mainloop()
    

提交回复
热议问题