0

I have a folder of dicom images and I stored these images in an array and I would like to print them out in a different folder.

I cannot find a method that will write out each of the images like the cv2.imwrite

import pydicom
import skimage, os
import numpy as np
FolderPathName = r'FolderPathName'
slices = [pydicom.read_file(FolderPathName + imagename) for imagename in os.listdir(FolderPathName)]
    # Sort the dicom slices in their respective order
slices.sort(key=lambda x: int(x.InstanceNumber))

for x in range(len(slices)): 
    #write the images in a new folder
BPDESILVA
  • 2,040
  • 5
  • 15
  • 35
  • You want to save the images again? As DICOM? With a different filename? – ColonelFazackerley Jul 09 '19 at 11:15
  • basically, the problem is the files are named out of order so I am using the sort method: slices.sort(key=lambda x: int(x.InstanceNumber)) , to organize them properly in the array slices and then I will rename them in order – CommingUpMilhouse Jul 09 '19 at 13:51

1 Answers1

0

Method 1: In your case, The answer is ...

import pydicom
import skimage, os
import numpy as np
FolderPathName = r'FolderPathName'
slices = [pydicom.read_file(FolderPathName + imagename) for imagename in os.listdir(FolderPathName)]
# Sort the dicom slices in their respective order
slices.sort(key=lambda x: int(x.InstanceNumber))
jpg_folder = '' # Set your jpg folder
for idx in range(len(slices)): 
    #write the images in a new folder
    jpg_filepath = os.path.join( jpg_folder, "pic-{}.jpg".format(idx) )
    np_pixel_array = slices[idx].pixel_array
    cv2.imwrite(jpg_filepath, np_pixel_array)

Method 2: But, there is better way to process dicom files ...

import pydicom
import os
import numpy as np
import cv2
dicom_folder = '' # Set the folder of your dicom files that inclued images 
jpg_folder = '' # Set the folder of your output folder for jpg files 
# Step 1. prepare your input(.dcm) and output(.jpg) filepath 
dcm_jpg_map = {}
for dicom_f in os.listdir(dicom_folder):
    dicom_filepath = os.path.join(dicom_folder, dicom_f)
    jpg_f = dicom_f.replace('.dcm', '.jpg') 
    jpg_filepath = os.path.join(jpg_folder,jpg_f)
    dcm_jpg_map[dicom_filepath] = jpg_filepath

# Now, dcm_jpg_map is key,value pair of input dcm filepath and output jpg filepath

# Step 2. process your image by input/output information
for dicom_filepath, jpg_filepath in dcm_jpg_map.items():
    # convert dicom file into jpg file
    dicom = pydicom.read_file(dicom_filepath)
    np_pixel_array = dicom.pixel_array
    cv2.imwrite(jpg_filepath, np_pixel_array)

In above code, the Step 1 is focus on file path processing. It's good for your to porting your code into different environment easily.

The Step 2 is major code which focus on any kind of image processing.

Milo Chen
  • 3,617
  • 4
  • 20
  • 36
  • 1
    I just get ---> 11 jpg_filepath = os.path.join( jpg_folder, "pic-{}.jpg".format(x) ) 12 np_pixel_array = slices[idx].pixel_array 13 cv2.imwrite(jpg_filepath, np_pixel_array) NameError: name 'x' is not defined using your first example – user3078100 Jul 24 '20 at 07:58
  • @user3078100 thanks for your feedback. It's helpful. I have changed it from format(x) to format(idx). – Milo Chen Jul 25 '20 at 01:26