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)
(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 Assignmentdatasetstart.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.
Do not rely on os.getcwd for relative path. Reason is shown below.
Instead, use pathlib.Path
. For i.e. think of these structure.
x:/
├ A
┃ ├ 1.png
┃ ├ 2.png
┃ ├ 3.png
┃ ├ 4.png
┃ └ 5.png
┃
└ B
└ script.py
If you want to access reliably from script.py
to 1.png, you’d better use __file__
to determine path of the script – as current working directory can differ from actual script location as shown in below example output.
import pathlib
import os
print("Current cwd: ", os.getcwd())
# Get current script's folder
script_location = pathlib.Path(__file__).parent
# get data folder
data_location = script_location.parent.joinpath("A")
# single file example
png_file = data_location.joinpath("1.png")
# alternatively can iterate thru directory
for file_ in data_location.iterdir():
# print full path in linux style
print(f"{file_.absolute().as_posix()}")
(Accessing file in different path in absolute path)
[email protected]/D: py X:Bscript.py
Current cwd: D:
X:/A/1.png
X:/A/2.png
X:/A/3.png
X:/A/4.png
X:/A/5.png
(Change directory)
[email protected]/D:
❯ cd x:
(Now use indirect path)
[email protected]/X: py .Bscript.py
Current cwd: X:
X:/A/1.png
X:/A/2.png
X:/A/3.png
X:/A/4.png
X:/A/5.png
See how cwd – current working directory – differ for os.getcwd
for each run.
However, no matter where you access that script either by indirectly or directly, outside or inside folder B, you can reliably access files relatively using __file__
, .parent
and .joinpath
methods of pathlib.Path
objects.
Check out documents of pathlib
here, as I see you’re Korean, this is linked to korean document.