简体   繁体   中英

Python script to add path to the images

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: 在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM