简体   繁体   中英

I have a Data in DICOM images i want to convert dicom images in to png or jpg format . but how to convert Number of folders at once with for loop

I have Data in DICOM images, and I want to convert dicom images into png or jpg format. However, I need to convert the images in multiple folders at once using a for-loop. Here what I've done so far:

import pydicom as dicom
import cv2 

# specify your image path
image_path = 'Data/LungSegmentation/NSCLC-Radiomics/LUNG1-001/09-18-2008-StudyID-69331/0.000000-82046/1-001.dcm'
ds = dicom.dcmread(image_path)

pixel_array_numpy = ds.pixel_array

image_format = '.jpg' # or '.png'
image_path = image_path.replace('.dcm', image_format)

cv2.imwrite(image_path, pixel_array_numpy)

#show image.....
plt.imshow(ds.pixel_array,cmap ='gray',vmin=0,vmax=255)

Iterate over files and folders with python modules like glob . Just a little example:

import glob

folders = glob.glob("PATH_CONTAINING_YOUR_FOLDERS")

for folder in folders:
    files = glob.glob(folder+'\*.dcm') # pick only dicom files
    for file in files:
        DO_YOUR_STUFF_HERE(file)

I've build a package to convert dicom files to jpg/png/bmp, you just pip install dicom2jpg then use

import dicom2jpg

dicom_dir = "/Users/user/Desktop/dir_a"
export_location = "/Users/user/Desktop/dir_b"

# convert all DICOM files in dicom_dir folder to jpg format
dicom2jpg.dicom2jpg(dicom_dir, export_location)

# convert all DICOM files in dicom_dir folder to png format
dicom2jpg.dicom2png(dicom_dir, export_location)  

hope it would help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM