1

I have the following script wherein I add the path to the images in TEST_IMAGE_PATHS. My input is "test_images" folder. In "test_images" folder images are named as image0, image1, image2 etc.

Script to add images in test_images to the path is:

PATH_TO_TEST_IMAGES_DIR = 'test_images'
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpeg'.format(i)) for i in range(5, 7) ]

This script outputs: enter image description here

How do I modify this script such that:

  1. I can input any type of images(not just limited to jpeg) as mentioned above. Type of image include JPG, png, jpg, etc

  2. Images should not be named as image0, image1, image2, image3, image4, image5, image6 etc. It should not be restricted to this naming convention and should pick up all the images in the 'test_images' folder

I referred How can I iterate over files in a given directory?. as suggested, however this link explains how to reference files in the folder and add them to the path. My question has to do with modifications to do to the file after being added to the path. I am able to add files to the path using above script. I need to attach multiple extensions of images. to the existing path

Ajinkya
  • 1,797
  • 3
  • 24
  • 54
  • 1
    Possible duplicate of [How can I iterate over files in a given directory?](https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory) – ddg Feb 28 '18 at 23:09
  • using shutil.move would move the image to whatever directory if that's what you're trying to figure out. – Cleve Green Mar 01 '18 at 16:22
  • I tried PATH_TO_TEST_IMAGES_DIR = 'test_images' TEST_IMAGE_PATHS = os.listdir(PATH_TO_TEST_IMAGES_DIR) . this lists all the images in the path but this is not what I want I want all the images to join to the current path as shown in the original script in the question – Ajinkya Mar 01 '18 at 16:27

1 Answers1

0
PATH_TO_TEST_IMAGES_DIR = 'test_images'
TEST_IMAGE_PATHS = []
for ext in ('*.png', '*.jpeg'):
    TEST_IMAGE_PATHS.extend(glob(join(PATH_TO_TEST_IMAGES_DIR, ext)))

solves the issue

Ajinkya
  • 1,797
  • 3
  • 24
  • 54