John Johns
John Johns

Reputation: 51

Not being able to load an image in python

I am trying to load an image into a canvas in python. However I am getting the error: TclError: couldn't recognize data in image file "C:\testimage\spongesea.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

I can open the image manually from the file, so it is not corrupted, and it is calling the correct place. Any ideas? Thanks

Upvotes: 0

Views: 78

Answers (1)

furas
furas

Reputation: 143187

PhotoImage works only with GIF and PGM/PPM.

You have to use Image, ImageTk to work with other formats

from PIL import Image, ImageTk

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

BTW: read PhotoImage and see "Garbage Collection problem" in note.

Upvotes: 1

Related Questions