Newer
Older
import os
import json
AXIS_CONVERTION = {
'0': "axial",
'1': "coronial",
'2': "sagittal"
}
def organize_images_by_axis(folder_path):
axis_dict = {}
for root, _, files in os.walk(folder_path):
for file in files:
if file.endswith('.png'):
image_path = os.path.join(root, file)
axis = AXIS_CONVERTION[image_path.rsplit("_", 2)[1]]
slice_pos = int(image_path.rsplit("_", 2)[2][:-4])
if axis not in axis_dict:
axis_dict[axis] = []
axis_dict[axis].append(slice_pos)
return axis_dict
# Example usage
folder_path = "C:/Users/Monika/Documents/IPCV/TRDP/new_data/N1/test"
image_data = organize_images_by_axis(folder_path)
# Save the dictionary to a JSON file
output_path = 'all_slices_N1.json' # Specify the output file path
with open(output_path, 'w') as json_file:
json.dump(image_data, json_file, indent=4) # Write the dictionary to the JSON file with indentation
print(f"Image data has been saved to {output_path}")