# Buggy code
def _get_PIL_supported_compressed_pixeldata(self):
    """Use PIL to decompress compressed Pixel Data.

    Returns
    -------
    bytes or str
        The decompressed Pixel Data

    Raises
    ------
    ImportError
        If PIL is not available.
    NotImplementedError
        If unable to decompress the Pixel Data.
    """
    if not have_pillow:
        msg = (
            "The pillow package is required to use pixel_array for "
            "this transfer syntax {0}, and pillow could not be "
            "imported.".format(self.file_meta.TransferSyntaxUID)
        )
        raise ImportError(msg)
    # decompress here
    if (
        self.file_meta.TransferSyntaxUID
        in pydicom.uid.JPEGLossyCompressedPixelTransferSyntaxes
    ):
        if self.BitsAllocated > 8:
            raise NotImplementedError("JPEG Lossy only supported if Bits Allocated = 8")
        generic_jpeg_file_header = (
            b"\xff\xd8\xff\xe0\x00\x10JFIF\x00",
            b"\x01\x01\x01\x00\x01\x00\x01\x00",
            b"\x00",
        )
        frame_start_from = 2
    elif (
        self.file_meta.TransferSyntaxUID
        in pydicom.uid.JPEG2000CompressedPixelTransferSyntaxes
    ):
        generic_jpeg_file_header = b""
        # generic_jpeg_file_header = (b'\x00\x00\x00\x0C\x6A\x50'
        #                             b'\x20\x20\x0D\x0A\x87\x0A')
        frame_start_from = 0
    else:
        generic_jpeg_file_header = b""
        frame_start_from = 0
    try:
        UncompressedPixelData = ""
        if "NumberOfFrames" in self and self.NumberOfFrames > 1:
            # multiple compressed frames
            CompressedPixelDataSeq = pydicom.encaps.decode_data_sequence(self.PixelData)
            for frame in CompressedPixelDataSeq:
                data = generic_jpeg_file_header + frame[frame_start_from:]
                fio = io.BytesIO(data)
                try:
                    decompressed_image = PILImg.open(fio)
                except IOError as e:
                    try:
                        message = str(e)
                    except BaseException:
                        try:
                            message = unicode(e)
                        except BaseException:
                            message = ""
                    raise NotImplementedError(message)
                UncompressedPixelData += decompressed_image.tobytes()
        else:
            # single compressed frame
            UncompressedPixelData = pydicom.encaps.defragment_data(self.PixelData)
            UncompressedPixelData = (
                generic_jpeg_file_header + UncompressedPixelData[frame_start_from:]
            )
            try:
                fio = io.BytesIO(UncompressedPixelData)
                decompressed_image = PILImg.open(fio)
            except IOError as e:
                try:
                    message = str(e)
                except BaseException:
                    try:
                        message = unicode(e)
                    except BaseException:
                        message = ""
                raise NotImplementedError(message)
            UncompressedPixelData = decompressed_image.tobytes()
    except BaseException:
        raise
    return UncompressedPixelData


# Traceback (TypeError)
Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default, Jul  2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import pydicom
dcm = pydicom.read_file("./charite20161216-1-173.dcm")
dcm.pixel_array
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/var/pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages/pydicom/dataset.py", line 709, in pixel_array
return self._get_pixel_array()
File "/usr/local/var/pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages/pydicom/dataset.py", line 695, in _get_pixel_array
self._pixel_array = self._compressed_pixel_data_numpy()
File "/usr/local/var/pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages/pydicom/dataset.py", line 583, in _compressed_pixel_data_numpy
UncompressedPixelData = self._get_PIL_supported_compressed_pixeldata()
File "/usr/local/var/pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages/pydicom/dataset.py", line 648, in _get_PIL_supported_compressed_pixeldata
UncompressedPixelData += decompressed_image.tobytes()
TypeError: Can't convert 'bytes' object to str implicitly
dcm.pixel_array.shape
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/var/pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages/pydicom/dataset.py", line 709, in pixel_array
return self._get_pixel_array()
File "/usr/local/var/pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages/pydicom/dataset.py", line 695, in _get_pixel_array
self._pixel_array = self._compressed_pixel_data_numpy()
File "/usr/local/var/pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages/pydicom/dataset.py", line 583, in _compressed_pixel_data_numpy
UncompressedPixelData = self._get_PIL_supported_compressed_pixeldata()
File "/usr/local/var/pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages/pydicom/dataset.py", line 648, in _get_PIL_supported_compressed_pixeldata
UncompressedPixelData += decompressed_image.tobytes()
TypeError: Can't convert 'bytes' object to str implicitly

# Fixed code
def _get_PIL_supported_compressed_pixeldata(self):
    """Use PIL to decompress compressed Pixel Data.

    Returns
    -------
    bytes or str
        The decompressed Pixel Data

    Raises
    ------
    ImportError
        If PIL is not available.
    NotImplementedError
        If unable to decompress the Pixel Data.
    """
    if not have_pillow:
        msg = (
            "The pillow package is required to use pixel_array for "
            "this transfer syntax {0}, and pillow could not be "
            "imported.".format(self.file_meta.TransferSyntaxUID)
        )
        raise ImportError(msg)
    # decompress here
    if (
        self.file_meta.TransferSyntaxUID
        in pydicom.uid.JPEGLossyCompressedPixelTransferSyntaxes
    ):
        if self.BitsAllocated > 8:
            raise NotImplementedError("JPEG Lossy only supported if Bits Allocated = 8")
        generic_jpeg_file_header = (
            b"\xff\xd8\xff\xe0\x00\x10JFIF\x00",
            b"\x01\x01\x01\x00\x01\x00\x01\x00",
            b"\x00",
        )
        frame_start_from = 2
    elif (
        self.file_meta.TransferSyntaxUID
        in pydicom.uid.JPEG2000CompressedPixelTransferSyntaxes
    ):
        generic_jpeg_file_header = b""
        # generic_jpeg_file_header = (b'\x00\x00\x00\x0C\x6A\x50'
        #                             b'\x20\x20\x0D\x0A\x87\x0A')
        frame_start_from = 0
    else:
        generic_jpeg_file_header = b""
        frame_start_from = 0
    try:
        UncompressedPixelData = bytearray()
        if "NumberOfFrames" in self and self.NumberOfFrames > 1:
            # multiple compressed frames
            CompressedPixelDataSeq = pydicom.encaps.decode_data_sequence(self.PixelData)
            for frame in CompressedPixelDataSeq:
                data = generic_jpeg_file_header + frame[frame_start_from:]
                fio = io.BytesIO(data)
                try:
                    decompressed_image = PILImg.open(fio)
                except IOError as e:
                    raise NotImplementedError(e.strerror)
                UncompressedPixelData += decompressed_image.tobytes()
        else:
            # single compressed frame
            UncompressedPixelData = pydicom.encaps.defragment_data(self.PixelData)
            UncompressedPixelData = (
                generic_jpeg_file_header + UncompressedPixelData[frame_start_from:]
            )
            try:
                fio = io.BytesIO(UncompressedPixelData)
                decompressed_image = PILImg.open(fio)
            except IOError as e:
                raise NotImplementedError(e.strerror)
            UncompressedPixelData = decompressed_image.tobytes()
    except BaseException:
        raise
    return bytes(UncompressedPixelData)