I am trying to combine several dicom files in a larger one using python3 with pydicom library and encounters an error message I do not understand.
Let several dicom files be given. We assume that each dicom file contains a video. That is, after calling pydicom.dcmread and calling the pixel_array, I get a numpy array in shape (number_of_frames, rows, columns, 3), with number_of_frames > 0. We will also assume that each dicom only differs in number of frames.
So I read their pixel_array, use numpy to concatenate them, and gets a bigger video with more frames. I would like to save this bigger video as another dicom. So I write the new video into a filedataset and call save_as in pydicom library. It turns out that:
- If my concatenated video is small, then my code runs without problem.
- If my concatenated video is sufficiently large (one example would be
(6000,500,500,3)), then I get the following error:
Traceback (most recent call last):
File "...\Lib\site-packages\pydicom\tag.py", line 28, in tag_in_exception
yield
File "...\Lib\site-packages\pydicom\filewriter.py", line 662, in write_dataset
write_data_element(fp, dataset.get_item(tag), dataset_encoding)
File "...\Lib\site-packages\pydicom\filewriter.py", line 620, in write_data_element
fp.write_UL(0xFFFFFFFF if is_undefined_length else value_length)
File "...\Lib\site-packages\pydicom\filebase.py", line 119, in write_leUL
self.write(pack(b"<L", val))
^^^^^^^^^^^^^^^^
struct.error: argument out of range
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "...\dicom_concatenate.py", line 56, in work
first_dicom.save_as(absolute_save_path)
File "...\Lib\site-packages\pydicom\dataset.py", line 2061, in save_as
pydicom.dcmwrite(filename, self, write_like_original)
File "...\Lib\site-packages\pydicom\filewriter.py", line 1153, in dcmwrite
_write_dataset(fp, dataset, write_like_original)
File "...\Lib\site-packages\pydicom\filewriter.py", line 889, in _write_dataset
write_dataset(fp, get_item(dataset, slice(0x00010000, None)))
File "...\Lib\site-packages\pydicom\filewriter.py", line 661, in write_dataset
with tag_in_exception(tag):
File "...\Lib\contextlib.py", line 155, in __exit__
self.gen.throw(typ, value, traceback)
File "...\Lib\site-packages\pydicom\tag.py", line 32, in tag_in_exception
raise type(exc)(msg) from exc
struct.error: With tag (7fe0, 0010) got exception: argument out of range
Traceback (most recent call last):
File "...\Lib\site-packages\pydicom\tag.py", line 28, in tag_in_exception
yield
File "...\Lib\site-packages\pydicom\filewriter.py", line 662, in write_dataset
write_data_element(fp, dataset.get_item(tag), dataset_encoding)
File "...\Lib\site-packages\pydicom\filewriter.py", line 620, in write_data_element
fp.write_UL(0xFFFFFFFF if is_undefined_length else value_length)
File "...\Lib\site-packages\pydicom\filebase.py", line 119, in write_leUL
self.write(pack(b"<L", val))
^^^^^^^^^^^^^^^^
struct.error: argument out of range
I do not understand this error message. What exactly goes wrong? It seems my video is too large (?). Is there any way to help me save my concatenated video?
GitHub Version: https://github.com/pydicom/pydicom/issues/1849
Due to the way the DICOM standard handles element length, there's a limit to the size of uncompressed pixel data of about 4.3 GB (2^32 bytes), and I have a feeling that you've hit that limit with your larger video.
You'll have to use the compression/encapsulation route if that's the case.