Image does not open with python imaging library

后端 未结 1 1372
独厮守ぢ
独厮守ぢ 2021-01-27 18:44

I have just installed the Python imaging library and started to learn it but I cannot sem to open the image file.

    import Image
    im = Image.open(\"Desert.j         


        
相关标签:
1条回答
  • 2021-01-27 19:10

    Use an absolute path. You can get the path of the script to create that 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 an absolute path) are opened relative to the current working directory, and that depends entirely on how you started the script and your operating system defaults.

    The __file__ variable is the filename of the current module, the os.path calls ensure it is a full absolute path (so including the drive when on Windows, starting with a / on POSIX systems), from which we then take just the directory portion.

    0 讨论(0)
提交回复
热议问题