0

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))

4
  • Please add more code -- what modules you've imported, how you've used them etc. Commented Jul 19, 2016 at 20:08
  • Are you on Python 3.x or 2.x? Commented Jul 19, 2016 at 20:17
  • 1
    Backslash is an escape character. Double them up. e.g. C:\\testpic.gif Commented Jul 19, 2016 at 20:23
  • @Hobbes: or use forward slashes, which have been supported in all mainstream OS's for quite a while. Commented Jul 19, 2016 at 21:19

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to accept the answer if it solved your problem ;)
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.