我想為每個類創建包含相關影像的子檔案夾。現在,我有一個包含所有影像的檔案夾。我創建了一個帶有 images_name 和 Complexity(標簽)的 .csv。我正在尋找根據 CSV 檔案中提到的復雜性(標簽)將所有影像移動到子目錄的最佳方法。
我的檔案夾結構如下所示:
- 檔案夾
- 圖片
- 檔案格式
我想創建一個子檔案夾并根據復雜性(標簽)移動所有影像
- 子檔案夾
- 基本的
- 緩和
- 復雜的
import pandas as pd
df =pd.read_csv("DF.csv")
df.head()
| 指數 | 標題 | 原料 | 影像名稱 | 成分計數 | 復雜 |
|---|---|---|---|---|---|
| 1 | 脆皮鹽和胡椒土豆 | ['2 個大蛋白'、'1 磅新土豆(直徑約 1 英寸)'、'2 茶匙粗鹽'、'? 茶匙細磨黑胡椒粉'、'1 茶匙切碎迷迭香'、'1 茶匙細碎切碎的百里香','1茶匙切碎的歐芹'] | 脆鹽和胡椒土豆丹克魯格 | 6 | 基本的 |
| 2 | 感恩節通心粉和奶酪 | ['1杯淡奶','1杯全脂牛奶','1茶匙。大蒜粉','1茶匙。洋蔥粉','1茶匙。煙熏辣椒粉','? 茶匙。新鮮黑胡椒粉','1茶匙。猶太鹽,加更多”,“2 磅超鋒利切達干酪,粗磨碎”,“4 盎司”。全脂奶油芝士','1磅肘通心粉'] | 感恩節奶酪通心粉埃里克威廉姆斯 | 11 | 緩和 |
| 3 | 意大利香腸和面包餡 | ['1(? 至 1 磅)圓形意大利面包,切成 1 英寸立方體(8 杯)','2 湯匙橄欖油,分開','2 磅意大利甜香腸,去除腸衣,分開',' 1 支無鹽黃油,切成小塊”,“3 個中等大小的洋蔥,切碎”,“4 個大芹菜排骨,切碎”,“5 個蒜瓣,切碎”,“4 個大雞蛋,打散”,“? 杯濃奶油,分開的”、“? 杯火雞內臟湯或低鈉雞湯”、“1 杯磨碎的 Parmigiano-Reggiano(2 盎司)”、“? 杯粗切平葉歐芹”、“4 夸脫淺陶瓷或玻璃烘焙盤子'] | 意大利香腸和面包餡240559 | 22 | 復雜的 |
任何幫助,將不勝感激。謝謝。
uj5u.com熱心網友回復:
基本上,OP 需要兩個功能:一個是創建子檔案夾,一個是移動影像。
在一切都需要將 CSV 作為資料框讀取之前。有關如何執行此操作的更多資訊,請閱讀:將CSV 檔案匯入為 Pandas DataFrame
由于我無權訪問該檔案,因此我將從 OP 共享表中創建一個資料框示例:
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']})
現在,讓我們從第一個函式開始。我們將給它一個名稱create_subfolders,假設讀取csv檔案時生成的資料框被稱為df,并且列名與上面的相同,它應該如下所示(注釋使其不言自明)
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)
然后,為了根據Complexity資料框列中提到的將影像移動到子檔案夾,我們將創建一個名為move_images. 它應該如下所示(注釋使其不言自明)
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')
可能需要做一些調整,因為我這里沒有影像,所以前面的函式只是一個例子。例如,如果一種格式具有.png或其他格式,則應更改擴展名。
uj5u.com熱心網友回復:
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}/")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/505831.html
上一篇:影像按鈕在布局上放大/裁剪?
下一篇:受約束的影像按鈕影像太放大了
