簡體   English   中英

如何根據Python中列表中的文件名將圖像文件分類到兩個文件夾中?

[英]How to sort image files into two folders based on filename from a list in Python?

我有一個包含狗的圖像的文件夾,名為dogID-X.jpg,其中X是屬於一個dogID的圖片編號,例如0a08e-1.jpg,0a08e-2.jpg,0a08e-3.jpg表示有屬於同一只狗的三個圖像。 如何基於僅具有dogID [0a08e,4a45t,...]的兩個列表將這些圖像分為兩個子文件夾,即,具有一個列表ID的所有圖像都應進入一個文件夾,而來自另一個列表的所有圖像應進入一個文件夾。另一個文件夾。 謝謝! 列表如下所示: 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))
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))

好吧,假設您有lis1和lis2作為僅包含dogID的2個列表,還有一個包含所有圖像的文件夾,我將其稱為“ mypath”,子文件夾將分別命名為“ lis1”和“ 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)

我希望它能解決您的問題。

嘗試這個,

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)

希望能幫助到你!

您可以使用shutil 只需將dst用作文件目的地(基於列表)。

from shutil import copyfile

copyfile(src, dst)

如果您顯示到目前為止已編寫的代碼,我可以提供進一步的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM