JPEG和PNG如何转换为dicom可读图像?在互联网上似乎没有关于这方面的信息。我见过有关将dicom图像转换为JPEG图像的帖子,但不是相反。 我尝试了下面的代码,但是我得到的图像(dcm)没有打开 -
import os
import tempfile
import datetime
import pydicom
from pydicom.dataset import Dataset, FileDataset
# Create some temporary filenames
suffix = '.dcm'
filename_little_endian ='bb.dcm'
filename_big_endian = 'testr.dcm'
print("Setting file meta information...")
# Populate required values for file meta information
file_meta = Dataset()
file_meta.MediaStorageSOPClassUID = "1.2.840.10008.5.1.4.1.1.7"
file_meta.MediaStorageSOPInstanceUID = "1.2.3"
file_meta.ImplementationClassUID = "1.2.3.4"
print("Setting dataset values...")
# Create the FileDataset instance (initially no data elements, but file_meta
# supplied)
ds = FileDataset(filename_little_endian, {},
file_meta=file_meta, preamble=b"\0" * 128)
# Add the data elements -- not trying to set all required here. Check DICOM
# standard
ds.PatientName = "Test^Firstname"
ds.PatientID = "123456"
# Set the transfer syntax
ds.is_little_endian = True
ds.is_implicit_VR = True
# Set creation date/time
dt = datetime.datetime.now()
ds.ContentDate = dt.strftime('%Y%m%d')
timeStr = dt.strftime('%H%M%S.%f') # long format with micro seconds
ds.ContentTime = timeStr
print("Writing test file", filename_little_endian)
ds.save_as(filename_little_endian)
print("File saved.")
# Write as a different transfer syntax XXX shouldn't need this but pydicom
# 0.9.5 bug not recognizing transfer syntax
ds.file_meta.TransferSyntaxUID = pydicom.uid.ExplicitVRBigEndian
ds.is_little_endian = False
ds.is_implicit_VR = False
print("Writing test file as Big Endian Explicit VR", filename_big_endian)
ds.save_as(filename_big_endian)
# reopen the data just for checking
for filename in (filename_little_endian, filename_big_endian):
print('Load file {} ...'.format(filename))
ds = pydicom.dcmread(filename)
print(ds)
请告诉我哪里出错?
答案 0 :(得分:0)
我不确定是否有将jpg / png图像转换为Dicom文件的python软件包。有几种进行转换的系统软件包,例如img2dcm,gdcmimg。
使用系统软件包管理器安装所需的系统软件包。
# linux
$ sudo apt install -y dcmtk
# mac
$ brew install dcmtk
安装后,您可以使用以下命令从外壳将其jpg转换为Dicom。
$ img2dcm test.jpg test.dcm
然后从您的python脚本中调用命令。
import os
command = 'img2dcm test.jpg test.dcm'
os.system(command)
这会将jpg文件转换为Dicom文件。