加载数据集并使用opencv将其存储在另一个文件中

问题描述 投票:-1回答:2

如何从数据集读取所有图像并使用opencv将其存储在另一个位置。

python-3.x opencv image-processing dataset
2个回答
1
投票

您可以使用glob读取文件夹中的文件。

import glob
import cv2
for file in glob.glob('source/*.png'):
    img = cv2.imread(file)
    filename = 'destination/'+file.split('source\\')[1]
    cv2.imwrite(filename, img)

python的Split函数可用于获取图像名称,然后将其写入目标文件夹。

注意 - 如果文件夹不在当前工作目录中,请指定绝对路径。有关绝对和相对路径的更多信息,请参阅here


1
投票
import os
import cv2

SOURCE_FOLDER = "a"
DESTINATION_FOLDER = "b"

for image_file_name in os.listdir(SOURCE_FOLDER):
    #  get full path to image file
    image_path = os.path.join(SOURCE_FOLDER, image_file_name)

    #  read image
    img = cv2.imread(image_path)

    #  store image in another folder
    image_write_path = os.path.join(DESTINATION_FOLDER, image_file_name)
    cv2.imwrite(image_write_path, img)
© www.soinside.com 2019 - 2024. All rights reserved.