3

I want to load an image but I get an error-message.

My code:

from PIL import Image
im = Image.open("D:\Python26\PYTHON-PROGRAMME\bild.jpg")
im.show()

I get this error:

Traceback (most recent call last):
  File "D:\Python26\PYTHON-PROGRAMME\00000000000000000", line 2, in <module>
    im = Image.open("D:\Python26\PYTHON-PROGRAMME\bild.jpg")
  File "D:\Python26\lib\site-packages\PIL\Image.py", line 1888, in open
    fp = __builtin__.open(fp, "rb")
IOError: [Errno 22] invalid mode ('rb') or filename: 'D:\\Python26\\PYTHON-PROGRAMME\x08ild.jpg'

1 Answer 1

9

You need to escape the backslashes:

im = Image.open("D:\\Python26\\PYTHON-PROGRAMME\\bild.jpg")
Sign up to request clarification or add additional context in comments.

3 Comments

Or alternatively, use a raw string like so r"D:\Python26\PYTHON-PROGRAMME\bild.jpg"
@Noufal Ibrahim: True. I tend to use escapes, but that's personal taste, I guess.
The Windows runtime will accept forward slashes, too: "D:/Python26/PYTHON-PROGRAMME/bild.jpg"

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.