0

I'm using python 2.7 on 64 bit win7 and have opencv 2.4.x.

When I write cv2.imread('pic') it opens pic in my default python path which is C:\Users\Myname. But how I will manage to browse different directory to open images? ie D:\MyPicLib.

Meanwhile, I do not want to change default directory, because all my python modules are saved in C:\Users\Myname. I just want to manage connecting pictures in D:\MyPicLib

After this part, can you also help me to browse not one but multiple images (in specific format, like just .jpg's in a directory) in for/while loop?

Thank you in advance, the problem seems easy but despite all my effort, I did not find any solution on changing default python path.

1 Answer 1

0

Is this what you are looking for? I recommend looking for some tutorials that show basic os and os.path usage. They are very useful tools. Here is the first one I found.

import os
import cv2

mypath = os.path.join('c:\\', 'asdf', 'jkl')

images = list()
for item in os.listdir(mypath):
    if '.jpg' in item:  # this could be more correctly done with os.path.splitext
        image = cv2.imread(os.path.join(mypath, item))
        if image is not None:
            images.append(image)
Sign up to request clarification or add additional context in comments.

1 Comment

it helps, thank you. so we should use os.path.join to access the files. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.