Saving compressed pixel data back to dicom file

416 views
Skip to first unread message

Kourosh Kalayeh

unread,
Jun 5, 2023, 2:06:43 PM6/5/23
to pyd...@googlegroups.com

Hi all,

I have a dicom file that I can read with pydicom without problem. Below is
some relevant information of this file.

(0002, 0010) Transfer Syntax UID                 UI: JPEG Baseline (Process 1)
(0028, 0002) Samples per Pixel                   US: 3
(0028, 0004) Photometric Interpretation          CS: 'YBR_FULL_422'
(0028, 0006) Planar Configuration                US: 0
(0028, 0008) Number of Frames                    IS: '852'
(0028, 0009) Frame Increment Pointer             AT: (0018, 1063)
(0028, 0010) Rows                                US: 649
(0028, 0011) Columns                             US: 1170
(0028, 0100) Bits Allocated                      US: 8
(0028, 0101) Bits Stored                         US: 8
(0028, 0102) High Bit                            US: 7
(0028, 0103) Pixel Representation                US: 0
(0028, 2110) Lossy Image Compression             CS: '01'
(0028, 2112) Lossy Image Compression Ratio       DS: '16.8'
(7fe0, 0010) Pixel Data                          OB: Array of 115504762 elements

I am trying to change some of the pixels and store it back to the DICOM file. So, I
have the following,

ds = pydicom.dcmread("path/to/dicom_file")
px_data = ds.pixel_array
n_frms = px_data.shape[0]
# my changes
px_data[0:n_frms, 0:50, :, :] = 0
px_data[0:n_frms, -90:-1, :, :] = 0

ds.PixelData = px_data.tobytes()
ds.save_as("path/to/new_dicom_file")

However, when saving, I get the following error:

ValueError: (7FE0,0010) Pixel Data has an undefined length indicating that it's compressed,
but the data isn't encapsulated as required. See pydicom.encaps.encapsulate() for more information.

As the error suggesting and based on my search on the web, I have to compress
the pixel data back to '1.2.840.10008.1.2.4.50' format. But, so far, I have
failed to do so. I was wondering if anyone can give my some pointers.

Thanks,
Kourosh

Darcy Mason

unread,
Jun 5, 2023, 2:17:41 PM6/5/23
to pydicom
Try using Dataset's decompress method after reading the file, which will update the Dataset properly.

That will save in uncompressed format, however.  If you need to save back to a compressed format, there isn't support for that currently in pydicom.



Kourosh Kalayeh

unread,
Jun 5, 2023, 3:44:18 PM6/5/23
to pydicom
Thanks for the response.  Two questions:
1. Can I use 3rd party library like pylibjpeg or GDCM to compress to the appropriate format and then save it?
2. Saving the decompressed data resulted in the following warning:
".venvs/main_env/lib/python3.10/site-packages/pydicom/pixel_data_handlers/numpy_handler.py:250: UserWarning: The Photometric Interpretation of the dataset is YBR_FULL_422, however the length of the pixel data (1940847480 bytes) is a third larger than expected (1293898320 bytes) which indicates that this may be incorrect. You may need to change the Photometric Interpretation to the correct value."
Also, the images in the new DICOM file is not showable in DICOM viewers like Horos or Osirix. 

Thanks, 
Kourosh 

Darcy Mason

unread,
Jun 5, 2023, 4:00:51 PM6/5/23
to pyd...@googlegroups.com
On 1, in theory, Yes, but we don't have the mechanisms in place yet. 

On 2, I don't have the expertise on color images to answer directly, but suggest do a search for pydicom and ybr_full or similar, and perhaps scaramallion (user name) who has often commented on these kinds of errors. 

Reply all
Reply to author
Forward
0 new messages