Write to DICOM file
import numpy as np
import matplotlib.pyplot as plt
import pydicom
import sys
def InitDicomFile():
infometa = pydicom.dataset.Dataset()
infometa.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian
infometa.ImplementationVersionName = 'Python ' + sys.version
infometa.MediaStorageSOPClassUID = 'CT Image Storage'
infometa.FileMetaInformationVersion = b'\x00\x01'
return infometa
filename = r'E:\34.dcm'
# ds = pydicom.dcmread(filename) # Read Tags from dicom image
ds = pydicom.read_file(filename) # Read Tags of the dicom image
info = pydicom.dataset.FileDataset({},ds)
temp = pydicom.dcmread(filename).pixel_array # read in DICOM image and convert to numpy
img = temp.astype('uint16')
info.PixelData = img.tobytes()
infometa = InitDicomFile() # must be initialized otherwise read in will prompt an error
info = pydicom.dataset.FileDataset({},info,is_implicit_VR =True, file_meta=infometa)
# info = pydicom.dataset.FileDataset({},info) # Read without file_meta=infometa will prompt an error
info.save_as(r'M:\result.dcm')
# Verify that the newly stored DICOM can be read in
f = pydicom.dcmread(r'M:\result.dcm',force=True)
plt.imshow(f.pixel_array,'gray')
plt.show()
If there are not the following two lines, reading the stored DICOM file will prompt the following error:
infometa = InitDicomFile()
info = pydicom.dataset.FileDataset({},info,is_implicit_VR =True, file_meta=infometa)
AttributeError: ‘FileMetaDataset’ object has no attribute ‘TransferSyntaxUID’
Read More:
- Python Openyxl Error: AttributeError: ‘int‘ object has no attribute ‘upper‘ [How to Solve]
- [Modified] AttributeError: ‘socket‘ object has no attribute ‘ioctl‘ python linux
- How to Solve Python AttributeError: ‘module’ object has no attribute ‘xxx’
- [2021-10-05] Python Error: AttributeError: ‘NoneType‘ object has no attribute ‘append‘
- Python 3.7 Error: AttributeError: ‘str‘ object has no attribute ‘decode‘ [How to Solve]
- [Solved] python Error: AttributeError: ‘NoneType‘ object has no attribute ‘split‘
- How to Solve Python AttributeError: ‘dict’ object has no attribute ‘item’
- [Solved] Python Keras Error: AttributeError: ‘Sequential‘ object has no attribute ‘predict_classes‘
- [Solved] Python Selenium Error: AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_xpath‘
- [Solved] AttributeError: ‘PngImageFile‘ object has no attribute ‘imshow‘
- AttributeError: DatetimeProperties object has no attribute
- [Solved] AttributeError: ‘DataFrame‘ object has no attribute ‘tolist‘
- How to Solve attributeerror: ‘list’ object has no attribute ‘shape‘
- [Solved] AttributeError: DataFrame object has no attribute’xxx’
- [Solved] AttributeError: ‘HTMLWriter‘ object has no attribute ‘_temp_names‘
- [Solved] AttributeError: ‘NoneType‘ object has no attribute ‘astype‘
- [Solved] AttributeError: ‘_IncompatibleKeys’ object has no attribute
- [Solved] AttributeError WriteOnlyWorksheet object has no attribute cell
- [Solved] AttributeError: ‘DataParallel‘ object has no attribute ‘save‘
- [Solved] Paramiko error: AttributeError: ‘NoneType’ object has no attribute ‘time’