无法在python中加载图像

2024-04-26 11:58:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python将图像加载到画布中。但是我得到了一个错误:TclError:无法识别图像文件“C:\testimage”中的数据\海绵菌.jpg““

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

我可以从文件中手动打开图像,因此它没有损坏,并且调用的位置正确。有什么想法吗?谢谢


Tags: thefrom图像imageimportgametkinter画布
1条回答
网友
1楼 · 发布于 2024-04-26 11:58:33

PhotoImage仅适用于GIFPGM/PPM。你知道吗

您必须使用ImageImageTk来处理其他格式

from PIL import Image, ImageTk

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

顺便说一句:阅读PhotoImage并查看注释中的“垃圾收集问题”。你知道吗

相关问题 更多 >