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')
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.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
Do you guys have any idea what's the issue here ? It works fine with the original dicom.
Thanks !