Can't read compressed data

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
RobinFrcd
Posts: 3
Joined: Mon, 2022-10-10, 14:40

Can't read compressed data

#1 Post by RobinFrcd »

Hi,
I have an uncompressed dicom I want to compress.

I'm using this portion of code to di it:

Code: Select all

import pydicom
from deid.dicom import replace_identifiers
from PIL import Image
from io import BytesIO
from pydicom.encaps import encapsulate

dcm = pydicom.read_file('example.dcm')

with BytesIO() as bio:
    Image.fromarray(dcm.pixel_array).save(
        bio,
        optimize=True,
        quality=95,
        format="JPEG",
        lossless=True,
    )
    dcm.PixelData = encapsulate([bio.getvalue()])
    dcm.file_meta.TransferSyntaxUID = pydicom.uid.JPEGBaseline
    
dcm.is_little_endian = True
dcm.is_implicit_VR = False
dcm[0x7fe0, 0x0010].VR = "OB"
dcm.save_as('dcm_comp.dcm')
The thing is, when I want to read the new dicom with dcmtk it fails with
I: reading DICOM file: /tmp/dcm/dcm_comp.dcm
I: preparing pixel data
E: can't change to unencapsulated representation for pixel data
E: can't determine 'PhotometricInterpretation' of decompressed image
E: mandatory attribute 'PhotometricInterpretation' is missing or can't be determined
F: Missing attribute
PhotometricInterpretation is set (to RGB), I've tried to change it to other YRB values but it didn't help. I've also tried other TransferSyntaxUID but it didn't help either.

Do you guys have any idea what's the issue here ? It works fine with the original dicom.

Thanks !

Marco Eichelberg
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 1437
Joined: Tue, 2004-11-02, 17:22
Location: Oldenburg, Germany
Contact:

Re: Can't read compressed data

#2 Post by Marco Eichelberg »

Try running the compressed file through "dciodvfy" from dicom3tools - in most cases you will get a meaningful error message.

RobinFrcd
Posts: 3
Joined: Mon, 2022-10-10, 14:40

Re: Can't read compressed data

#3 Post by RobinFrcd »

Thank you for your answer

Code: Select all

$ dciodvfy ./dcm_comp.dcm
Warning - Missing attribute or value that would be needed to build DICOMDIR - Patient ID
Warning - Missing attribute or value that would be needed to build DICOMDIR - Study Date
Warning - Missing attribute or value that would be needed to build DICOMDIR - Study Time
Warning - Missing attribute or value that would be needed to build DICOMDIR - Study ID
Error - Information Object Not found
The warnings fields are not mandatory, right ?

Marco Eichelberg
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 1437
Joined: Tue, 2004-11-02, 17:22
Location: Oldenburg, Germany
Contact:

Re: Can't read compressed data

#4 Post by Marco Eichelberg »

You can ignore the warnings, but "Information Object Not found" indicates some fundamental problem. SOP Class UID missing?

Marco Eichelberg
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 1437
Joined: Tue, 2004-11-02, 17:22
Location: Oldenburg, Germany
Contact:

Re: Can't read compressed data

#5 Post by Marco Eichelberg »

I can, however, successfully run "dcm_comp.dcm" through DCMTK's dcmdjpeg tool, which decompresses it. I can also run dcm2pnm over the compressed image to create a bitmap rendering. However, if you want to see correct colors, the PhotometricInterpretation needs to be set to YBR_FULL before the image is decompressed.

RobinFrcd
Posts: 3
Joined: Mon, 2022-10-10, 14:40

Re: Can't read compressed data

#6 Post by RobinFrcd »

Thanks for your answer !

But dcm2pnm doesn't work at all for me:

Code: Select all

dcm2pnm -d -v ~/Downloads/dcms/dcm_comp.dcm ~/Downloads/dcms/dcm_comp.jpg
I: reading DICOM file: /home/robz/Downloads/dcms/dcm_comp.dcm
W: DcmItem: Length of element (7fe0,0010) is not a multiple of 2 (VR=OW)
I: preparing pixel data
E: can't change to unencapsulated representation for pixel data
E: can't determine 'PhotometricInterpretation' of decompressed image
E: mandatory attribute 'PhotometricInterpretation' is missing or can't be determined
F: Missing attribute
And according to dcmdump, PhotometricInterpretation is set.

Code: Select all

dcmdump -d -v ~/Downloads/dcms/dcm_comp.dcm +P PhotometricInterpretation     
W: DcmItem: Length of element (7fe0,0010) is not a multiple of 2 (VR=OW)
(0028,0004) CS [RGB]                                    #   4, 1 PhotometricInterpretation
How could it work for you but not me, on the same file ?

I'm running:

Code: Select all

$dcmtk: dcm2pnm v3.6.6 2021-01-14 $

dcm2pnm: Convert DICOM images to PGM/PPM, PNG, TIFF or BMP

Host type: Debian
Character encoding: system default (unknown)

External libraries used:
- ZLIB, Version 1.2.11
- LIBTIFF, Version 4.3.0
- LIBPNG, Version 1.6.37

Marco Eichelberg
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 1437
Joined: Tue, 2004-11-02, 17:22
Location: Oldenburg, Germany
Contact:

Re: Can't read compressed data

#7 Post by Marco Eichelberg »

Well, as I said:
  • adjust the PhotometricInterpretation to YBR_FULL (e.g. using dcmodify)
  • decompress the image using dcmdjpeg
  • then apply dcm2pnm to render the image
Alternatively, you can use "dcmj2pnm", which will cover steps 2 and 3 in one tool.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], George, Google [Bot], Semrush [Bot] and 1 guest