Exemple #1
0
    def _extract_transcoded(self):
        im = None
        if self.mode == 'RGB' and self.bits_per_component == 8:
            # No point in accessing the buffer here, size qpdf decodes to 3-byte
            # RGB and Pillow needs RGBX for raw access
            data = self.read_bytes()
            im = Image.frombytes('RGB', self.size, data)
        elif self.mode in ('L', 'P') and self.bits_per_component == 8:
            buffer = self.get_stream_buffer()
            stride = 0  # tell Pillow to calculate stride from line width
            ystep = 1  # image is top to bottom in memory
            im = Image.frombuffer('L', self.size, buffer, "raw", 'L', stride,
                                  ystep)
            if self.mode == 'P':
                base_mode, palette = self.palette
                if base_mode in ('RGB', 'L'):
                    im.putpalette(palette, rawmode=base_mode)
                else:
                    raise NotImplementedError('palette with ' + base_mode)
        elif self.bits_per_component == 1:
            if self.filters and self.filters[0] == '/JBIG2Decode':
                if not jbig2.jbig2dec_available():
                    raise DependencyError(
                        "jbig2dec - not installed or installed version is too old "
                        "(older than version 0.15)")
                jbig2_globals_obj = self.filter_decodeparms[0][1].get(
                    '/JBIG2Globals')
                im = jbig2.extract_jbig2(self.obj, jbig2_globals_obj)
            else:
                data = self.read_bytes()
                im = Image.frombytes('1', self.size, data)
        else:
            raise UnsupportedImageTypeError(repr(self))

        if self.mode == 'P' and self.bits_per_component == 1:
            # Fix paletted 1-bit images
            base_mode, palette = self.palette
            if base_mode == 'RGB' and palette != b'\x00\x00\x00\xff\xff\xff':
                im = im.convert('P')
                im.putpalette(palette, rawmode=base_mode)
                gp = im.getpalette()
                gp[765:768] = gp[3:6]  # work around Pillow bug
                im.putpalette(gp)
            elif base_mode == 'L' and palette != b'\x00\xff':
                im = im.convert('P')
                im.putpalette(palette, rawmode=base_mode)
                gp = im.getpalette()
                gp[255] = gp[1]  # work around Pillow bug
                im.putpalette(gp)

        if self.colorspace == '/ICCBased':
            im.info['icc_profile'] = self.icc.tobytes()

        return im
Exemple #2
0
    def _extract_transcoded_1bit(self) -> Image.Image:
        if self.filters and self.filters[0] == '/JBIG2Decode':
            if not jbig2.jbig2dec_available():
                raise DependencyError(
                    "jbig2dec - not installed or installed version is too old "
                    "(older than version 0.15)"
                )
            jbig2_globals_obj = self.filter_decodeparms[0][1].get('/JBIG2Globals')
            im = jbig2.extract_jbig2(self.obj, jbig2_globals_obj)
        else:
            if self.mode in ('RGB', 'CMYK'):
                raise UnsupportedImageTypeError("1-bit RGB and CMYK are not supported")
            data = self.read_bytes()
            im = Image.frombytes('1', self.size, data)

        if self.palette is not None:
            base_mode, palette = self.palette
            im = _transcoding.fix_1bit_palette_image(im, base_mode, palette)

        return im