0
votes

Message File Name
Syntax Error
(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: >truncated \UXXXXXXXX escape C:\visualexample.py

I've Imported PIL and tkinter, and tried a few different things, but the one I see mostly suggested is:

img = ImageTk.PhotoImage(Image.open("C:\testpic.gif"))

or

img = tk.PhotoImage(Image.open("C:\testpic.gif"))

Have also tried image file types: .bmp .jpeg .png .gif....put them into a label and pack or place.

I have also got another error msg that I don't have at hand, but it was something like:

Invaild '____str____' value type: 'JpegImageFormat' is not a recognised format.

When this one comes up, it also does it for all picture file types.

I think i'm just going to have to pretty up my program with plain old solid colour backgrounds and font colours. Maybe the PIL Lib is the wrong one (I had to get it separate from python 3.3), I thought I got the right one.

Maybe theres a file size(Mb's) limit?

If anyone has any suggestions as to how to put a picture file in the background that would be great!

edit: python 3x, import tkinter and from PIL import ImageTk, Image

suggestion worked for .gif .bmp .png .jpg :)

img = PIL.ImageTk.PhotoImage(PIL.Image.open(picfilepath))

2
Please add more code -- what modules you've imported, how you've used them etc. - Parviz Karimli
Are you on Python 3.x or 2.x? - Parviz Karimli
Backslash is an escape character. Double them up. e.g. C:\\testpic.gif - Hobbes
@Hobbes: or use forward slashes, which have been supported in all mainstream OS's for quite a while. - Bryan Oakley

2 Answers

3
votes

I had also problems with PIL and Tkinter Images... I had been searching for hours when I found a solution. Try this, I hope it will help you, this simple program runs a label with an image on it:

from tkinter import *
import PIL.Image
import PIL.ImageTk

root = Tk()

image = PIL.ImageTk.PhotoImage(PIL.Image.open ("C:/Users/Vladi/example.jpg"))  
label = Label (root, image = image).pack() 

root.mainloop()

I've just specified all the libraries where I took the modules from. And take also care to the backslashes... Using those which you have used, you will get an error. Use this one: / or: \\

If you want to resize it:

img = PIL.Image.open ("C:/Users/Vladi/example.jpg")
img = img.resize ((80, 75))
img = PIL.ImageTk.PhotoImage(self.im)

You have to resize it before calling PhotoImage, you can also write it all in a line:

img = PIL.ImageTk.PhotoImage(PIL.Image.open ("C:/Users/Vladi/example.jpg").resize((80, 75)))

I hope I've helped you

1
votes

It seems like PIL has serious problems. I think it doesn't support Python 3 yet at all (https://mail.python.org/pipermail/image-sig/2009-March/005498.html). But I also tried it on Python 2.7.11, and couldn't show any image. If you're trying to show an image on Tkinter, I would suggest you try this:

from tkinter import *
root = Tk()

imgobj = PhotoImage(file="filename.png")

label = Label(root, image=imgobj)
label.pack()

It also supports gif image format, but doesn't support jpg type.