Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

python - Split data and save to separate folders

I have a folder that contains 300+ jpeg images that I want to split into 5 folders equally (20% of total data). I ended up needing to sort my images because with os.walk or simple for loop through the folder the files would not read from 1 to 100. Instead they would read (for example) 1,10,100,2,20,200,3.... By printing the type of one of the split files, it is no longer a numpy array and instead a string. So I belive that is the problem why I cannot find any method to save my split file as a jpeg because it is a string data type now. Are there any suggestions on how I could change this to retain the original data type and sort the files accordingly to save at the end?

import os
from PIL import Image


def split(path):

    # Properly order the files
    os.chdir(path)
    files_jpeg = os.listdir(path + '\training_images\')
    files_jpeg.sort(key=lambda x: int(x.split('.')[0]))

    # Identify number of files in training folder
    path_jpeg = path + '\training_images\'

    # Get list of files
    file_list_jpeg = []
    for i_jpeg in files_jpeg:
        file_list_jpeg.append(i_jpeg)

    # Six different augmentations
    # Augmentations will be split by 20%
    noise_split_files_jpeg = file_list_jpeg[:int(len(file_list_jpeg)*0.2)]
    blur_split_files_jpeg = file_list_jpeg[int(len(file_list_jpeg)*0.2):int(len(file_list_jpeg)*0.4)]
    brightness_split_files_jpeg = file_list_jpeg[int(len(file_list_jpeg)*0.4):int(len(file_list_jpeg)*0.6)]
    contrast_split_files_jpeg = file_list_jpeg[int(len(file_list_jpeg)*0.6):int(len(file_list_jpeg)*0.8)]
    rotation_split_files_jpeg = file_list_jpeg[int(len(file_list_jpeg)*0.8):int(len(file_list_jpeg)*1)]

    # concatinate path with files
    noise_split_files_jpeg_path = [path_jpeg + x_jpeg for x_jpeg in noise_split_files_jpeg]
    blur_split_files_jpeg_path = [path_jpeg + x_jpeg for x_jpeg in blur_split_files_jpeg]
    brightness_split_files_jpeg_path = [path_jpeg + x_jpeg for x_jpeg in brightness_split_files_jpeg]
    contrast_split_files_jpeg_path = [path_jpeg + x_jpeg for x_jpeg in contrast_split_files_jpeg]
    rotation_split_files_jpeg_path = [path_jpeg + x_jpeg for x_jpeg in rotation_split_files_jpeg]

    os.makedirs(path + '\noise\')
    os.makedirs(path + '\blur\')
    os.makedirs(path + '\brightness\')
    os.makedirs(path + '\contrast\')
    os.makedirs(path + '\rotate\')

    x = 0
    os.chdir(path + '\noise\')
    for i in noise_split_files_jpeg_path:
        im = Image.fromarray(i)
        im.save("your_file" + str(x) + ".jpeg")
        x = x + 1
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't have to read images just to copy them. Use glob (from glob import glob) for gettings paths of all the files available in the folder in a list (os.listdir should work as well, but glob will give you the list of file paths directly). Sort the list as the filenames won't be sorted by default. Use copyfile function from shutil library to copy files to the destination folder. copyfile accepts two parameters, absolute source file path and absolute destination file path where you want to move the file. You'll need to loop through the list of file paths. You should look into path module from os library as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...