Esempio n. 1
0
def decompress(inFile, outFile):
    print("Extract (or copy) %s to %s" % (inFile, outFile))
    try:
        reader = gdcm.ImageReader()
        reader.SetFileName(inFile)
        if not reader.Read():
            raise Exception("Unable to read %s with gdcm.ImageReader()" %
                            inFile)

        change = gdcm.ImageChangeTransferSyntax()
        change.SetTransferSyntax(
            gdcm.TransferSyntax(gdcm.TransferSyntax.ImplicitVRLittleEndian))
        change.SetInput(reader.GetImage())
        if not change.Change():
            raise Exception(
                "Unable to change %s with gdcm.ImageChangeTransferSyntax()" %
                inFile)

        writer = gdcm.ImageWriter()
        writer.SetFileName(outFile)
        writer.SetFile(reader.GetFile())
        writer.SetImage(change.GetOutput())
        if not writer.Write():
            raise Exception("Unable to write %s with gdcm.ImageWriter()" %
                            outFile)

    except Exception as e:
        sys.stderr.write("%s, copying instead" % str(e))
        if inFile is not outFile:
            shutil.copyfile(inFile, outFile)
Esempio n. 2
0
def decomp(file1, file2='temp_file_22.dcm', return_dcm=False):
    #file1 = '/data/gabriel/TAVR/TAVR_Sample_Study/IM-0003-1528.dcm'#sys.argv[1] # input filename

    #file2 = file2#sys.argv[2] # output filename
    if os.path.isfile(file2):
        os.remove(file2)
    reader = gdcm.ImageReader()
    reader.SetFileName(file1)
    reader.GetFile()
    if not reader.Read():
        sys.exit(1)

    change = gdcm.ImageChangeTransferSyntax()
    change.SetTransferSyntax(
        gdcm.TransferSyntax(gdcm.TransferSyntax.ImplicitVRLittleEndian))
    change.SetInput(reader.GetImage())
    if not change.Change():
        sys.exit(1)

    writer = gdcm.ImageWriter()
    writer.SetFileName(file2)
    writer.SetFile(reader.GetFile())
    writer.SetImage(change.GetOutput())
    if not writer.Write():
        sys.exit(1)

    if return_dcm:
        return pydicom.dcmread(file2, force=True)
    else:
        return pydicom.dcmread(file2, force=True).pixel_array
Esempio n. 3
0
    def cn(self, file1):
        file2 = "tmp00.dcm"  # output filename

        reader = gdcm.ImageReader()
        reader.SetFileName(file1)
        reader.Read()
        change = gdcm.ImageChangeTransferSyntax()
        change.SetTransferSyntax(
            gdcm.TransferSyntax(gdcm.TransferSyntax.ImplicitVRLittleEndian))
        change.SetInput(reader.GetImage())
        change.Change()
        writer = gdcm.ImageWriter()
        writer.SetFileName(file2)
        writer.SetFile(reader.GetFile())
        writer.SetImage(change.GetOutput())
        writer.Write()
        zz = pydicom.read_file("tmp00.dcm")
        os.remove("tmp00.dcm")
        return zz
Esempio n. 4
0
def dc(inputFile, outputFile):
	reader = gdcm.ImageReader()
	reader.SetFileName(inputFile)

	if not reader.Read():
		sys.exit(1)

	change = gdcm.ImageChangeTransferSyntax()
	change.SetTransferSyntax( gdcm.TransferSyntax(gdcm.TransferSyntax.ImplicitVRLittleEndian) )
	change.SetInput( reader.GetImage() )
	if not change.Change():
		sys.exit(1)

	writer = gdcm.ImageWriter()
	writer.SetFileName(outputFile)
	writer.SetFile( reader.GetFile() )
	writer.SetImage( change.GetOutput() )

	if not writer.Write():
		sys.exit(1)
Esempio n. 5
0
def method2(file1, file2):

    reader = gdcm.ImageReader()
    reader.SetFileName(file1)

    if not reader.Read():
        sys.exit(1)

    change = gdcm.ImageChangeTransferSyntax()
    change.SetTransferSyntax(
        gdcm.TransferSyntax(gdcm.TransferSyntax.ImplicitVRLittleEndian))
    change.SetInput(reader.GetImage())
    if not change.Change():
        sys.exit(1)

    writer = gdcm.ImageWriter()
    writer.SetFileName(file2)
    writer.SetFile(reader.GetFile())
    writer.SetImage(change.GetOutput())

    if not writer.Write():
        raise ValueError
Esempio n. 6
0
if __name__ == "__main__":
    mypath='/home/vishnu/Projects/series-000001/'
    oppath='/home/vishnu/Projects/output/'
    onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
    for files in onlyfiles:
        file1= mypath+files
        file2 = oppath+files

  #file1 = sys.argv[1] # input filename
  #file2 = sys.argv[2] # output filename

        reader = gdcm.ImageReader()
        reader.SetFileName( file1 )

        if not reader.Read():
            sys.exit(1)

        change = gdcm.ImageChangeTransferSyntax()
        change.SetTransferSyntax( gdcm.TransferSyntax(gdcm.TransferSyntax.ImplicitVRLittleEndian) )
        change.SetInput( reader.GetImage() )
        if not change.Change():
            sys.exit(1)

        writer = gdcm.ImageWriter()
        writer.SetFileName( file2 )
        writer.SetFile( reader.GetFile() )
        writer.SetImage( change.GetOutput() )

        if not writer.Write():
            sys.exit(1)
Esempio n. 7
0
def _rle_encode(src: bytes, **kwargs: Any) -> bytes:
    """Return RLE encoded image data from `src`.

    Parameters
    ----------
    src : bytes
        The raw image frame data to be encoded.
    **kwargs
        Required parameters:

        * `rows`: int
        * `columns`: int
        * `samples_per_pixel`: int
        * `number_of_frames`: int
        * `bits_allocated`: int
        * `bits_stored`: int
        * `pixel_representation`: int
        * `photometric_interpretation`: str

    Returns
    -------
    bytes
        The encoded image data.
    """
    # Check the parameters are valid for RLE encoding with GDCM
    samples_per_pixel: int = kwargs['samples_per_pixel']
    bits_allocated: int = kwargs['bits_allocated']

    # Bug up to v3.0.9 (Apr 2021) in handling 32-bit, 3 sample/px data
    gdcm_version = [int(c) for c in gdcm.Version.GetVersion().split('.')]
    if gdcm_version < [3, 1, 0]:
        if bits_allocated == 32 and samples_per_pixel == 3:
            raise RuntimeError(
                "The 'gdcm' plugin is unable to RLE encode 32-bit, 3 "
                "samples/px data with GDCM v3.0.9 or older")

    if bits_allocated > 32:
        raise ValueError(
            f"The 'gdcm' plugin is unable to encode {bits_allocated}-bit data")

    # Create a gdcm.Image with the uncompressed `src` data
    image = _create_gdcm_image(src, **kwargs)

    # Converts an image to match the set transfer syntax
    converter = gdcm.ImageChangeTransferSyntax()

    # Set up the converter with the intended transfer syntax...
    ts = gdcm.TransferSyntax.GetTSType(kwargs['transfer_syntax_uid'])
    converter.SetTransferSyntax(gdcm.TransferSyntax(ts))
    # ...and image to be converted
    converter.SetInput(image)

    # Perform the conversion, returns bool
    # 'PALETTE COLOR' and a lossy transfer syntax will return False
    result = converter.Change()
    if not result:
        raise RuntimeError(
            "An error occurred with the 'gdcm' plugin: "
            "ImageChangeTransferSyntax.Change() returned a failure result")

    # The converted image as gdcm.Image
    # Weirdly, reusing `image` as the variable name causes a segfault here
    output = converter.GetOutput()
    # The element's value is the encapsulated encoded pixel data
    seq = output.GetDataElement().GetSequenceOfFragments()

    # RLECodec::Code() uses only 1 fragment per frame
    if seq is None or seq.GetNumberOfFragments() != 1:
        # Covers both no sequence and unexpected number of fragments
        raise RuntimeError(
            "An error occurred with the 'gdcm' plugin: unexpected number of "
            "fragments found in the 'Pixel Data'")

    fragment: str = seq.GetFragment(0).GetByteValue().GetBuffer()
    return fragment.encode("utf-8", "surrogateescape")
