8

I have folder of images named ImageData which has 5000 sub-folders each sub-folder contains 2-3 images. I want to retrieve these images and store them at one place/folder.How can i do this by using python.

Zunaira Akmal
  • 95
  • 1
  • 1
  • 5
  • Possible duplicate of [How to get files in a directory, including all subdirectories](https://stackoverflow.com/questions/954504/how-to-get-files-in-a-directory-including-all-subdirectories) – Harshil Shah Apr 06 '19 at 19:27
  • A couple questions. When moving the images into another folder, do you want them to remain in a duplicate sub-folder? Also, do you want to delete the original folder of images? – Will Lacey Apr 06 '19 at 19:27

5 Answers5

4

I'm not 100% sure I got what you mean by "store them", but as I wanted to help regardless I'll go ahead by assuming that you want to copy them to a different path. In particular, if I understood correctly, you have the following folder structure:

├── ImageData
│   ├── dir_1
│   │   ├── img_1
│   │   ├── img_2
│   ├── dir_2
│   │   ├── img_1
│   │   ├── img_2

and so forth, and you don't care about saving the name of the subdirectories, but you simply want to move all these images to one unique directory with no substructure.

If my assumptions are correct, then a solution that comes to my mind might be the following:

import glob 
import shutil

    
destination_path = "/path/to/your/oneplace_folder/"
pattern = "/path/to/your/ImageData/*/*"  
for img in glob.glob(pattern):
    shutil.copy(img, destination_path)

the two wildcards are for the subdirectories below ImageData and the images within them, respectively. Additionally, if the images contain metadata that you want to preserve whilst copying them, then you might want to use copy2() as specified in the documentation

Fabrizio Miano
  • 493
  • 3
  • 11
2

You could use a solution from here: Recursive sub folder search and return files in a list python to get a list of all the files.

And then loop through that list and copy each file to the main directory with a solution from here: How to move a file in Python

You would end up with something like this

import glob
import os

# Location with subdirectories
my_path = "Images/"
# Location to move images to
main_dir = "ImagesMain/"

# Get List of all images
files = glob.glob(my_path + '/**/*.jpg', recursive=True)

# For each image
for file in files:
    # Get File name and extension
    filename = os.path.basename(file)
    # Copy the file with os.rename
    os.rename(
        file,
        main_dir + filename
    )
Tom
  • 685
  • 8
  • 17
2

Another Possible Solution:

import os
import cv2

directory = 'ImageData'
new_directory = 'NewImageData'

# If dir does not exist otherwise delete next line
os.mkdir(new_directory)

def copy_images():
    for file_name in os.listdir(directory):
        sub_dir_path = directory + '/' + file_name
        if (os.path.isdir(sub_dir_path)):
            for image_name in os.listdir(sub_dir_path):
                if image_name[-4:] == '.jpg':
                    img = cv2.imread(image_name)
                    copied_image_path = new_directory + '/' + image_name
                    cv2.imwrite(copied_image_path, img)

copy_images()

This code will copy images and save them to a newly created target directory. It does not retain sub-folder hierarchy.

Will Lacey
  • 91
  • 6
1

Modified the answer in How to get files in a directory, including all subdirectories for this use case.

import os

# Type of image in the folder for filtering eg: '.jpg' , '.png'etc.
TYPE_OF_IMAGE = '.jpg'

#List of folders to process.
folder_list =['fld1','fld2']

for fld in folder_list:
  for dirpath, dirnames, filenames in os.walk(fld):
    for filename in [f for f in filenames if f.endswith(".log")]:
      # os.path.join(dirpath, filename) gives the fullrelative path with folder_list
      print(os.path.join(dirpath, filename))
KRG
  • 655
  • 7
  • 18
0

A little change to above answer: Since sometimes file with same name might exist, checking is important.

import glob
import os
import shutil

# Location with subdirectories
my_path = "Source/"
# Location to move images to
main_dir = "Dest/"

# Get List of all images
files = glob.glob(my_path + '/**/*.jpg', recursive=True)

# For each image
for file in files:
    # Get File name and extension
    filename = os.path.basename(file)
    filename=filename[:-4].replace(".", "-") + ".jpg"
    #print(filename)
    # Copy the file with os.rename
    if not os.path.exists(os.path.join(main_dir, filename)):
        os.rename(file, os.path.join(main_dir, filename))