Skip to content

Instantly share code, notes, and snippets.

@niklasravnsborg
Created May 17, 2023 16:47
Show Gist options
  • Save niklasravnsborg/ef64cf02a9264fd16f0687cb0b414af0 to your computer and use it in GitHub Desktop.
Save niklasravnsborg/ef64cf02a9264fd16f0687cb0b414af0 to your computer and use it in GitHub Desktop.
DICOM to JPG with Python
# Influenced by:
# - https://stackoverflow.com/questions/59458801/how-to-sort-dicom-slices-in-correct-order
# - https://medium.com/@vivek8981/dicom-to-jpg-and-extract-all-patients-information-using-python-5e6dd1f1a07d
import glob
import pydicom as dicom
import os
import cv2
import sys
import numpy as np
def main():
input_folder = sys.argv[1]
output_folder = "converted"
for file in glob.glob('{}/**/*.dcm'.format(input_folder), recursive=True):
# Load the DICOM file
ds = dicom.dcmread(file)
# Uncomment to print all metadata
# print(ds)
number = getattr(ds, 'InstanceNumber')
study = getattr(ds, 'SeriesDescription')
folder_name = os.path.join(output_folder, study)
# Create folder for export if it doesn't exist
if not os.path.exists(folder_name):
os.makedirs(folder_name)
# Convert to float to avoid overflow or underflow losses
image_2d = ds.pixel_array.astype(float)
# Rescaling grey scale between 0-255
image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 256
file_name = os.path.join(folder_name, "{}.jpg".format(str(number)))
cv2.imwrite(file_name, image_2d_scaled)
if number % 50 == 0:
print('{} images converted'.format(number))
if __name__ == "__main__":
main()
dicom2jpg==0.1.10
numpy==1.24.3
opencv-python==4.7.0.72
pydicom==2.3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment