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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…