在python中添加图片时出错
我是python新手,正在使用python 3.5版本。我想将photo添加到python中,下面是我编写的代码:在python中添加图片时出错,python,tkinter,python-imaging-library,python-3.5,Python,Tkinter,Python Imaging Library,Python 3.5,我是python新手,正在使用python 3.5版本。我想将photo添加到python中,下面是我编写的代码: from tkinter import * win=Tk() win.title("Python Image") canvas=Canvas(win,width=500,height=500) canvas.pack() my_image=PhotoImage(file="C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.
from tkinter import *
win=Tk()
win.title("Python Image")
canvas=Canvas(win,width=500,height=500)
canvas.pack()
my_image=PhotoImage(file="C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg")
canvas.create_image(0,0,anchor=NW,image=my_image)
win.mainloop()
但当我运行它时,出现了以下错误:
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
================ RESTART: C:\Users\LABE-2\Desktop\rakibul.py ================
Traceback (most recent call last):
File "C:\Users\LABE-2\Desktop\rakibul.py", line 8, in <module>
my_image=PhotoImage(file="C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg")
File "C:\Users\LABE-2\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3539, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\LABE-2\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3495, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"
>>>
win32上的Python 3.6.4(v3.6.4:d48eceb,2017年12月19日,06:04:45)[MSC v.1900 32位(英特尔)]
有关详细信息,请键入“copyright”、“credits”或“license()”。
>>>
================重新启动:C:\Users\LABE-2\Desktop\rakibul.py================
回溯(最近一次呼叫最后一次):
文件“C:\Users\LABE-2\Desktop\rakibul.py”,第8行,在
my_image=PhotoImage(file=“C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg”)
文件“C:\Users\LABE-2\AppData\Local\Programs\Python\Python36-32\lib\tkinter\\ uuu init\uuuu.py”,第3539行,在\uu init中__
图像。_u初始(自我,“照片”,名称,cnf,主机,**千瓦)
文件“C:\Users\LABE-2\AppData\Local\Programs\Python\Python36-32\lib\tkinter\\ uuuu init\uuuu.py”,第3495行,在\uu init中__
self.tk.call(('image','create',imgtype,name,)+选项)
_tkinter.TclError:无法识别图像文件“C:\Users\Public\Pictures\Sample Pictures\Desert.jpg”中的数据
>>>
Photoimage本机只能加载
png
和pgm&ppm
图像()
您可以通过加载其他图像格式。对于python3,请如下使用:
from PIL import Image, ImageTk
from tkinter import Tk,Canvas,NW
win=Tk()
win.title("Python Image")
canvas=Canvas(win,width=500,height=500)
canvas.pack()
# use your path here ...
my_image = ImageTk.PhotoImage(Image.open(r"some.jpg"))
canvas.create_image(0,0,anchor=NW,image= my_image )
win.mainloop()
您也可以在中找到所有这些信息。的可能副本