|
1
|
|
|
import logging |
|
|
|
|
|
|
2
|
|
|
import os |
|
3
|
|
|
import pydicom, subprocess |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
from DICOM.validate import DICOM_validator |
|
|
|
|
|
|
6
|
|
|
from pydicom.filereader import read_file_meta_info |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
def DICOM_decompress(input_file, out_put): |
|
|
|
|
|
|
10
|
|
|
""" |
|
11
|
|
|
A wrapper for DCMDJPEG for decompression |
|
12
|
|
|
:param input_file: |
|
13
|
|
|
:param out_put: |
|
14
|
|
|
:return: |
|
15
|
|
|
""" |
|
16
|
|
|
logger = logging.getLogger(__name__) |
|
17
|
|
|
|
|
18
|
|
|
if os.path.exists(out_put): |
|
19
|
|
|
return False, "Output_exist already." |
|
20
|
|
|
|
|
21
|
|
|
try: |
|
22
|
|
|
subprocess.check_output(['dcmdjpeg', input_file, out_put]) |
|
23
|
|
|
|
|
24
|
|
|
# When dcmdjpeg has errors |
|
25
|
|
|
except subprocess.CalledProcessError as e: |
|
|
|
|
|
|
26
|
|
|
logger.info(e.output) |
|
27
|
|
|
ErrorMessage = "File type not compatible for " + input_file |
|
|
|
|
|
|
28
|
|
|
logger.info(ErrorMessage) |
|
29
|
|
|
return False, ErrorMessage |
|
30
|
|
|
except Exception as e: |
|
|
|
|
|
|
31
|
|
|
logger.info(e.output) |
|
|
|
|
|
|
32
|
|
|
ErrorMessage = "Unknown exception detected. Contact the author about " + input_file |
|
|
|
|
|
|
33
|
|
|
logger.info(ErrorMessage) |
|
34
|
|
|
return False, ErrorMessage |
|
35
|
|
|
|
|
36
|
|
|
# Ensure that data is actually written out. |
|
37
|
|
|
if not os.path.exists(out_put): |
|
38
|
|
|
ErrorMessage = "Cannot write out final file for some reason " + input_file |
|
|
|
|
|
|
39
|
|
|
logger.info(ErrorMessage) |
|
40
|
|
|
return False, ErrorMessage |
|
41
|
|
|
|
|
42
|
|
|
# Test read the data after writing. |
|
43
|
|
|
try: |
|
44
|
|
|
pydicom.read_file(out_put) |
|
45
|
|
|
except Exception as e: |
|
|
|
|
|
|
46
|
|
|
ErrorMessage = "Exception encountered while verifying the proper writing out of the DICOM data. Contact author to investigate, attach " + input_file |
|
|
|
|
|
|
47
|
|
|
logger.info(ErrorMessage) |
|
48
|
|
|
return False, ErrorMessage |
|
49
|
|
|
|
|
50
|
|
|
logger.info("Success written " + input_file + " to " + out_put) |
|
51
|
|
|
return True, "All good" |
|
52
|
|
|
|
|
53
|
|
|
def DICOM_RequireDecompression(transfer_syntax): |
|
|
|
|
|
|
54
|
|
|
""" |
|
55
|
|
|
Determine if the transfer syntax symbolize LEE or JPEG compressed! |
|
56
|
|
|
:param transfer_syntax: |
|
57
|
|
|
:return: whether the DICOM files are compressed. |
|
58
|
|
|
""" |
|
59
|
|
|
|
|
60
|
|
|
if not ("1.2.840.10008.1.2" in transfer_syntax): |
|
|
|
|
|
|
61
|
|
|
raise ValueError |
|
62
|
|
|
elif transfer_syntax == "1.2.840.10008.1.2" or transfer_syntax[18] == '1' or transfer_syntax[18] == '2': |
|
|
|
|
|
|
63
|
|
|
return False |
|
64
|
|
|
elif transfer_syntax[18] == '4' or transfer_syntax[18] == '5' or transfer_syntax[18] == '6': |
|
65
|
|
|
return True |
|
66
|
|
|
else: |
|
67
|
|
|
raise ValueError |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
def DICOM_TransferSyntax(file_path): |
|
|
|
|
|
|
71
|
|
|
""" |
|
72
|
|
|
Used to find if a file is compressed |
|
73
|
|
|
:param file_path: |
|
74
|
|
|
:return: |
|
75
|
|
|
""" |
|
76
|
|
|
|
|
77
|
|
|
# Validity check: |
|
78
|
|
|
success, DICOM = DICOM_validator(file_path) |
|
|
|
|
|
|
79
|
|
|
if not success: |
|
80
|
|
|
raise IOError |
|
81
|
|
|
|
|
82
|
|
|
# Now read the meta information. |
|
83
|
|
|
dicom_file = read_file_meta_info(file_path) |
|
84
|
|
|
transfer_syntax = dicom_file.TransferSyntaxUID |
|
85
|
|
|
|
|
86
|
|
|
return transfer_syntax |
|
87
|
|
|
|
|
88
|
|
|
def DICOM_QuickTransferSyntaxCheck(file_path): |
|
|
|
|
|
|
89
|
|
|
# Validity check: |
|
90
|
|
|
success, DICOM = DICOM_validator(file_path) |
|
|
|
|
|
|
91
|
|
|
if not success: |
|
92
|
|
|
raise IOError |
|
93
|
|
|
import dicom |
|
94
|
|
|
|
|
95
|
|
|
# Now read the meta information. |
|
96
|
|
|
if DICOM.file_meta.TransferSyntaxUID in dicom.dataset.NotCompressedPixelTransferSyntaxes: |
|
|
|
|
|
|
97
|
|
|
return False |
|
98
|
|
|
else: |
|
99
|
|
|
return True |
|
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.