Hunar
Hunar

Reputation: 399

save multi directory images in a single file after preprocessing

I am working on DICOM images, I have 5 scans(folders) each scan contain multiple images, after working some preprocessing on the images, I want to save the processed images in a single file using "np.save", I have the code below that save each folder in a separate file:

data_path = 'E:/jupyter/test/LIDC-IDRI/'
patients_data = os.listdir(data_path)
for pd in range(len(patients_data)):
    full_path = load_scan(data_path + patients_data[pd])
    after_pixel_hu = get_pixels_hu(full_path)
    after_resample, spacing = resample(after_pixel_hu, full_path, [1,1,1])
    np.save(output_path + "images_of_%s_patient.npy" % (patients_data[pd]), after_resample)

load_scan is a function for loading(reading) DICOM files, what I want to do with this code is to save all processed images in a single file, not in five files, can anyone tell me how to do that, please?

Upvotes: 1

Views: 448

Answers (1)

R Balasubramanian
R Balasubramanian

Reputation: 809

The first thing to notice is that you are using %s with patients_data[pd]. I assume patients_data is a list of the names of the patients, which means you are constructing a different output path for each patient - you are asking numpy to save each of your processed images to a new location.

Secondly, .npy is probably not the file type you want to use for your purposes, as it does not handle appending data. You probably want to pick a different file type, and then np.save() to the same file path each time.

Edit: Regarding file type, a pdf may be your best option, where you can make each of your images a separate page.

Upvotes: 1

Related Questions