1

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?

Hunar
  • 399
  • 3
  • 7
  • 27

1 Answers1

1

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.

  • @Balasubramanian thank you for your nice answer, I know about the %s and I can play with it as I want, your second answer is new for me can you tell me more about that, please? any other file type to use for that purpose? – Hunar Jun 21 '18 at 14:28
  • I guess first I am confused why you want to save all your images into the same file - it seems like it would make further processing, viewing, or distribution harder? Is it not sufficient to save individual images in a designated output folder? – R Balasubramanian Jun 21 '18 at 14:33
  • Regardless, I have edited my original answer to include an option with .pdf. Hope that helps! – R Balasubramanian Jun 21 '18 at 14:36
  • thank you for your tiredness, I want to send the preprocessing result to a deep learning model for training, I see if it's in a single file it's easier. saving the files in .pdf file is not a good option for me. – Hunar Jun 21 '18 at 17:30
  • can you elaborate on this deep learning model? without further information I am even unsure why separate image files would not work, given that you can use the same naming convention to automatically load in many images even if they are in different files – R Balasubramanian Jun 21 '18 at 18:01