Skip to content

Commit

Permalink
initial attempt to recompress dicom
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandip117 committed Aug 27, 2024
1 parent fab360a commit d4d5956
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 14 additions & 3 deletions dicommake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> │ "
Expand Down Expand Up @@ -144,15 +146,24 @@ 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()
ds.SOPInstanceUID = pydicom.uid.generate_uid()

# 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]]:
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d4d5956

Please sign in to comment.