本文整理汇总了Python中pydicom.filebase.DicomBytesIO.tell方法的典型用法代码示例。如果您正苦于以下问题:Python DicomBytesIO.tell方法的具体用法?Python DicomBytesIO.tell怎么用?Python DicomBytesIO.tell使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pydicom.filebase.DicomBytesIO
的用法示例。
在下文中一共展示了DicomBytesIO.tell方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_data_element
# 需要导入模块: from pydicom.filebase import DicomBytesIO [as 别名]
# 或者: from pydicom.filebase.DicomBytesIO import tell [as 别名]
def write_data_element(fp, data_element, encodings=None):
"""Write the data_element to file fp according to
dicom media storage rules.
"""
# Write element's tag
fp.write_tag(data_element.tag)
# If explicit VR, write the VR
VR = data_element.VR
if not fp.is_implicit_VR:
if len(VR) != 2:
msg = ("Cannot write ambiguous VR of '{}' for data element with "
"tag {}.\nSet the correct VR before writing, or use an "
"implicit VR transfer syntax".format(
VR, repr(data_element.tag)))
raise ValueError(msg)
if not in_py2:
fp.write(bytes(VR, default_encoding))
else:
fp.write(VR)
if VR in extra_length_VRs:
fp.write_US(0) # reserved 2 bytes
# write into a buffer to avoid seeking back which can be expansive
buffer = DicomBytesIO()
buffer.is_little_endian = fp.is_little_endian
buffer.is_implicit_VR = fp.is_implicit_VR
if data_element.is_raw:
# raw data element values can be written as they are
buffer.write(data_element.value)
is_undefined_length = data_element.length == 0xFFFFFFFF
else:
if VR not in writers:
raise NotImplementedError(
"write_data_element: unknown Value Representation "
"'{0}'".format(VR))
encodings = encodings or [default_encoding]
encodings = convert_encodings(encodings)
writer_function, writer_param = writers[VR]
is_undefined_length = data_element.is_undefined_length
if VR in text_VRs or VR in ('PN', 'SQ'):
writer_function(buffer, data_element, encodings=encodings)
else:
# Many numeric types use the same writer but with numeric format
# parameter
if writer_param is not None:
writer_function(buffer, data_element, writer_param)
else:
writer_function(buffer, data_element)
# valid pixel data with undefined length shall contain encapsulated
# data, e.g. sequence items - raise ValueError otherwise (see #238)
if is_undefined_length and data_element.tag == 0x7fe00010:
val = data_element.value
if (fp.is_little_endian and not
val.startswith(b'\xfe\xff\x00\xe0') or
not fp.is_little_endian and
not val.startswith(b'\xff\xfe\xe0\x00')):
raise ValueError('Pixel Data with undefined length must '
'start with an item tag')
value_length = buffer.tell()
if (not fp.is_implicit_VR and VR not in extra_length_VRs and
not is_undefined_length):
try:
fp.write_US(value_length) # Explicit VR length field is 2 bytes
except struct.error:
msg = ('The value for the data element {} exceeds the size '
'of 64 kByte and cannot be written in an explicit transfer '
'syntax. You can save it using Implicit Little Endian '
'transfer syntax, or you have to truncate the value to not '
'exceed the maximum size of 64 kByte.'
.format(data_element.tag))
raise ValueError(msg)
else:
# write the proper length of the data_element in the length slot,
# unless is SQ with undefined length.
fp.write_UL(0xFFFFFFFF if is_undefined_length else value_length)
fp.write(buffer.getvalue())
if is_undefined_length:
fp.write_tag(SequenceDelimiterTag)
fp.write_UL(0) # 4-byte 'length' of delimiter data item
示例2: write_file_meta_info
# 需要导入模块: from pydicom.filebase import DicomBytesIO [as 别名]
# 或者: from pydicom.filebase.DicomBytesIO import tell [as 别名]
def write_file_meta_info(fp, file_meta, enforce_standard=True):
"""Write the File Meta Information elements in `file_meta` to `fp`.
If `enforce_standard` is True then the file-like `fp` should be positioned
past the 128 byte preamble + 4 byte prefix (which should already have been
written).
DICOM File Meta Information Group Elements
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From the DICOM standard, Part 10 Section 7.1, any DICOM file shall contain
a 128-byte preamble, a 4-byte DICOM prefix 'DICM' and (at a minimum) the
following Type 1 DICOM Elements (from Table 7.1-1):
* (0002,0000) FileMetaInformationGroupLength, UL, 4
* (0002,0001) FileMetaInformationVersion, OB, 2
* (0002,0002) MediaStorageSOPClassUID, UI, N
* (0002,0003) MediaStorageSOPInstanceUID, UI, N
* (0002,0010) TransferSyntaxUID, UI, N
* (0002,0012) ImplementationClassUID, UI, N
If `enforce_standard` is True then (0002,0000) will be added/updated,
(0002,0001) and (0002,0012) will be added if not already present and the
other required elements will be checked to see if they exist. If
`enforce_standard` is False then `file_meta` will be written as is after
minimal validation checking.
The following Type 3/1C Elements may also be present:
* (0002,0013) ImplementationVersionName, SH, N
* (0002,0016) SourceApplicationEntityTitle, AE, N
* (0002,0017) SendingApplicationEntityTitle, AE, N
* (0002,0018) ReceivingApplicationEntityTitle, AE, N
* (0002,0100) PrivateInformationCreatorUID, UI, N
* (0002,0102) PrivateInformation, OB, N
If `enforce_standard` is True then (0002,0013) will be added/updated.
Encoding
~~~~~~~~
The encoding of the File Meta Information shall be Explicit VR Little
Endian
Parameters
----------
fp : file-like
The file-like to write the File Meta Information to.
file_meta : pydicom.dataset.Dataset
The File Meta Information DataElements.
enforce_standard : bool
If False, then only the File Meta Information elements already in
`file_meta` will be written to `fp`. If True (default) then a DICOM
Standards conformant File Meta will be written to `fp`.
Raises
------
ValueError
If `enforce_standard` is True and any of the required File Meta
Information elements are missing from `file_meta`, with the
exception of (0002,0000), (0002,0001) and (0002,0012).
ValueError
If any non-Group 2 Elements are present in `file_meta`.
"""
validate_file_meta(file_meta, enforce_standard)
if enforce_standard and 'FileMetaInformationGroupLength' not in file_meta:
# Will be updated with the actual length later
file_meta.FileMetaInformationGroupLength = 0
# Write the File Meta Information Group elements
# first write into a buffer to avoid seeking back, that can be
# expansive and is not allowed if writing into a zip file
buffer = DicomBytesIO()
buffer.is_little_endian = True
buffer.is_implicit_VR = False
write_dataset(buffer, file_meta)
# If FileMetaInformationGroupLength is present it will be the first written
# element and we must update its value to the correct length.
if 'FileMetaInformationGroupLength' in file_meta:
# Update the FileMetaInformationGroupLength value, which is the number
# of bytes from the end of the FileMetaInformationGroupLength element
# to the end of all the File Meta Information elements.
# FileMetaInformationGroupLength has a VR of 'UL' and so has a value
# that is 4 bytes fixed. The total length of when encoded as
# Explicit VR must therefore be 12 bytes.
file_meta.FileMetaInformationGroupLength = buffer.tell() - 12
buffer.seek(0)
write_data_element(buffer, file_meta[0x00020000])
fp.write(buffer.getvalue())
示例3: TestWriteFileMetaInfo
# 需要导入模块: from pydicom.filebase import DicomBytesIO [as 别名]
# 或者: from pydicom.filebase.DicomBytesIO import tell [as 别名]
#.........这里部分代码省略.........
def test_missing_elements(self):
"""Test that missing required elements raises ValueError."""
meta = Dataset()
self.assertRaises(ValueError, _write_file_meta_info, self.fp, meta)
meta.MediaStorageSOPClassUID = '1.1'
self.assertRaises(ValueError, _write_file_meta_info, self.fp, meta)
meta.MediaStorageSOPInstanceUID = '1.2'
self.assertRaises(ValueError, _write_file_meta_info, self.fp, meta)
meta.TransferSyntaxUID = '1.3'
self.assertRaises(ValueError, _write_file_meta_info, self.fp, meta)
meta.ImplementationClassUID = '1.4'
_write_file_meta_info(self.fp, meta)
def test_prefix(self):
"""Test that the 'DICM' prefix is present."""
meta = Dataset()
meta.MediaStorageSOPClassUID = '1.1'
meta.MediaStorageSOPInstanceUID = '1.2'
meta.TransferSyntaxUID = '1.3'
meta.ImplementationClassUID = '1.4'
_write_file_meta_info(self.fp, meta)
self.fp.seek(0)
prefix = self.fp.read(4)
self.assertEqual(prefix, b'DICM')
def test_group_length(self):
"""Test that the value for FileMetaInformationGroupLength is OK."""
meta = Dataset()
meta.MediaStorageSOPClassUID = '1.1'
meta.MediaStorageSOPInstanceUID = '1.2'
meta.TransferSyntaxUID = '1.3'
meta.ImplementationClassUID = '1.4'
_write_file_meta_info(self.fp, meta)
# 78 in total, - 4 for prefix, - 12 for group length = 62
self.fp.seek(12)
self.assertEqual(self.fp.read(4), b'\x3E\x00\x00\x00')
def test_group_length_updated(self):
"""Test that FileMetaInformationGroupLength gets updated if present."""
meta = Dataset()
meta.FileMetaInformationGroupLength = 100 # Actual length
meta.MediaStorageSOPClassUID = '1.1'
meta.MediaStorageSOPInstanceUID = '1.2'
meta.TransferSyntaxUID = '1.3'
meta.ImplementationClassUID = '1.4'
_write_file_meta_info(self.fp, meta)
self.fp.seek(12)
self.assertEqual(self.fp.read(4), b'\x3E\x00\x00\x00')
# Check original file meta is unchanged/updated
self.assertEqual(meta.FileMetaInformationGroupLength, 62)
self.assertEqual(meta.FileMetaInformationVersion, b'\x00\x01')
self.assertEqual(meta.MediaStorageSOPClassUID, '1.1')
self.assertEqual(meta.MediaStorageSOPInstanceUID, '1.2')
self.assertEqual(meta.TransferSyntaxUID, '1.3')
self.assertEqual(meta.ImplementationClassUID, '1.4')
def test_version(self):
"""Test that the value for FileMetaInformationVersion is OK."""
meta = Dataset()
meta.MediaStorageSOPClassUID = '1.1'
meta.MediaStorageSOPInstanceUID = '1.2'
meta.TransferSyntaxUID = '1.3'
meta.ImplementationClassUID = '1.4'
_write_file_meta_info(self.fp, meta)
self.fp.seek(16 + 12)
self.assertEqual(self.fp.read(2), b'\x00\x01')
def test_filelike_position(self):
"""Test that the file-like's ending position is OK."""
# 4 bytes prefix
# 8 + 4 bytes FileMetaInformationGroupLength
# 12 + 2 bytes FileMetaInformationVersion
# 8 + 4 bytes MediaStorageSOPClassUID
# 8 + 4 bytes MediaStorageSOPInstanceUID
# 8 + 4 bytes TransferSyntaxUID
# 8 + 4 bytes ImplementationClassUID
# 78 bytes total
meta = Dataset()
meta.MediaStorageSOPClassUID = '1.1'
meta.MediaStorageSOPInstanceUID = '1.2'
meta.TransferSyntaxUID = '1.3'
meta.ImplementationClassUID = '1.4'
_write_file_meta_info(self.fp, meta)
self.assertEqual(self.fp.tell(), 78)
# 8 + 6 bytes ImplementationClassUID
# 80 bytes total, group length 64
self.fp.seek(0)
meta.ImplementationClassUID = '1.4.1'
_write_file_meta_info(self.fp, meta)
# Check File Meta length
self.assertEqual(self.fp.tell(), 80)
# Check Group Length
self.fp.seek(12)
self.assertEqual(self.fp.read(4), b'\x40\x00\x00\x00')
_write_file_meta_info(self.fp, meta)