简体   繁体   中英

Trying to convert Dicom image files into png

Hi I have a folder of dcm files i want to change into png files to put into a png folder. Here is the code I have done:

dcm_folder= '/Users/riaroque/Desktop/DCM Pneumonia cases'
PNG_folder= '/Users/riaroque/Desktop/PNG folder'

os.makedirs(PNG_folder, exist_ok=True)
for dcm_file in os.listdir(dcm_folder):
    dcm_file_path = os.path.join(dcm_folder, dcm_file)
    png_file_path = os.path.join(PNG_folder, '%s.png' % dcm_file)
    try:
        convert_file(dcm_file_path, png_file_path)
        print (dcm_file_path, '-->', png_file_path)
    except:
        print ('FAIL>', dcm_file_path, '-->', png_file_path)

It's giving me a list this error

FAIL> /Users/riaroque/Desktop/DCM Pneumonia cases/UP0084.dcm --> /Users/riaroque/Desktop/PNG folder/UP0084.dcm.png

I can see that from the error its not properly converted having.dcm.png at the end, How do I remove the.dcm and just replace it with.png?

Replace the following line:

png_file_path = os.path.join(PNG_folder, '%s.png' % dcm_file)

with

png_file_path = os.path.join(PNG_folder, dcm_file.replace('.dcm', '.png')

This uses a string's replace method to change '.dcm' to '.png'.

It assumes that '.dcm' only occurs as the suffix at the end of the file name. If there is an earlier occurrence of '.dcm' in the string, you'll have to do something more sophisticated.

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