Pillow won't open an image in the same directory. "FileNotFoundError: [Errno 2] No such file or directory:"

Pillow won't open an image in the same directory. "FileNotFoundError: [Errno 2] No such file or directory:"

pillow 库不会再在与脚本相同的目录中打开图像,而以前是这样。我没有改变任何与文件加载方式有关的内容。我当然在测试脚本的文件夹中有 jpg。

我已经尝试导入所有这三个,因为我读到它有效:

from PIL import Image
import PIL.Image
import PIL

None 个有效,输出为

FileNotFoundError: [Errno 2] No such file or directory: 'pic.jpg'

加载图片的代码是:

def main(new_width=300):
    
    try:
        image = PIL.Image.open("pic.jpg")
    except:
        print("Please rename the file to 'pic.jpg' and restart the script")
        quit()

尝试使用图像的绝对路径

from PIL import Image

try:
    image = Image.open("/path/to/pic.jpg")
except:
    print("Please rename the file to 'pic.jpg' and restart the script")
    quit()