Wei Gong
Wei Gong

Reputation: 33

How to read the image in the a folder

Saying my .py file is in the folder named Source. I have another folder inside Source named TestImage. TestImage folder contains a file called 1.png

Source folder is opened in VS Code, and I am using cv2.imread() to read images. I know if I move 1.png into folder Source, I can do cv2.imread("1.png"). However, what should I do if I want to access 1.png in folder TestImage ?

Upvotes: 0

Views: 56

Answers (2)

Sicotfre
Sicotfre

Reputation: 38

According to VS Code documentation, the default working directory for defaults to ${workspaceFolder} (Source in your case) so that

cv2.imread("TestImage /1.png")

should work.

Upvotes: 0

SpaceKatt
SpaceKatt

Reputation: 1411

Assuming the following directory structure...

.
└── Source
    ├── TestImage
    │   └── 1.png
    └── main.py

...and you're starting the script with python main.py.

You should be able to use:

cv2.imread("./TestImage/1.png")

If you are on Windows, then you may want to use \ instead of /.

Upvotes: 2

Related Questions