Last active
June 28, 2024 18:42
-
-
Save amrvignesh/6814a7fc2e4cbe1debc7e9babbc82061 to your computer and use it in GitHub Desktop.
Python script using pydicom to load a DICOM image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import pydicom | |
import os | |
import traceback | |
def extract_dicom_headers(input_file_path, output_file_path): | |
try: | |
# Read the DICOM file | |
ds = pydicom.dcmread(input_file_path) | |
# Write headers to the output file | |
with open(output_file_path, 'w') as f: | |
f.write(str(ds)) | |
print(f"DICOM headers have been written to {output_file_path}") | |
except pydicom.errors.InvalidDicomError: | |
print(f"Error: {input_file_path} is not a valid DICOM file.") | |
except FileNotFoundError: | |
print(f"Error: File {input_file_path} not found.") | |
except Exception as e: | |
print(f"An error occurred: {repr(e)}") | |
print("Traceback:") | |
print(traceback.format_exc()) | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
print("Usage: python script_name.py <path_to_dicom_file>") | |
sys.exit(1) | |
input_file_path = sys.argv[1] | |
output_file_path = os.path.splitext(input_file_path)[0] + "_headers.txt" | |
extract_dicom_headers(input_file_path, output_file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment