Convert DICOM images to JPEG format

import os
import pydicom
from PIL import Image
import numpy as np

# Path to folder with DICOM files
dicom_dir = “D:/IMAGE_CLUSTER/manifest-1622561851074/NSCLC Radiogenomics”
# Path to the folder where the converted JPEG files will be saved
output_dir = “D:/GÖRÜNTÜ_KÜMESİ/manifest-1622561851074/NSCLC_RADİO.J”

# Checks if a folder exists, if not, creates it
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# loops all files in the dicom_dir folder
for filename in os.listdir(dicom_dir):
    if filename.endswith(“.dcm”): # Only files with .dcm extension
        filepath = os.path.join(dicom_dir, filename)
        
        # Reads DICOM file
        ds = pydicom.dcmread(filepath)
        
        # Receives and normalizes pixel data
        image_array = ds.pixel_array
        image = Image.fromarray(image_array)
        
        # Saves the file name with .jpeg extension
        jpeg_filename = os.path.splitext(filename)[0] + “.jpeg”
        output_path = os.path.join(output_dir, jpeg_filename)
        
        # Saves the image in JPEG format
        image.save(output_path)

print(“DICOM files have been successfully converted to JPEG format.”)

There is no error in the code script, but there are images with .cdm extension in the relevant folder, they are in different folders inside the relevant folder and there are many folders inside the folder and there are images with .cdm extension in those folders. (that is, there are multiple folders inside a folder and new folders inside them and then there are .cdm extensions) I realize that I have a very complicated analysis.
Can you help me to convert .dcm images to .jpeg format with python?

@ABDULVAHAP

To convert DICOM images to JPEG format in Python, especially when dealing with multiple nested folders containing .dcm files, you can modify your existing script to traverse through all subdirectories. Below is an updated version of your code that accomplishes this:

import os
import pydicom
from PIL import Image

# Path to the main folder with DICOM files
dicom_dir = "D:/IMAGE_CLUSTER/manifest-1622561851074/NSCLC Radiogenomics"
# Path to the folder where the converted JPEG files will be saved
output_dir = "D:/GÖRÜNTÜ_KÜMESİ/manifest-1622561851074/NSCLC_RADİO.J"

# Checks if a folder exists, if not, creates it
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# Function to convert DICOM to JPEG
def convert_dicom_to_jpeg(dicom_path, output_path):
    # Reads DICOM file
    ds = pydicom.dcmread(dicom_path)
    
    # Receives and normalizes pixel data
    image_array = ds.pixel_array
    image = Image.fromarray(image_array)
    
    # Saves the image in JPEG format
    image.save(output_path)

# Walk through all directories and files
for root, dirs, files in os.walk(dicom_dir):
    for filename in files:
        if filename.endswith(".dcm"):  # Only files with .dcm extension
            dicom_filepath = os.path.join(root, filename)
            jpeg_filename = os.path.splitext(filename)[0] + ".jpeg"
            output_path = os.path.join(output_dir, jpeg_filename)
            
            # Convert DICOM to JPEG
            convert_dicom_to_jpeg(dicom_filepath, output_path)

print("DICOM files have been successfully converted to JPEG format.")

Explanation:

  1. os.walk(): This function is used to traverse through all directories and subdirectories starting from dicom_dir. It yields a tuple of the current directory path, the directories in it, and the files in it.
  2. File Conversion: For each file that ends with .dcm, the script reads the DICOM file, converts it to a JPEG image, and saves it in the specified output directory.

This approach ensures that all .dcm files in nested folders are processed and converted to JPEG format. Make sure you have the necessary libraries (pydicom and PIL) installed in your Python environment.

Thank you teacher, it does not give an error, but output_path =54 there is an image number, but there is no image in the folder in the path “D:/VIEW_CLUSTER/manifest-1622561851074/NSCLC_RADIO.J”.

Hi, @ABDULVAHAP
Thank you for your interest in Aspose.Imaging.

Can you help me to convert .dcm images to .jpeg format with python?

Yes, you can use the following code to convert dicom to jpeg in Python.
Firstly, install Aspose.Imaging for Python via .NET
pip3 install aspose-imaging-python-net
The code for the conversion


import os
from aspose.imaging import Image

# Path to folder with DICOM files
dicom_dir = 'D:/IMAGE_CLUSTER/manifest-1622561851074/NSCLC Radiogenomics'
# Path to the folder where the converted JPEG files will be saved
output_dir = 'D:/GÖRÜNTÜ_KÜMESİ/manifest-1622561851074/NSCLC_RADİO.J'

# Checks if a folder exists, if not, creates it
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# loops all files in the dicom_dir folder
for filename in os.listdir(dicom_dir):
    if filename.endswith(('.dcm', '.dicom')): # Only files with .dcm extension
        filepath = os.path.join(dicom_dir, filename)
        
        # Reads DICOM file
        with Image.load(filepath) as image:
            # Saves the file name with .jpeg extension
            jpeg_filename = os.path.splitext(filename)[0] + '.jpeg'
            output_path = os.path.join(output_dir, jpeg_filename)
            
            # Saves the image in JPEG format
            image.save(output_path)

print('DICOM files have been successfully converted to JPEG format.')

Thank you teacher, there is no error in the last code script you gave, but it did not convert .cdm files.
import dicom2jpg # folder path where DICOM files are located dicom_dir = “D:/VIEW_CLUSTER/manifest-1622561851074/NSCLC Radiogenomics” # folder path where converted JPEG files will be saved output_dir = “D:/VIEW_CLUSTER/manifest-1622561851074/NSCLC_RADIO.J” # convert all DICOM files in dicom_dir folder to jpg format dicom2jpg.dicom2jpg(dicom_dir, output_dir)

the script converted nested folder extensions to jpg format. but not .dcm extensions.

@ABDULVAHAP , as far as I know .cdm file is not an image format, unless it is a wrong extension for an other image format file.
Could you share more info on any exceptions you get?