Skip to content

Instantly share code, notes, and snippets.

@nico-i
Last active May 16, 2022 11:01
Show Gist options
  • Save nico-i/61765334ab3dd56e35d8c1d2f54567e9 to your computer and use it in GitHub Desktop.
Save nico-i/61765334ab3dd56e35d8c1d2f54567e9 to your computer and use it in GitHub Desktop.
Copy nested images into an output directory
import os
import shutil
import sys
from tqdm import tqdm
work_path = '.'
if len(sys.argv) > 1:
work_path = sys.argv[1]
output_dir = os.path.abspath('./all_files/')
if len(sys.argv) > 2:
output_dir = sys.argv[2]
new_files_name = 'IMG'
if len(sys.argv) > 3:
new_files_name = sys.argv[3]
script_file_name = os.path.basename(__file__)
file_counter = 1
copy_tuples = []
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for root, dirs, files in os.walk(work_path):
if script_file_name in files or output_dir == os.path.abspath(root):
continue
for file in files:
base_name, extension = os.path.splitext(file)
if extension not in ['.jpeg', '.jpg', '.png', '.gif', '.tiff', '.RAW', '.ARW']:
continue
new_file_path = os.path.join(
output_dir, new_files_name + '_' + str(file_counter) + extension)
copy_tuples.append((os.path.join(root, file), new_file_path))
file_counter += 1
for copy_tuple in tqdm(copy_tuples, desc='Copying files: '):
shutil.copy(copy_tuple[0], copy_tuple[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment