如何重命名子目录中的图像?
How can I rename Images in subdirectories?
我创建了一个用于训练神经网络的合成图像数据集。每个图像文件夹都有一个 "images" 文件夹和一个 "masks" 文件夹。
不幸的是,"images" 文件夹中的图像名称不正确。
├── img995
│ ├── images
│ │ └── img142.png
│ └── masks
│ ├── img10_mask10_.png
│ ├── img10_mask10.png
我的目标是重命名 "images" 文件夹中的图像(而不是 masks 文件夹中的图像)。
我试过这段代码,但它没有按预期工作:
import os
os.getcwd()
collection = "/home/dataset/"
for imi in os.listdir(collection):
for images, masks in imi:
for p in images:
os.rename(collection + p + images,
collection + p + str(995 + i) + ".png")
Error message:
4 for imi in os.listdir(collection):
----> 5 for images, masks in imi:
6 for p in images:
7 os.rename(collection + p + images,
ValueError: not enough values to unpack (expected 2, got 1)
我对您的要求有点困惑,但是如果您想将 _new
添加到 image
文件夹中的所有图像,您可以使用以下命令:
for root, dirs, files in os.walk('dataset'):
if os.path.basename(root) == 'images':
for f in files:
name, ext = os.path.splitext(f)
os.rename(os.path.join(root, f), os.path.join(root, name + '_new' + ext))
glob
可以成为你的朋友。基本上,如果你想循环遍历 image
子文件夹下的所有 png
文件,你可以这样做:
import os
import glob
collection = "/home/dataset"
generic_pattern = os.path.join(
collection,
"*", "images", "*.png"
)
for a_file in glob.glob(generic_pattern):
f_path, f_name = os.path.split(a_file)
f_raw_name, f_ext = f_name.split('.')
# not sure what you want to do for the new file name
new_name = f_path.split(os.sep)[-2] # would be 'img995' in the example
f_new = os.path.join(
f_path,
'{0}.{1}'.format(new_name, f_ext)
)
# to see what you will be doing:
print(a_file, f_new)
# if everything works fine, uncomment this:
# os.rename(a_file, f_new)
import os
os.getcwd()
collection = "/home/dataset/"
for imi in os.listdir(collection): # Lists all files and folders
path = '{0}{1}'.format(collection, imi)
if(os.path.isdir(path)): # Filter by folders
folder_number = imi[3:] # Collect folder number
image_folder = '{0}/images'.format(path) # Collect image folder
for image in os.listdir(image_folder): # Collect image in image folder
image_path = '{0}/{1}'.format(image_folder, image) # Build original image path
new_image = 'img{0}.png'.format(folder_number) # New image file name
new_image_path = '{0}/{1}'.format(image_folder, new_image) # Build new image path
os.rename(image_path, new_image_path) # Rename image with folder number
按照您的示例树结构,这将重命名
├── imgX
│ ├── images
│ │ └── imgY.png <-- This File
│ └── masks
│ ├── img10_mask10_.png
│ ├── img10_mask10.png
到
├── imgX
│ ├── images
│ │ └── imgX.png <-- This File
│ └── masks
│ ├── img10_mask10_.png
│ ├── img10_mask10.png
对于 home/dataset/
中的所有 img
个文件夹
我认为这可以解决您的问题:
import os
first_list = [name for name in os.listdir("/home/dataset/") if name.endswith(".png")]
# if you want to rename and enumerate your images
final = [str(x[0])+'_new_name.png' for x in enumerate(first_list)]
# if you don't want to enumerate and just rename them
final = ['new_name.' + x.split('.')[1] for x in first_list]
祝你好运!
我创建了一个用于训练神经网络的合成图像数据集。每个图像文件夹都有一个 "images" 文件夹和一个 "masks" 文件夹。 不幸的是,"images" 文件夹中的图像名称不正确。
├── img995
│ ├── images
│ │ └── img142.png
│ └── masks
│ ├── img10_mask10_.png
│ ├── img10_mask10.png
我的目标是重命名 "images" 文件夹中的图像(而不是 masks 文件夹中的图像)。
我试过这段代码,但它没有按预期工作:
import os
os.getcwd()
collection = "/home/dataset/"
for imi in os.listdir(collection):
for images, masks in imi:
for p in images:
os.rename(collection + p + images,
collection + p + str(995 + i) + ".png")
Error message:
4 for imi in os.listdir(collection):
----> 5 for images, masks in imi:
6 for p in images:
7 os.rename(collection + p + images,
ValueError: not enough values to unpack (expected 2, got 1)
我对您的要求有点困惑,但是如果您想将 _new
添加到 image
文件夹中的所有图像,您可以使用以下命令:
for root, dirs, files in os.walk('dataset'):
if os.path.basename(root) == 'images':
for f in files:
name, ext = os.path.splitext(f)
os.rename(os.path.join(root, f), os.path.join(root, name + '_new' + ext))
glob
可以成为你的朋友。基本上,如果你想循环遍历 image
子文件夹下的所有 png
文件,你可以这样做:
import os
import glob
collection = "/home/dataset"
generic_pattern = os.path.join(
collection,
"*", "images", "*.png"
)
for a_file in glob.glob(generic_pattern):
f_path, f_name = os.path.split(a_file)
f_raw_name, f_ext = f_name.split('.')
# not sure what you want to do for the new file name
new_name = f_path.split(os.sep)[-2] # would be 'img995' in the example
f_new = os.path.join(
f_path,
'{0}.{1}'.format(new_name, f_ext)
)
# to see what you will be doing:
print(a_file, f_new)
# if everything works fine, uncomment this:
# os.rename(a_file, f_new)
import os
os.getcwd()
collection = "/home/dataset/"
for imi in os.listdir(collection): # Lists all files and folders
path = '{0}{1}'.format(collection, imi)
if(os.path.isdir(path)): # Filter by folders
folder_number = imi[3:] # Collect folder number
image_folder = '{0}/images'.format(path) # Collect image folder
for image in os.listdir(image_folder): # Collect image in image folder
image_path = '{0}/{1}'.format(image_folder, image) # Build original image path
new_image = 'img{0}.png'.format(folder_number) # New image file name
new_image_path = '{0}/{1}'.format(image_folder, new_image) # Build new image path
os.rename(image_path, new_image_path) # Rename image with folder number
按照您的示例树结构,这将重命名
├── imgX
│ ├── images
│ │ └── imgY.png <-- This File
│ └── masks
│ ├── img10_mask10_.png
│ ├── img10_mask10.png
到
├── imgX
│ ├── images
│ │ └── imgX.png <-- This File
│ └── masks
│ ├── img10_mask10_.png
│ ├── img10_mask10.png
对于 home/dataset/
img
个文件夹
我认为这可以解决您的问题:
import os
first_list = [name for name in os.listdir("/home/dataset/") if name.endswith(".png")]
# if you want to rename and enumerate your images
final = [str(x[0])+'_new_name.png' for x in enumerate(first_list)]
# if you don't want to enumerate and just rename them
final = ['new_name.' + x.split('.')[1] for x in first_list]
祝你好运!