python – 如何將傳輸語法uid新增到資料集的filemeta

我使用pydicom庫使用來自CT和MRI機器的資料集生成.dcm文件,但是在該資料集中,標籤(0002,0010)缺失.因為我沒有那個標籤,我無法檢測傳輸語法是否是隱式VR小端,顯式VR小端,jpeg無損等.我需要傳輸語法來儲存資料集,如下所示
ds.is_little_endian = True
    ds.is_implicit_VR = False
    ds.file_meta.TransferSyntaxUID = JPEGLossless
    ds.is_explicit_VR = True  etc

如果我不使用上面的標誌,那麼生成的dcm文件將無效,因為沒有傳輸語法.

由於我不知道傳輸語法,我在執行程序時在命令列引數中傳送傳輸語法,並相應地設定上述標誌,並儲存資料集.我知道這是錯誤的方法,但我只是用它作為臨時解決方案.有沒有更好的方法來檢測傳輸語法,因為標籤(0002,0010)缺失.

下面是我使用來自CT機器的資料集儲存dcm文件的程式碼.現在我傳送傳輸語法作為命令列引數

from pynetdicom3 import AE, VerificationPresentationContexts,      StoragePresentationContexts, QueryRetrievePresentationContexts
from pydicom.uid import ImplicitVRLittleEndian, ExplicitVRLittleEndian, JPEGLossless
from pynetdicom3.sop_class import CTImageStorage, MRImageStorage
from pynetdicom3 import pynetdicom_uid_prefix
from pydicom.dataset import Dataset, FileDataset


import argparse

import uuid
import os
import django
import logging

ttt = []
ttt.extend(VerificationPresentationContexts)
ttt.extend(StoragePresentationContexts)
ttt.extend(QueryRetrievePresentationContexts)


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lh_dcm_viewer.settings')
django.setup()



from dcm_app.models import DCMFile, DCMFileException

def _setup_argparser():


parser = argparse.ArgumentParser(
    description="The getscp application implements a Service Class "
                "Provider (SCP) for the Query/Retrieve (QR) Service Class "
                "and the Basic Worklist Management (BWM) Service Class. "
                "getscp only supports query functionality using the C-GET "
                "message. It receives query keys from an SCU and sends a "
                "response. The application can be used to test SCUs of the "
                "QR and BWM Service Classes.",
    usage="getscp [options] port")

# Parameters

# Transfer Syntaxes
ts_opts = parser.add_argument_group('Preferred Transfer Syntaxes')
ts_opts.add_argument("--type",
                     help="prefer explicit VR local byte order (default)")
ts_opts.add_argument("--detect_transfer_syntax",
                     help="Detect transfer syntax")
ts_opts.add_argument("--port",
                     help="port at which the SCP listens")



return parser.parse_args()


args = _setup_argparser()


ae = AE(ae_title=b'MY_ECHO_SCP', port=int(args.port))

   if args.type == "jpeg_lossless":
ae.add_supported_context('1.2.840.10008.1.2.4.57')
   elif args.type == "implicit":
ae.add_supported_context('1.2.840.10008.1.2')
print("ImplicitVRLittleEndian")
   elif args.type == "explicit":
ae.add_supported_context('1.2.840.10008.1.2.1')


ae.requested_contexts = ttt
ae.supported_contexts = ttt
DICOM_IP = '192.168.1.11'
DICOM_IP = '127.0.0.1'
DICOM_PORT = 5678



def save_file(dataset, context, info):


try:

    random_str = uuid.uuid4().hex
    meta = Dataset()
    meta.MediaStorageSOPClassUID = dataset.SOPClassUID
    meta.MediaStorageSOPInstanceUID = dataset.SOPInstanceUID
    meta.ImplementationClassUID = pynetdicom_uid_prefix
    meta.FileMetaInformationGroupLength = 202


    received_file_path = "../received_dcms/%s.dcm" % random_str

    dataset_vr = None
    try:
        dataset_vr = dataset[("6000", "3000")].VR
    except KeyError:
        pass

    print(dataset_vr)

    if args.type == "implicit" or dataset_vr == "OB or OW":


        ds = FileDataset(received_file_path, {}, file_meta=meta, preamble=b"\0" * 128)
        ds.update(dataset)
        ds.is_little_endian = True
        ds.is_implicit_VR = True

        if(dataset_vr == "OB or OW"):
            print("forced ImplicitVRLittleEndian")

        ds.file_meta.TransferSyntaxUID = ImplicitVRLittleEndian
    elif args.type == "jpeg_lossless":
        ds = FileDataset(received_file_path, {}, file_meta=meta, preamble=b"\0" * 128)
        ds.update(dataset)
        ds.is_little_endian = True
        ds.is_implicit_VR = False
        ds.file_meta.TransferSyntaxUID = JPEGLossless
        ds.is_explicit_VR = True
    elif args.type == "explicit":
        meta.TransferSyntaxUID = "1.2.840.10008.1.2.1"

        ds = FileDataset(received_file_path, {}, file_meta=meta, preamble=b"\0" * 128)
        ds.update(dataset)
        ds.is_little_endian = True

        ds.is_implicit_VR = False
        ds.file_meta.TransferSyntaxUID = ExplicitVRLittleEndian
        ds.is_explicit_VR = True

    ds.save_as(received_file_path)

    f = open(received_file_path, "rb")

    f.close()

    return 0XC200
    return status
except Exception as e:

    logger.error(e)



ae.on_c_store = save_file
ae.start()
我討厭送你離開,但這是一個非常專業的工具,它擁有自己的支援社群.我查找了你的問題,發現它可能是一個bug.有一個使用者有類似的問題,他說如果他使用舊版本的庫,傳輸語法不會丟失.

類似問題:

https://groups.google.com/forum/#!topic/pydicom/Oxd7cbCwseU

可在此處找到對此庫的支援:

https://groups.google.com/forum/#!forum/pydicom

程式碼本身在GitHub上維護,您可以在其中開啟錯誤報告並檢視所有其他錯誤報告: https://github.com/pydicom/pydicom/issues

翻譯自:https://stackoverflow.com/questions/52757300/how-to-add-transfer-syntax-uid-to-the-filemeta-of-dataset