Tkinter error: Couldn't recognize data in image file

后端 未结 5 1381
独厮守ぢ
独厮守ぢ 2021-01-04 01:12

I\'m trying to put a jpg image to a tkinter canvas. tkinter gives me this error:

couldn\'t recognize data in image file

I use th

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-04 01:52

    Install PIL/Pillow with:

    pip install Pillow
    

    or:

    sudo pip install pillow
    
    from PIL import Image
    from PIL import ImageTk
    import tkinter
    
    image = Image.open('bll.jpg')
    image = image.resize((20, 20))
    image = ImageTk.PhotoImage(image)
    
    canv = Canvas(root, width=80, height=80, bg='white')
    canv.grid(row=2, column=3)
    
    img = PhotoImage(file=image)
    

    Also using .PNG instead of .JPG is better for Tkinter.

提交回复
热议问题