如何修复“LookupError:未知编码:ISO_IR 149”

问题描述 投票:0回答:2

我正在使用 pydicom 更改 dicom 标头。 PatientName包含韩语,所以我将PatientName修改为英文。 之后,尝试保存时会输出 ISO_IR_149 错误。 我在 charset.py 中查找了与 ISO_IR_149 相关的部分,但无法解决这个问题。 我们该如何解决这个问题呢? 谢谢。

import pydicom
import os
import tempfile
import datetime
from pydicom.dataset import Dataset, FileDataset
suffix = '.dcm'
filename_little_endian = tempfile.NamedTemporaryFile(suffix=suffix).name
filename_big_endian = tempfile.NamedTemporaryFile(suffix=suffix).name
#print(ds.PatientSex)
ds = pydicom.filereader.dcmread("003.dcm")
ds.PatientName = "Patient1"
print(ds.PatientName)

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)

    # remove the created file
    print('Remove file {} ...'.format(filename))
    os.remove(filename)

错误信息如下。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/admin/PycharmProjects/Cylinder/OpenHeader-dicom.py", line 15, in <module>
    ds.save_as(filename_little_endian)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pydicom\dataset.py", line 1108, in save_as
    pydicom.dcmwrite(filename, self, write_like_original)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pydicom\filewriter.py", line 888, in dcmwrite
    write_dataset(fp, get_item(dataset, slice(0x00010000, None)))
  File "C:\ProgramData\Anaconda3\lib\site-packages\pydicom\filewriter.py", line 521, in write_dataset
    write_data_element(fp, dataset.get_item(tag), dataset_encoding)
  File "C:\ProgramData\Anaconda3\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pydicom\tag.py", line 37, in tag_in_exception
    raise type(ex)(msg)
LookupError: With tag (0010, 0010) got exception: unknown encoding: ISO_IR 149
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\pydicom\tag.py", line 30, in tag_in_exception
    yield
  File "C:\ProgramData\Anaconda3\lib\site-packages\pydicom\filewriter.py", line 521, in write_dataset
    write_data_element(fp, dataset.get_item(tag), dataset_encoding)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pydicom\filewriter.py", line 464, in write_data_element
    writer_function(buffer, data_element, encodings=encodings)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pydicom\filewriter.py", line 264, in write_PN
    val = [elem.encode(encodings) for elem in val]
  File "C:\ProgramData\Anaconda3\lib\site-packages\pydicom\filewriter.py", line 264, in <listcomp>
    val = [elem.encode(encodings) for elem in val]
  File "C:\ProgramData\Anaconda3\lib\site-packages\pydicom\valuerep.py", line 763, in encode
    return _encode_personname(self.components, encodings)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pydicom\valuerep.py", line 594, in _encode_personname
    for group in comp.split('^')]
  File "C:\ProgramData\Anaconda3\lib\site-packages\pydicom\valuerep.py", line 594, in <listcomp>
    for group in comp.split('^')]
  File "C:\ProgramData\Anaconda3\lib\site-packages\pydicom\charset.py", line 274, in encode_string
    encoded = value.encode(encoding)
LookupError: unknown encoding: ISO_IR 149
python dicom pydicom
2个回答
1
投票

答案很简单,但可能令人不满意:DICOM 不直接支持 ISO_IR 149。它确实支持具有韩语代码扩展技术的字符集。 DICOM 中韩文字符的定义术语是:“ISO 2022 IR 149”。也许使用 UTF-8 (ISO_IR 192) 是您的另一种选择?

参见 PS3.3、C.12.1.1.2


0
投票

示例代码

 #Set Specific Character Set, replacing euc_kr with ISO_IR 149 because pydicom can't decode ISO_IR 149
 specific_charset = dicom_data.get("SpecificCharacterSet", "")
 if specific_charset == "ISO_IR 149":
    specific_charset = "euc_kr"

 # Patient's Name decode
 raw_name = dicom_data[0x00100010].value
 patient_name = raw_name.decode(specific_charset)
 dicom_data.PatientName = patient_name
© www.soinside.com 2019 - 2024. All rights reserved.