【问题标题】:Pillow : File not found枕头:找不到文件
【发布时间】:2017-04-13 15:34:19
【问题描述】:

我想在 tkinter 的画布上使用图像,但我无法用 Pillow 打开图像。事实上,我将所有图像与我的代码放在同一个文件夹中,但是当我放入“icon.png”时它不起作用。然后,当我输入图像的完整路径 (C:/Users/myName/Desktop/PythonWork/game/src/icon.png) 时,它就可以工作了。

File "F:\Python\lib\site-packages\PIL\Image.py", line 2312, in open fp = builtins.open(filename, "rb") FileNotFoundError: [Errno 2] No such file or directory: 'icon.png'

因此我的问题是:如何使相对路径起作用?

提前感谢您的帮助:)

【问题讨论】:

  • 相对路径 are 工作 - 路径相对于当前工作目录,而不是脚本所在的目录。见stackoverflow.com/q/918154/7432
  • @BryanOakley 他的意思是相对于有效点的脚本

标签: python python-3.x tkinter tkinter-canvas


【解决方案1】:

如果你想引用和Image.py同目录的文件,把这个放到Image.py中:

import os

# get the directory path of the current python file
my_path = os.path.dirname(__file__)

然后,您可以将要访问的文件的名称附加到该路径。

【讨论】:

    猜你喜欢
    • 2014-01-07
    • 1970-01-01
    • 2015-07-17
    • 2020-01-24
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多