Reputation: 13
I have a folder with images of dogs, named dogID-X.jpg where X is the number of the picture that belongs to one dogID, e.g. 0a08e-1.jpg, 0a08e-2.jpg, 0a08e-3.jpg means there are three images that belong to the same dog.
How do I sort these images into two subfolders based on two lists that have only the dogID [0a08e, 4a45t, ...] i.e. all images with IDs from one list should go to one folder, and all images from another list should go into the other folder. Thanks! The list looks like this: list(y_labels) = ['86e1089a3',
'6296e909a',
'5842f1ff5',
'850a43f90',
'd24c30b4b',
'1caa6fcdb', ...]
for image in list(y_labels):
folder = y_labels.loc[image, 'PetID']
old = './train_images/{}'.format(image)
new = '//train_images_new/{}/{}'.format(folder, image)
try:
os.rename(old, new)
except:
print('{} - {}'.format(image,folder))
Upvotes: 1
Views: 2570
Reputation: 11228
import os
import shutil
path = r'C:\Users\user\temp\test\dog_old' #folder where all dog images present
list_name =[]
# traverse root directory, and list directories as dirs and files as files
for root, dirs, files in os.walk(path):
list_name.extend(files)
from collections import defaultdict
dic=defaultdict(list)
for i in list_name:
filename,ext =os.path.splitext(i)
group, img_index = filename.split('-')
dic[group].append(img_index)
# folder path where new dog images had to added
new_folder = r'C:\Users\user\temp\test\dog_new'
for i in dic:
if not os.path.exists(os.path.join(new_folder,i)):
os.mkdir(os.path.join(new_folder,i))
for img in dic[i]:
old_image = os.path.join(path,'{}-{}.jpg'.format(i,img))
new_image = r'{}.jpg'.format(img)
new_path =os.path.join(new_folder,i)
shutil.move(old_image,os.path.join(new_path,new_image))
else:
for img in dic[i]:
old_image = os.path.join(path,'{}-{}.jpg'.format(i,img))
new_image = r'{}.jpg'.format(img)
new_path =os.path.join(new_folder,i)
print(new_path)
shutil.move(old_image,os.path.join(new_path,new_image))
Upvotes: 1
Reputation: 41
Try this,
import os
pet_names = ['0a08e', '0a08d']
image_ids = ["0a08e-1.jpg", "0a08e-2.jpg", "0a08e-3.jpg","0a08d-1.jpg", "0a08d-2.jpg", "0a08d-3.jpg"]
image_folder_path = os.getcwd()#"<image folder path>"
# assuming you want to name the folder with the pet name, create folders with the names in the list.
for pet_name in pet_names:
if not os.path.exists(os.path.join(image_folder_path,pet_name)):
print("creating")
os.makedirs(pet_name)
# loop over the image id's match the pet name and put it in the respective folder
for img_id in image_ids:
for pet_name in pet_names:
if pet_name in img_id:
image_full_path_source = os.path.join(image_folder_path,img_id)
dest_path = os.path.join(image_folder_path,pet_name)
image_full_path_destination = os.path.join(dest_path,img_id)
os.rename(image_full_path_source, image_full_path_destination)
Hope it helps!
Upvotes: 0
Reputation: 58
Well let's assume you have lis1 and lis2 as 2 lists containing only dogID, there is also a folder which contains all the images and I'll call it "mypath", sub folders will be named "lis1" and "lis2".
import os
# path to image folder, get all filenames on this folder
# and store it in the onlyfiles list
mypath = "PATH TO IMAGES FOLDER"
onlyfiles = [f for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath, f))]
# your list of dogID's
lis1 = ["LIST ONE"]
lis2 = ["LIST TWO"]
# create two seperate lists from onlyfiles list based on lis1 and lis2
lis1files = [i for i in onlyfiles for j in lis1 if j in i]
lis2files = [i for i in onlyfiles for j in lis2 if j in i]
# create two sub folders in mypath folder
subfolder1 = os.path.join(mypath, "lis1")
subfolder2 = os.path.join(mypath, "lis2")
# check if they already exits to prevent error
if not os.path.exists(subfolder1):
os.makedirs(subfolder1)
if not os.path.exists(subfolder2):
os.makedirs(subfolder2)
# move files to their respective sub folders
for i in lis1files:
source = os.path.join(mypath, i)
destination = os.path.join(subfolder1, i)
os.rename(source, destination)
for i in lis2files:
source = os.path.join(mypath, i)
destination = os.path.join(subfolder2, i)
os.rename(source, destination)
I hope it solves your problem.
Upvotes: 1