Search code examples
pythonpython-imaging-libraryfile-not-found

FileNotFoundError in PIL image with relative path and absoulute path


import PIL.Image as pilimg
import numpy as np

# Read image
im = pilimg.open("../dataset/start.jpg")

# Display image
im.show()

# Fetch image pixel data to numpy array
pix = np.array(im)

Project directory

(I'm no the author of the dataset, so I couldn't upload the whole project file.)

I'm trying to read the image with Pillow and convert it to NumPy array. However, I tried several methods such as using absolute path and methods in PIL image.open() working for some images but not others except method using OpenCV. However, it makes FileNotFound always.

The absolute path of start.jpg is F:\GIST 강의 자료\2021 1학기\EC3202 신호 및 시스템\Programming Assignment\dataset\start.jpg. and I believe the relative path to start.jpg from src/program.py is ../dataset/start.jpg.

I'm using conda 4.10.1 and python 3.8.8 and windows 10.


Solution

  • The paths are relative to the current working directory. If you are unsure what the cwd is, you can find it out by doing:

    print(f"Current working dir: {os.getcwd()})
    

    Then you can use os.path.join to concat your relative path from there

    os.path.join(os.getcwd(),'dataset/start.jpg')