views:

164

answers:

1

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'
+4  A: 

You need to escape the backslashes:

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