Python 2.7 opens several files from a directory other than the standard (for opencv)

I am 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 python path by default, which is equal C:\Users\Myname. But how do I manage to browse another directory to open images? those. D:\MyPicLib.

Meanwhile, I don't want to change the default directory, because all my python modules are stored in C:\Users\Myname. I just want to control the connection of images inD:\MyPicLib

After this part, you can also help me view not one, but several images (in a specific format, for example, just .jpg in the directory) for the while / while loop?

Thanks in advance, the problem seems easy, but despite all my efforts, I have not found a solution to change the python path by default.

0
source share
1 answer

Is this what you are looking for? I recommend looking for some tutorials that show the basic features of os and os.path. These are very useful tools. Here is the first 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)
0
source

Source: https://habr.com/ru/post/1613355/


All Articles