Home > other >  Creating Subfolders on bases of Image Name and Complexity in CSV Python
Creating Subfolders on bases of Image Name and Complexity in CSV Python

Time:09-06

I want to create subfolders with related images for each class. Right now, I have a folder with all images. I created a .csv with images_name and Complexity (label). I am looking for the best ways to move all images to subdirectories according to Complexity(label) mentioned in the CSV file.

My folder structure looks like this:

  • Folder
    • Images
    • DF.csv

I would like to create a subfolder and move all images according to complexity (labels)

  • Subfolder
    • Basic
    • Moderate
    • Complex
    import pandas as pd
    df =pd.read_csv("DF.csv")
        df.head()
index Title Ingredients Image_Name Ingredient Count Complexity
1 Crispy Salt and Pepper Potatoes ['2 large egg whites', '1 pound new potatoes (about 1 inch in diameter)', '2 teaspoons kosher salt', '¾ teaspoon finely ground black pepper', '1 teaspoon finely chopped rosemary', '1 teaspoon finely chopped thyme', '1 teaspoon finely chopped parsley'] crispy-salt-and-pepper-potatoes-dan-kluger 6 Basic
2 Thanksgiving Mac and Cheese ['1 cup evaporated milk', '1 cup whole milk', '1 tsp. garlic powder', '1 tsp. onion powder', '1 tsp. smoked paprika', '½ tsp. freshly ground black pepper', '1 tsp. kosher salt, plus more', '2 lb. extra-sharp cheddar, coarsely grated', '4 oz. full-fat cream cheese', '1 lb. elbow macaroni'] thanksgiving-mac-and-cheese-erick-williams 11 Moderate
3 Italian Sausage and Bread Stuffing ['1 (¾- to 1-pound) round Italian loaf, cut into 1-inch cubes (8 cups)', '2 tablespoons olive oil, divided', '2 pounds sweet Italian sausage, casings removed, divided', '1 stick unsalted butter, cut into pieces', '3 medium onions, chopped', '4 large celery ribs, chopped', '5 garlic cloves, minced', '4 large eggs, lightly beaten', '¾ cup heavy cream, divided', '½ cup turkey giblet stock or reduced-sodium chicken broth', '1 cup grated Parmigiano-Reggiano (2 ounces)', '½ cup coarsely chopped flat-leaf parsley', '4-qt shallow ceramic or glass baking dish'] italian-sausage-and-bread-stuffing-240559 22 Complex

Any help would be appreciated. Thanks.

CodePudding user response:

Basically what OP needs is two functions: one to create the subfolders, and one to move the images.

Before everything one needs to read the CSV as a dataframe. For more information on how to do that, read this: Import CSV file as a Pandas DataFrame

As I don't have access to the file, I will create an example of a dataframe, from the table OP shared:

import pandas as pd
import os

df = pd.DataFrame({'Title': ['Crispy Salt and Pepper Potatoes', 'Thanksgiving Mac and Cheese', 'Italian Sausage and Bread Stuffing'],
                     'Ingredients': [['2 large egg whites', '1 pound new potatoes (about 1 inch in diameter)', '2 teaspoons kosher salt', '¾ teaspoon finely ground black pepper', '1 teaspoon finely chopped rosemary', '1 teaspoon finely chopped thyme', '1 teaspoon finely chopped parsley'],
                                        ['1 cup evaporated milk', '1 cup whole milk', '1 tsp. garlic powder', '1 tsp. onion powder', '1 tsp. smoked paprika', '½ tsp. freshly ground black pepper', '1 tsp. kosher salt, plus more', '2 lb. extra-sharp cheddar, coarsely grated', '4 oz. full-fat cream cheese', '1 lb. elbow macaroni'],
                                        ['1 (¾- to 1-pound) round Italian loaf, cut into 1-inch cubes (8 cups)', '2 tablespoons olive oil, divided', '2 pounds sweet Italian sausage, casings removed, divided', '1 stick unsalted butter, cut into pieces', '3 medium onions, chopped', '4 large celery ribs, chopped', '5 garlic cloves, minced', '4 large eggs, lightly beaten', '¾ cup heavy cream, divided', '½ cup turkey giblet stock or reduced-sodium chicken broth', '1 cup grated Parmigiano-Reggiano (2 ounces)', '½ cup coarsely chopped flat-leaf parsley', '4-qt shallow ceramic or glass baking dish']],
                        'Image_Name': ['crispy-salt-and-pepper-potatoes-dan-kluger', 'thanksgiving-mac-and-cheese-erick-williams', 'italian-sausage-and-bread-stuffing-240559'],
                        'Ingredient Count': [6, 11, 22],
                        'Complexity': ['Basic', 'Moderate', 'Complex']})

Now, let's begin with the first function. We will give it a name create_subfolders, and, assuming the dataframe generated when one reads the csv file is called df, and the column names are equal to the one's above, it should look like the following (the comments make it self-explanatory)

def create_subfolders(df):
    # Get the unique values of Complexity
    complexity = df['Complexity'].unique()
    # Create a subfolder for each unique value of Complexity
    for i in complexity:
        os.mkdir(i)

Then, in order to move the images to the subfolders according to the Complexity mentioned in the dataframe column, we will create a new function called move_images. It should look like the following (the comments make it self-explanatory)

def move_images(df):
    # Get the unique values of Complexity
    complexity = df['Complexity'].unique()
    # Move images to subfolders according to Complexity(label) mentioned in the CSV file.
    for i in complexity:
        # Get the images with Complexity(label) = i
        images = df.loc[df['Complexity'] == i, 'Image_Name']
        # Move images to subfolders according to Complexity mentioned in the CSV file.
        for j in images:
            os.rename(j   '.jpg', i   '/'   j   '.jpg')

One might have to do a few tweaks, as I don't have here the images, so the previous function is just an example. For example, if one has .png or other formats, one should change the extension.

CodePudding user response:

import os 
import pandas as pd


def create_subfolder(path):
    if not os.path.exists(path):
        os.makedirs(path)
    return path


df = pd.read_csv("DF.csv")
data = df.values

# Subfolder name
SUB_FOLDER = create_subfolder("Subfolder") 

for item in data:
    # file name without extension
    file_name = item[2] 

    # label folder in <subfolder>
    sub_folder = create_subfolder(os.path.join(SUB_FOLDER, item[4])) 

    # move file to <subfolder>/<labelfolder>
    os.system(f"mv Images/{file_name}.* {sub_folder}/") 
  • Related