簡體   English   中英

重命名多個圖像並使用python保存到另一個文件夾

[英]Rename multiple images and save into another folder with python

我的文件夾中有100張圖片,我想重命名所有圖片。 例如,Car.1.jpg,Car.2.jpg,Car.3.jpg等,然后將它們保存到另一個文件夾中。 我編寫了代碼,可以根據需要重命名所有圖像,但是將其保存在存在圖像的相同文件夾中。 我想重命名所有圖像,並將原始圖像名稱保留在目錄中,然后將重命名的圖像復制到另一個目錄中。

import os
from tqdm import tqdm

path = './training_data/car/'

def image_rename():
    cnt = 1
    for img in tqdm(os.listdir(path)):
        if os.path.isfile(path+img):
            filename, file_extention = os.path.splitext(path+img)
            os.rename(os.path.join(path, img), os.path.join(path, 
                       str('car.') + str(cnt) + file_extention))
       cnt +=1

image_rename()

您應該嘗試使用shutil.move()

import shutil
shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")

添加一個指向要導出文件的文件夾的變量output_path ,然后在os.rename()的seconde參數中使用此變量,如下所示:

import os
from tqdm import tqdm

path = './training_data/car/'
output_path = './training_data/output_folder/'

def image_rename():
    cnt = 1
    for img in tqdm(os.listdir(path)):
        if os.path.isfile(path+img):
            filename, file_extention = os.path.splitext(path+img)
            os.rename(os.path.join(path, img), os.path.join(output_path, 
                       str('car.') + str(cnt) + file_extention))
       cnt +=1

image_rename()

確保在系統中創建輸出文件夾(例如,使用mkdir )。

暫無
暫無

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

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