重复文件夹内的一些图像
Repeat some images inside folder
我有一个由子文件夹组成的数据集文件夹,每个子文件夹必须包含 8 张图像,但其中一些包含的图像少于 8,例如 3、4、5、6、7 或可能是 8。我想通过重复现有图像从第一个可用图像到最后一个图像直到达到 8 个图像,将它们全部均衡为 8 个图像,然后停止并将所有图像保存在相同的目录路径中。
我循环获取图像并按照下面的代码读取它们,但我面临着如何重复它们直到每个文件夹达到 8 个图像的问题。
ID_Paths = "path\of\dataset\folder"
listing = os.listdir(ID_Paths)
for fold_Path in listing:
print("NOW input images of new individual... ", fold_Path)
image_fold = os.listdir(ID_Paths + "\" + fold_Path)
for file in image_fold:
segments = os.listdir(ID_Paths + "\" + fold_Path + "\" + file)
im = ID_Paths + "\" + fold_Path + "\" + file + "\" + segments
test_image = cv2.imread(im)
您的答案中仍有许多未指明的内容,但为了向您提供至少接近您想要的内容,我将 post 给出一个答案。它使用 itertools
module to generate filenames for the copied files. It just copies the image files using the shutil.copyfile()
函数中的几个函数,而不是使用 cv2
模块来将每个函数读入内存,然后再次写出——这似乎没有必要,因为你没有对图像做任何其他事情。
我尽量使变量名易于理解,并在关键位置添加了注释。
from itertools import count, cycle
import os
import os.path
from shutil import copyfile
ID_Paths = r"path\of\dataset\folder"
REQ_NUM_IMGS = 8 # Required number of image files in each subfolder.
# Subfolders in ID_Paths.
subfolders = [subfolder for subfolder in os.listdir(ID_Paths)
if os.path.isdir(os.path.join(ID_Paths, subfolder))]
# Equalize number of (image) files in each subfolder.
for subfolder in subfolders:
print("NOW input images of new individual...", subfolder)
# Assumes all files in subfolder are image files.
subfolder_path = os.path.join(ID_Paths, subfolder)
image_filenames = os.listdir(subfolder_path)
print(' original files:', image_filenames)
shortage = REQ_NUM_IMGS - len(image_filenames)
if shortage > 0: # Need repeats?
for i, src_img_filename in zip(count(REQ_NUM_IMGS+1),
cycle(image_filenames)):
src_imgfile_path = os.path.join(subfolder_path, src_img_filename)
img_filename, img_ext = os.path.splitext(src_img_filename)
suffix = '_' + str(i)
dst_img_filename = os.path.join(subfolder_path,
img_filename+suffix+img_ext)
dst_imgfile_path = os.path.join(subfolder_path, dst_img_filename)
print(' copying: {}\n'
' to: {}'.format(src_imgfile_path, dst_imgfile_path))
copyfile(src_imgfile_path, dst_imgfile_path)
shortage -= 1
if shortage == 0:
break
我有一个由子文件夹组成的数据集文件夹,每个子文件夹必须包含 8 张图像,但其中一些包含的图像少于 8,例如 3、4、5、6、7 或可能是 8。我想通过重复现有图像从第一个可用图像到最后一个图像直到达到 8 个图像,将它们全部均衡为 8 个图像,然后停止并将所有图像保存在相同的目录路径中。
我循环获取图像并按照下面的代码读取它们,但我面临着如何重复它们直到每个文件夹达到 8 个图像的问题。
ID_Paths = "path\of\dataset\folder"
listing = os.listdir(ID_Paths)
for fold_Path in listing:
print("NOW input images of new individual... ", fold_Path)
image_fold = os.listdir(ID_Paths + "\" + fold_Path)
for file in image_fold:
segments = os.listdir(ID_Paths + "\" + fold_Path + "\" + file)
im = ID_Paths + "\" + fold_Path + "\" + file + "\" + segments
test_image = cv2.imread(im)
您的答案中仍有许多未指明的内容,但为了向您提供至少接近您想要的内容,我将 post 给出一个答案。它使用 itertools
module to generate filenames for the copied files. It just copies the image files using the shutil.copyfile()
函数中的几个函数,而不是使用 cv2
模块来将每个函数读入内存,然后再次写出——这似乎没有必要,因为你没有对图像做任何其他事情。
我尽量使变量名易于理解,并在关键位置添加了注释。
from itertools import count, cycle
import os
import os.path
from shutil import copyfile
ID_Paths = r"path\of\dataset\folder"
REQ_NUM_IMGS = 8 # Required number of image files in each subfolder.
# Subfolders in ID_Paths.
subfolders = [subfolder for subfolder in os.listdir(ID_Paths)
if os.path.isdir(os.path.join(ID_Paths, subfolder))]
# Equalize number of (image) files in each subfolder.
for subfolder in subfolders:
print("NOW input images of new individual...", subfolder)
# Assumes all files in subfolder are image files.
subfolder_path = os.path.join(ID_Paths, subfolder)
image_filenames = os.listdir(subfolder_path)
print(' original files:', image_filenames)
shortage = REQ_NUM_IMGS - len(image_filenames)
if shortage > 0: # Need repeats?
for i, src_img_filename in zip(count(REQ_NUM_IMGS+1),
cycle(image_filenames)):
src_imgfile_path = os.path.join(subfolder_path, src_img_filename)
img_filename, img_ext = os.path.splitext(src_img_filename)
suffix = '_' + str(i)
dst_img_filename = os.path.join(subfolder_path,
img_filename+suffix+img_ext)
dst_imgfile_path = os.path.join(subfolder_path, dst_img_filename)
print(' copying: {}\n'
' to: {}'.format(src_imgfile_path, dst_imgfile_path))
copyfile(src_imgfile_path, dst_imgfile_path)
shortage -= 1
if shortage == 0:
break