I have 100 images in a folder and I want to rename all of them. For example, Car.1.jpg, Car.2.jpg,Car.3.jpg so on and save them into another folder. I wrote the code which renames all the images as I want but it saves in the same folder which images exist. I want to rename all images and keep the original image name in the directory and copy renamed images into another directory.
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()
You should try using shutil.move()
import shutil
shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
Add a variable output_path
pointing to the folder to which you want to export the files, then use this variable in the seconde argument of os.rename()
, like this :
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()
Make sure to create the output folder in your system (by using mkdir
, for example).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.