Esempio n. 8
0
def _rle_encode(src: bytes, **kwargs: Any) -> bytes:
    """Return RLE encoded image data from `src`.

    Parameters
    ----------
    src : bytes
        The raw image frame data to be encoded.
    **kwargs
        Required parameters:

        * `rows`: int
        * `columns`: int
        * `samples_per_pixel`: int
        * `number_of_frames`: int
        * `bits_allocated`: int
        * `bits_stored`: int
        * `pixel_representation`: int
        * `photometric_interpretation`: str

    Returns
    -------
    bytes
        The encoded image data.
    """
    # Check the parameters are valid for RLE encoding with GDCM
    rows = kwargs['rows']
    columns = kwargs['columns']
    samples_per_pixel = kwargs['samples_per_pixel']
    number_of_frames = kwargs['number_of_frames']
    pixel_representation = kwargs['pixel_representation']
    bits_allocated = kwargs['bits_allocated']
    bits_stored = kwargs['bits_stored']
    photometric_interpretation = kwargs['photometric_interpretation']

    # Bug up to v3.0.9 (Apr 2021) in handling 32-bit, 3 sample/px data
    gdcm_version = [int(c) for c in gdcm.Version.GetVersion().split('.')]
    if gdcm_version < [3, 0, 10]:
        if bits_allocated == 32 and samples_per_pixel == 3:
            raise RuntimeError(
                "The 'gdcm' plugin is unable to RLE encode 32-bit, 3 "
                "samples/px data with GDCM v3.0.9 or older"
            )

    if bits_allocated > 32:
        raise ValueError(
            f"The 'gdcm' plugin is unable to encode {bits_allocated}-bit data"
        )

    # Create a gdcm.Image with the uncompressed `src` data
    pi = gdcm.PhotometricInterpretation.GetPIType(
        photometric_interpretation
    )

    # GDCM's null photometric interpretation gets used for invalid values
    if pi == gdcm.PhotometricInterpretation.PI_END:
        raise ValueError(
            "An error occurred with the 'gdcm' plugin: invalid photometric "
            f"interpretation '{photometric_interpretation}'"
        )

    # `src` uses little-endian byte ordering
    ts = gdcm.TransferSyntax.ImplicitVRLittleEndian

    # Must use ImageWriter().GetImage() to create a gdcmImage
    #   also have to make sure `writer` doesn't go out of scope
    writer = gdcm.ImageWriter()
    image = writer.GetImage()
    image.SetNumberOfDimensions(2)
    image.SetDimensions((columns, rows, 1))
    image.SetPhotometricInterpretation(
        gdcm.PhotometricInterpretation(pi)
    )
    image.SetTransferSyntax(gdcm.TransferSyntax(ts))

    pixel_format = gdcm.PixelFormat(
        samples_per_pixel,
        bits_allocated,
        bits_stored,
        bits_stored - 1,
        pixel_representation
    )
    image.SetPixelFormat(pixel_format)
    if samples_per_pixel > 1:
        # Default `src` is planar configuration 0 (i.e. R1 G1 B1 R2 G2 B2)
        image.SetPlanarConfiguration(0)

    # Add the Pixel Data element and set the value to `src`
    elem = gdcm.DataElement(gdcm.Tag(0x7FE0, 0x0010))
    elem.SetByteStringValue(src)
    image.SetDataElement(elem)

    # Converts an image to match the set transfer syntax
    converter = gdcm.ImageChangeTransferSyntax()

    # Set up the converter with the intended transfer syntax...
    rle = gdcm.TransferSyntax.GetTSType(kwargs['transfer_syntax_uid'])
    converter.SetTransferSyntax(gdcm.TransferSyntax(rle))
    # ...and image to be converted
    converter.SetInput(image)

    # Perform the conversion, returns bool
    # 'PALETTE COLOR' and a lossy transfer syntax will return False
    result = converter.Change()
    if not result:
        raise RuntimeError(
            "An error occurred with the 'gdcm' plugin: "
            "ImageChangeTransferSyntax.Change() returned a failure result"
        )

    # A new gdcmImage with the converted pixel data element
    image = converter.GetOutput()

    # The element's value is the encapsulated encoded pixel data
    seq = image.GetDataElement().GetSequenceOfFragments()

    # RLECodec::Code() uses only 1 fragment per frame
    if seq is None or seq.GetNumberOfFragments() != 1:
        # Covers both no sequence and unexpected number of fragments
        raise RuntimeError(
            "An error occurred with the 'gdcm' plugin: unexpected number of "
            "fragments found in the 'Pixel Data'"
        )

    fragment = seq.GetFragment(0).GetByteValue().GetBuffer()
    return cast(bytes, fragment.encode("utf-8", "surrogateescape"))