1

I want to read multiple .jpg images, that are in 3 separate folders. The 3 folders are on the same path. I tried to do it like this:

path1 = os.path.abspath('Type_1')
path2 = os.path.abspath('Type_2')
path3 = os.path.abspath('Type_3')
folder = os.path.join(path1, path2, path3)

def load_images_from_folder(folder):
    images = []
    for filename in os.listdir(folder):
        if filename.endswith(".jpg"):
            img = cv2.imread(os.path.join(folder, filename))
            if img is not None:
                images.append(img)
            return images

print(load_images_from_folder(folder))

But it only returns the last path and not all of them. I also tried to use relative paths, such as:

path1 = os.path.relpath('Type_1')
path2 = os.path.relpath('Type_2')
path3 = os.path.relpath('Type_3')
folder = os.path.join(os.path.sep, path1, path2, path3)

but still the same problem. Can someone help with this?

0

3 Answers 3

4

Your return images line is inside your loop, so it will return as soon as it finds any matching result. You only want it to return after the whole loop as finished.

Reduce the indentation of the return statement so that it lies after the loop instead of inside it.

def load_images_from_folder(folder):
    images = []
    for filename in os.listdir(folder):
        if filename.endswith(".jpg"):
            img = cv2.imread(os.path.join(folder, filename))
            if img is not None:
                images.append(img)
    return images

[Edit]

If you want to look in multiple adjacent folders, you want something like this:

root_folder = '[whatever]/data/train'
folders = [os.path.join(root_folder, x) for x in ('Type_1', 'Type_2', 'Type_3')]
all_images = [img for folder in folders for img in load_images_from_folder(folder)]

That will call load_images on each folder, and put all the results into one list.

Sign up to request clarification or add additional context in comments.

4 Comments

actually i realized that it still doesn't work. when i print(len(load_images_from_folder(folder))) it returns the number of images located at path3 and not at folder
NameError: name 'folders' is not defined
Also, if I want to define path1 as a path referring to a parent directory, is this way correct, or is there a more flexible/smart solution? path1 = os.path.abspath('..\\data\\train\\Type_1')
@joasa My code does not produce name 'folders' is not defined'. It's defined right there.
3

If I understand the problem correctly, your file structure looks as follows:

- Type1
    - Image1.jpg
    - Image2.jpg
- Type2
    - Image1.jpg
    - Image2.jpg
- Type3
    - Image1.jpg
    - Image2.jpg

If this is true, then the os.path.join call is errant (it'll result in a string that reads "Type1/Type2/Type3" which achieves nothing for you).

I think the code you're looking for is as follows:

def load_images_from_folder(folder):
    images = []
    for filename in os.listdir(folder):
        if any([filename.endswith(x) for x in ['.jpeg', '.jpg']]):
            img = cv2.imread(os.path.join(folder, filename))
            if img is not None:
                images.append(img)
    return images

folders = [
    'Type1',
    'Type2',
    'Type3',
]

for folder in folders:
    images = load_images_from_folder(folder)
    # your code that does something with the return images goes here

2 Comments

Ok, I tried that but when I do print(len(images)) it returns: 1 1 1 (vertically) instead of the total number of images in folder. I guess it means that it sees only one image per path? But why is that?
Ah, my bad there- the return is indented too far. Drop that back so it's inline with the for (as per your original code snippet) and it should be good.
0

I know this is really old, but this worked for me recently.

 def create_dataset(img_folder):
    img_data_array=[]
    class_name=[]

    for dirl in os.listdir(img_folder):
        for file in os.listdir(os.path.join(img_folder,dirl)):
        
            if any([file.endswith(x) for x in ['.jpeg', '.jpg']]):
            

                 image_path=os.path.join(img_folder,dirl,file)
                 image=cv2.imread(image_path,cv2.COLOR_BGR2RGB)
             
                 img_data_array.append(image)
                 class_name.append(dirl)
    return img_data_array,class_name


img_data, class_name =create_dataset(train_folder)
        

Comments

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.