From d4d59567a05de9c5afd989ac29ce98525b3d7f2c Mon Sep 17 00:00:00 2001 From: Sandip Samal Date: Tue, 27 Aug 2024 17:10:36 -0400 Subject: [PATCH] initial attempt to recompress dicom --- dicommake.py | 17 ++++++++++++++--- requirements.txt | 4 ++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/dicommake.py b/dicommake.py index 69604bc..9d57087 100644 --- a/dicommake.py +++ b/dicommake.py @@ -19,7 +19,9 @@ from PIL import Image import numpy as np from loguru import logger -from pydicom.uid import ExplicitVRLittleEndian +from pydicom.uid import ExplicitVRLittleEndian,JPEG2000, JPEG2000Lossless +import pylibjpeg +from pydicom.uid import RLELossless LOG = logger.debug logger_format = ( "{time:YYYY-MM-DD HH:mm:ss} │ " @@ -144,8 +146,10 @@ def npimage_get(image): # file itself isn't compressed, saving it as a DICOM file would throw error. # The below fix handles this. # We change the transfer syntax UID (https://github.com/pydicom/pydicom/issues/1109) - ds.file_meta.TransferSyntaxUID = ExplicitVRLittleEndian - ds.PixelData = np_image.tobytes() + ds.PixelData = np_image.tobytes() + ds.file_meta.TransferSyntaxUID = ExplicitVRLittleEndian + + ds.AcquisitionTime = AcquisitionTime() ds.AcquisitionDate = AcquisitionDate() ds.SeriesInstanceUID = pydicom.uid.generate_uid() @@ -153,6 +157,13 @@ def npimage_get(image): # NB! If this is not set, images will not render properly in Cornerstone ds.PlanarConfiguration = 0 + ds.compress(RLELossless) + # Recompress the image data using JPEG2000 + # compressed_pixel_data = pylibjpeg.encode(np_image.tobytes(), transfer_syntax='JPEG2000') + + # Update the DICOM dataset + # ds.PixelData = compressed_pixel_data + ds.file_meta.TransferSyntaxUID = JPEG2000 return ds def doubly_map(x: PathMapper, y: PathMapper) -> Iterable[tuple[Path, Path, Path, Path]]: diff --git a/requirements.txt b/requirements.txt index 3b27252..0376c40 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,7 @@ loguru pftag==1.2.22 pflog==1.2.26 pftel-client +# for bug fix on transfer syntax errors +pylibjpeg +pylibjpeg-libjpeg +python-gdcm \ No newline at end of file