2016-11-03 70 views
0

我想将图像加载到python中的画布上。但是我得到的错误:TclError:在图像文件“C:\ testimage \ spongesea.jpg”无法识别的数据无法在python中加载图像

import Tkinter 
from Tkinter import * 
import time 
import inspect 
import os 
from PIL import Image, ImageTk 

class game_one: 

    def __init__(self): 

     global root 
     global canvas_one 
     root = Tk() 
     root.title(" Thanks Josh Dark 
     canvas_one = Tkinter.Canvas(root, bg="BLACK") 
     canvas_one.pack(expand= YES, fill= BOTH) 
     canvas_one.focus_set() #allows keyboard events 
     p = PhotoImage(file="C:\testimage\spongesea.jpg") 
     canvas_one.create_image(0, 0, image = p, anchor=NW) 
     root.grab_set()#I forget what this does. Don't change it. 
     root.lift()#This makes root appear in front of the other applications 

ObjectExample = game_one()# starts the animation 

我可以从文件中手动打开图像,所以它没有被破坏,它正在呼唤正确的地方。有任何想法吗?谢谢

+0

在许多语言中\具有特殊含义的字符串 - 即。 '\ t'表示'tab' - 你在路径中有'\ t'。因此,使用\\('“C:\\ ... \\ ...”)或'/'(C:/ .../...“)或前缀为”r“的原始字符串 - 'r“C:\ ... \”' – furas

回答

1

PhotoImage只适用于GIFPGM/PPM

你必须使用ImageImageTk其它格式

from PIL import Image, ImageTk 

image = Image.open("lenna.jpg") 
photo = ImageTk.PhotoImage(image) 

BTW工作:阅读PhotoImage并查看笔记 “垃圾收集问题”。

+0

谢谢。它正在工作 –