def encode(ds, is_implicit_vr, is_little_endian): """Encode a pydicom Dataset `ds` to a byte stream. Parameters ---------- ds : pydicom.dataset.Dataset The dataset to encode is_implicit_vr : bool The element encoding scheme the dataset will be encoded with. is_little_endian : bool The byte ordering the dataset will be encoded in. Returns ------- bytes or None The encoded dataset (if successful), None if the encoding failed. """ # pylint: disable=broad-except fp = DicomBytesIO() fp.is_implicit_VR = is_implicit_vr fp.is_little_endian = is_little_endian try: write_dataset(fp, ds) except Exception as ex: LOGGER.error("pydicom.write_dataset() failed:") LOGGER.error(ex) fp.close() return None bytestring = fp.parent.getvalue() fp.close() return bytestring
def encode(ds, is_implicit_VR, is_little_endian): """ Given a pydicom Dataset, encode it to a byte stream Parameters ---------- ds - pydicom.dataset.Dataset The dataset to encode is_implicit_VR - bool Transfer syntax implicit/explicit VR is_little_endian - bool Transfer syntax byte ordering Returns ------- bytes or None The encoded dataset (if successful), None if encoding failed. """ f = DicomBytesIO() f.is_implicit_VR = is_implicit_VR f.is_little_endian = is_little_endian try: write_dataset(f, ds) except Exception as e: logger.error("pydicom.write_dataset() failed:") logger.error(e) f.close() return None rawstr = f.parent.getvalue() f.close() return rawstr
def encode(ds, is_implicit_VR, is_little_endian): f = DicomBytesIO() f.is_implicit_VR = is_implicit_VR f.is_little_endian = is_little_endian write_dataset(f, ds) rawstr = f.parent.getvalue() f.close() return rawstr
def check_empty_binary_element(value): setattr(ds, tag_name, value) elem = ds[tag_name] assert bool(elem.value) is False assert 0 == elem.VM assert elem.value == value fp = DicomBytesIO() filewriter.write_dataset(fp, ds) ds_read = dcmread(fp, force=True) assert ds_read[tag_name].value is None
def encode(ds: Dataset, is_implicit_vr: bool, is_little_endian: bool, deflated: bool = False) -> Optional[bytes]: """Encode a *pydicom* :class:`~pydicom.dataset.Dataset` `ds`. .. versionchanged:: 1.5 Added `deflated` keyword parameter Parameters ---------- ds : pydicom.dataset.Dataset The dataset to encode is_implicit_vr : bool The element encoding scheme the dataset will be encoded with, ``True`` for implicit VR, ``False`` for explicit VR. is_little_endian : bool The byte ordering the dataset will be encoded in, ``True`` for little endian, ``False`` for big endian. deflated : bool, optional ``True`` if the dataset is to be encoded using *Deflated Explicit VR Little Endian* transfer syntax (default ``False``). Returns ------- bytes or None The encoded dataset as :class:`bytes` (if successful) or ``None`` if the encoding failed. """ # pylint: disable=broad-except fp = DicomBytesIO() fp.is_implicit_VR = is_implicit_vr fp.is_little_endian = is_little_endian try: write_dataset(fp, ds) except Exception as exc: LOGGER.error("pydicom.write_dataset() failed:") LOGGER.exception(exc) fp.close() return None bytestring: bytes = fp.parent.getvalue() # type: ignore fp.close() if deflated: # Compress the encoded dataset compressor = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -zlib.MAX_WBITS) bytestring = compressor.compress(bytestring) bytestring += compressor.flush() bytestring += b'\x00' if len(bytestring) % 2 else b'' return bytestring
def test_empty_sequence_is_handled_as_array(self): ds = Dataset() ds.AcquisitionContextSequence = [] elem = ds['AcquisitionContextSequence'] assert bool(elem.value) is False assert 0 == elem.VM assert elem.value == [] fp = DicomBytesIO() fp.is_little_endian = True fp.is_implicit_VR = True filewriter.write_dataset(fp, ds) ds_read = dcmread(fp, force=True) elem = ds_read['AcquisitionContextSequence'] assert 0 == elem.VM assert elem.value == []
def encode(ds, is_implicit_vr, is_little_endian): """Encode a *pydicom* :class:`~pydicom.dataset.Dataset` `ds`. Parameters ---------- ds : pydicom.dataset.Dataset The dataset to encode is_implicit_vr : bool The element encoding scheme the dataset will be encoded with, ``True`` for implicit VR, ``False`` for explicit VR. is_little_endian : bool The byte ordering the dataset will be encoded in, ``True`` for little endian, ``False`` for big endian. Returns ------- bytes or None The encoded dataset as :class:`bytes` (if successful) or ``None`` if the encoding failed. """ # pylint: disable=broad-except fp = DicomBytesIO() fp.is_implicit_VR = is_implicit_vr fp.is_little_endian = is_little_endian try: write_dataset(fp, ds) except Exception as exc: LOGGER.error("pydicom.write_dataset() failed:") LOGGER.exception(exc) fp.close() return None bytestring = fp.parent.getvalue() fp.close() return bytestring
def test(): write_dataset(self.fp, ds)