Tag Archives: Medical image

[Python] error reported when reading DICOM file pydicom.errors.invaliddicomerror

[Python] error reported when reading DICOM file pydicom.errors.invaliddicomerror

When using Python to read and display DICOM files, the code is as follows:

import pydicom
import matplotlib.pyplot as plt
a = pydicom.read_file(r'C:\Users\shdn\Desktop\004.dcm')
print(a)
plt.imshow(img)
plt.show()

However, I sometimes encounter the following errors (I made the following error when reading the data given by the hospital):

The reason for this error is that the data given by the hospital may not be processed, and the file meta information header is missing, so it cannot be read directly. Solution: force reading.

The code is as follows:

import pydicom
import matplotlib.pyplot as plt
a = pydicom.read_file(r'C:\Users\shdn\Desktop\004.dcm' , force=True)
a.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian
print(a)
plt.imshow(img)
plt.show()

Successfully resolved: