-1

I have cats images in a directory.I want to apply data augmentation manually to these images.

My directory is Downloads/Images/Cats/.
This directory has images with two types of labels/names:

  1. cat1_001.png, cat1_002.png, ...
  2. cat2_001.png, cat2_002.png, ...

specifying two types of cat images.

So how can I iterate through the images with label 1 only from the directory in python?

martineau
  • 119,623
  • 25
  • 170
  • 301
Shiv
  • 144
  • 9
  • `for f in glob.glob('Downloads/Images/Cats/*001.png'): ...` – jordanm Aug 25 '20 at 18:14
  • 1
    Or maybe `cat1_*.png`, depending exactly what "label 1" is meant to mean. Putting an example in questions is generally helpful when a description might be ambiguous. – alani Aug 25 '20 at 18:15

1 Answers1

1

You can do it like this:

import os
directory_in_str = 'Downloads/Images/Cats/'
directory = os.fsencode(directory_in_str)
    
for file in os.listdir(directory):
    filename = os.fsdecode(file)
    if(filename.startswith('cat1_') and filename.endswith('.png')):
        print(filename)
        # do your staff here
xszym
  • 928
  • 1
  • 5
  • 11
  • Instead of testing if cat1_ is in filename, I would test if filename starts with cat1_. We don't know the name convention and this should be more accurate. – Nelala_ Aug 25 '20 at 18:28
  • Good point :) I edited anwser – xszym Aug 25 '20 at 18:34