Reputation: 1875
I used the following python code to compress a dicom file using GDCM:
import gdcm
reader = gdcm.PixmapReader()
reader.SetFileName(str(source))
if not reader.Read():
raise BaseException("Could not read (pixmap) %s" % source)
image = reader.GetPixmap()
change = gdcm.ImageChangeTransferSyntax()
change.SetForce(False)
change.SetCompressIconImage(False)
transfer_syntax = gdcm.TransferSyntax(gdcm.TransferSyntax.JPEG2000Lossless)
change.SetTransferSyntax(transfer_syntax)
change.SetInput(image)
if not change.Change():
raise BaseException("Could not change the Transfer Syntax: ")
....
In the line change.Change()
, there's an assertion in GDCM source which may faile:
Assertion `((fragment_size + 1)/2 ) * 2 == ((image_height * image_width * numcomps * (bitsallocated/8) + 1)/ 2 )* 2' failed.
Unfortunately python process would be killed in the case of assertion failure at GDCM. Is there a way to handle such assertions (without having checked the conditions at python before calling change.Change()
)?
Upvotes: 0
Views: 131
Reputation: 1875
Reluctantly I checked the Dicom integrity using pydicom
before compressing Dicom:
def check_dicom(dicom_file):
p = dicom.read_file(dicom_file)
assert p.SamplesPerPixel in (1, 3)
if p.BitsAllocated % 8 != 0:
return False
# assert( ((fragment_size + 1)/2 ) * 2 == ((image_height * image_width * numcomps * (bitsallocated/8) + 1)/ 2 )* 2 );
left = ((len(p.PixelData) + 1)/2 ) * 2
right = ((p.Rows * p.Columns * p.SamplesPerPixel * (p.BitsAllocated/8) + 1)/ 2 )* 2
if left != right:
raise BaseException("DICOM attributes are in conflict with pixel data size")
return True
Upvotes: 0
Reputation: 2220
A way to handle this is to start a child process to call the C library, then raise an exception if the child process dies because of a C assert statement.
Upvotes: 1