Image does not open with python imaging library -


i have installed python imaging library , started learn cannot sem open image file.

    import image     im = image.open("desert.jpg")     print im.format, im.size, im.mode     im.show() 

the image attributes printed fine when image opens in windows viewer, error

     file "c:\python27\lib\site-packages\pythonwin\pywin\framework\scriptutils.py"       ,line 325, in runscript                 exec codeobject in __main__.__dict__      file "c:\users\hassan\documents\imtest.py", line 2, in <module>      im = image.open('desert.jpg')      file "c:\python27\lib\site-packages\pil\image.py", line 1952, in open      fp = __builtin__.open(fp, "rb")      ioerror: [errno 2] no such file or directory: 'desert.jpg'  

i have placed file desert.jpg in same directory script saved.what should open file.

use absolute path. can path of script create path:

import os.path  script_dir = os.path.dirname(os.path.abspath(__file__)) im = image.open(os.path.join(script_dir, 'desert.jpg')) 

otherwise, relative files (anything without absolute path) opened relative current working directory, , depends entirely on how started script , operating system defaults.

the __file__ variable filename of current module, os.path calls ensure full absolute path (so including drive when on windows, starting / on posix systems), take directory portion.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -