【问题标题】:Insert an JPG image in Python using Tkinter使用 Tkinter 在 Python 中插入 JPG 图像
【发布时间】:2020-01-24 21:39:53
【问题描述】:

我正在尝试使用 Tkinter 插入图像但它不起作用,有一条错误消息说:(实际上它是说 python 无法识别图像文件中的数据)

Traceback (most recent call last):
  File "E:/Tle/ISN/Programs (Pyhton)/IMC (Widget) ULTIMATE.py", line 10, in <module>
    my_image = PhotoImage(file="C:/Users/mateo.PCMATEO/Pictures/MonCoachPersonnel.jpg")
  File "C:\Users\mateo.PCMATEO\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 3545, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\mateo.PCMATEO\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 3501, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "C:/Users/mateo.PCMATEO/Pictures/MonCoachPersonnel.jpg"

这是我输入的代码:

from tkinter import*
import tkinter as tk
window = tk.Tk()
window.title("My Personnal Fitness Coach")
window.geometry("400x500")
window.configure(background='grey')

canvas = Canvas(window, width = 100, height = 100)
canvas.pack
my_image = PhotoImage(file="C:/Users/mateo.PCMATEO/Pictures/MonCoachPersonnel.jpg")
canvas.create_image(0, 0, anchor = NW, image=my_image)

window.mainloop()

问题是我没有任何模块,除了用 python 预下载的模块,我还不想安装一些模块。那你能帮帮我吗?

【问题讨论】:

标签: python image tkinter jpeg


【解决方案1】:

如果你使用 jpg 你必须这样做

from PIL import ImageTk

然后

my_image = ImageTk.PhotoImage(file="MonCoachPersonnel.jpg")
canvas.create_image(50, 50, image=my_image, anchor=NW)

我已经简化了文件目录,注意我已经增加了图片的尺寸,从 0,0 到 50,50。

【讨论】:

  • 事实上我已经尝试过类似的东西,但它说我没有任何 PIL 模块..所以这就是我不理解的原因
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-24
  • 1970-01-01
  • 2011-07-19
  • 1970-01-01
  • 1970-01-01
  • 2014-07-17
相关资源
最近更新 更多