2

Code to show image is giving a syntax error when I use the Address to the image.

from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

Scare = Tk()
Scare.title('?????')
Countdown = 2
CountTotal = 2
CountTotal = IntVar()

def CountdownWork():
    global Countdown
    if Countdown > 0:
        Countdown = Countdown -1
        CountTotal.set(Countdown)
        Scare.after(1000, CountdownWork)
    else:
        ImageAddress = 'C:\Users\KINSLED\Desktop\New folder\ScareTest.jpg'
        ImageItself = Image.open(ImageAddress)
        ImageNumpyFormat = np.asarray(ImageItself)
        plt.imshow(ImageNumpyFormat)
        plt.draw()
        plt.pause(5) # pause how many seconds
        plt.close()



Count = Label(Scare, font=('arial', 10, 'bold'), textvariable=CountTotal, 
bd=30, bg='SeaGreen1', justify='right').grid(row=7,columnspan=8)

CountdownWork()

Scare.mainloop()

The Syntax Error is highlighting the space just after the equals in ImageAdress.

The Error is:

(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated\UXXXXXXXX escape

Community
  • 1
  • 1
Praemium
  • 23
  • 1
  • 4
  • Does this answer your question? [Why do I get a SyntaxError for a Unicode escape in my file path?](https://stackoverflow.com/questions/18084554/why-do-i-get-a-syntaxerror-for-a-unicode-escape-in-my-file-path) – mkrieger1 Jun 17 '22 at 09:36

3 Answers3

3

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

I believe the error is referencing your adress, specifically the special character "\" in it. You cannot use "\" in your string as it will escape the string. You could try using "\\" in your address, I think this should work.

Please see here for futher reading on the subject: http://www.pitt.edu/~naraehan/python2/tutorial7.html

CodeMonkey123
  • 1,046
  • 13
  • 22
  • Thanks I didn't know that the slash would interfere. It works now. – Praemium Oct 31 '17 at 08:45
  • OK, but this also occurs in comments, e.g. `''' C:\Users '''`. Yet, it doesn't occur with comments starting with '#', e.g. `# C:\Users'. This looks insane, since comments starting with ''' should be just ignored, like those starting with '#'! – Apostolos Mar 15 '22 at 08:56
0

The error lies in the way you've typed the file path. Windows uses backslashes \ to separate files and directories in filenames, but any time the interpreter sees these special characters, it looks for an unicode escape sequnce, like \n. To insert a backslash you need to insert \\, one slash to trigger the escape sequence and another to indicate the backslash itlesf as the desired special character.

Your assignment then becomes

ImageAddress = 'C\\:Users\\KINSLED\\Desktop\\New folder\\ScareTest.jpg'

which does't throw any error on my simulation.

mencucci
  • 180
  • 1
  • 10
0

Instead of using \ use / in python. This way you can overcome this

(Unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated\UXXXXXXXX escape" error
MjH
  • 1,170
  • 1
  • 8
  • 16
balaji
  • 1