Ejemplo n.º 1
0
    def PIL_imagedata(self):
        image = self.image
        if image.format == 'JPEG':
            fp = image.fp
            fp.seek(0)
            return self._jpg_imagedata(fp)
        self.source = 'PIL'
        zlib = import_zlib()
        if not zlib: return
        myimage = image.convert('RGB')
        imgwidth, imgheight = myimage.size

        # this describes what is in the image itself
        # *NB* according to the spec you can only use the short form in inline images
        #imagedata=['BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /FlateDecode] ID]' % (imgwidth, imgheight,'RGB')]
        imagedata = [
            'BI /W %d /H %d /BPC 8 /CS /RGB /F [/A85 /Fl] ID' %
            (imgwidth, imgheight)
        ]

        #use a flate filter and Ascii Base 85 to compress
        raw = myimage.tostring()
        assert (len(raw) == imgwidth * imgheight,
                "Wrong amount of data for image")
        compressed = zlib.compress(raw)  #this bit is very fast...
        encoded = pdfutils._AsciiBase85Encode(
            compressed)  #...sadly this may not be
        #append in blocks of 60 characters
        pdfutils._chunker(encoded, imagedata)
        imagedata.append('EI')
        return (imagedata, imgwidth, imgheight)
Ejemplo n.º 2
0
    def PIL_imagedata(self):
        image = self.image
        if image.format=='JPEG':
            fp=image.fp
            fp.seek(0)
            return self._jpg_imagedata(fp)
        self.source = 'PIL'
        zlib = import_zlib()
        if not zlib: return

        # Use the colorSpace in the image
        if image.mode == 'CMYK':
            myimage = image
            colorSpace = 'DeviceCMYK'
            bpp = 4
        else:
            myimage = image.convert('RGB')
            colorSpace = 'RGB'
            bpp = 3
        imgwidth, imgheight = myimage.size

        # this describes what is in the image itself
        # *NB* according to the spec you can only use the short form in inline images
        imagedata=['BI /W %d /H %d /BPC 8 /CS /%s /F [%s/Fl] ID' % (imgwidth, imgheight,colorSpace, rl_config.useA85 and '/A85 ' or '')]

        #use a flate filter and, optionally, Ascii Base 85 to compress
        raw = myimage.tostring()
        assert len(raw) == imgwidth*imgheight*bpp, "Wrong amount of data for image"
        data = zlib.compress(raw)    #this bit is very fast...
        if rl_config.useA85:
            data = pdfutils._AsciiBase85Encode(data) #...sadly this may not be
        #append in blocks of 60 characters
        pdfutils._chunker(data,imagedata)
        imagedata.append('EI')
        return (imagedata, imgwidth, imgheight)
    def PIL_imagedata(self):
        self.source = 'PIL'
        zlib = import_zlib()
        if not zlib: return
        image = self.image
        myimage = image.convert('RGB')
        imgwidth, imgheight = myimage.size

        # this describes what is in the image itself
        # *NB* according to the spec you can only use the short form in inline images
        #imagedata=['BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /FlateDecode] ID]' % (imgwidth, imgheight,'RGB')]
        imagedata=['BI /W %d /H %d /BPC 8 /CS /RGB /F [/A85 /Fl] ID' % (imgwidth, imgheight)]

        #use a flate filter and Ascii Base 85 to compress
        raw = myimage.tostring()
        assert(len(raw) == imgwidth * imgheight, "Wrong amount of data for image")
        compressed = zlib.compress(raw)   #this bit is very fast...
        encoded = pdfutils._AsciiBase85Encode(compressed) #...sadly this isn't
        #write in blocks of (??) 60 characters per line to a list
        outstream = getStringIO(encoded)
        dataline = outstream.read(60)
        while dataline <> "":
            imagedata.append(dataline)
            self.binaryData.append(dataline)
            dataline = outstream.read(60)
        imagedata.append('EI')
        return (imagedata, imgwidth, imgheight)
Ejemplo n.º 4
0
    def PIL_imagedata(self):
        image = self.image
        if image.format=='JPEG':
            fp=image.fp
            fp.seek(0)
            return self._jpg_imagedata(fp)
        self.source = 'PIL'
        zlib = import_zlib()
        if not zlib: return
        myimage = image.convert('RGB')
        imgwidth, imgheight = myimage.size

        # this describes what is in the image itself
        # *NB* according to the spec you can only use the short form in inline images
        #imagedata=['BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /FlateDecode] ID]' % (imgwidth, imgheight,'RGB')]
        imagedata=['BI /W %d /H %d /BPC 8 /CS /RGB /F [/A85 /Fl] ID' % (imgwidth, imgheight)]

        #use a flate filter and Ascii Base 85 to compress
        raw = myimage.tostring()
        assert(len(raw) == imgwidth * imgheight, "Wrong amount of data for image")
        compressed = zlib.compress(raw)   #this bit is very fast...
        encoded = pdfutils._AsciiBase85Encode(compressed) #...sadly this may not be
        #append in blocks of 60 characters
        pdfutils._chunker(encoded,imagedata)
        imagedata.append('EI')
        return (imagedata, imgwidth, imgheight)
Ejemplo n.º 5
0
    def PIL_imagedata(self):
        image = self.image
        if image.format == 'JPEG':
            fp = image.fp
            fp.seek(0)
            return self._jpg_imagedata(fp)
        self.source = 'PIL'
        zlib = import_zlib()
        if not zlib: return

        bpc = 8
        # Use the colorSpace in the image
        if image.mode == 'CMYK':
            myimage = image
            colorSpace = 'DeviceCMYK'
            bpp = 4
        elif image.mode == '1':
            myimage = image
            colorSpace = 'DeviceGray'
            bpp = 1
            bpc = 1
        elif image.mode == 'L':
            myimage = image
            colorSpace = 'DeviceGray'
            bpp = 1
        else:
            myimage = image.convert('RGB')
            colorSpace = 'RGB'
            bpp = 3
        imgwidth, imgheight = myimage.size

        # this describes what is in the image itself
        # *NB* according to the spec you can only use the short form in inline images
        imagedata = [
            'BI /W %d /H %d /BPC %d /CS /%s /F [%s/Fl] ID' %
            (imgwidth, imgheight, bpc, colorSpace, rl_config.useA85 and '/A85 '
             or '')
        ]

        #use a flate filter and, optionally, Ascii Base 85 to compress
        raw = (myimage.tobytes
               if hasattr(myimage, 'tobytes') else myimage.tostring)()
        rowstride = (imgwidth * bpc * bpp + 7) >> 3
        assert len(
            raw) == rowstride * imgheight, "Wrong amount of data for image"
        data = zlib.compress(raw)  #this bit is very fast...
        if rl_config.useA85:
            data = asciiBase85Encode(data)  #...sadly this may not be
        #append in blocks of 60 characters
        pdfutils._chunker(data, imagedata)
        imagedata.append('EI')
        return (imagedata, imgwidth, imgheight)
Ejemplo n.º 6
0
    def cache_imagedata(self):
        image = self.image
        if not pdfutils.cachedImageExists(image):
            zlib = import_zlib()
            if not zlib: return
            if not haveImages: return
            pdfutils.cacheImageFile(image)

        #now we have one cached, slurp it in
        cachedname = os.path.splitext(image)[0] + '.a85'
        imagedata = open(cachedname, 'rb').readlines()
        #trim off newlines...
        imagedata = map(string.strip, imagedata)
        return imagedata
Ejemplo n.º 7
0
    def cache_imagedata(self):
        image = self.image
        if not pdfutils.cachedImageExists(image):
            zlib = import_zlib()
            if not zlib: return
            if not haveImages: return
            pdfutils.cacheImageFile(image)

        #now we have one cached, slurp it in
        cachedname = os.path.splitext(image)[0] + (rl_config.useA85 and '.a85' or '.bin')
        imagedata = open(cachedname,'rb').readlines()
        #trim off newlines...
        imagedata = list(map(string.strip, imagedata))
        return imagedata
    def cache_imagedata(self):
        image = self.image
        if not pdfutils.cachedImageExists(image):
            zlib = import_zlib()
            if not zlib: return
            if not PIL_Image: return
            pdfutils.cacheImageFile(image)

        #now we have one cached, slurp it in
        cachedname = os.path.splitext(image)[0] + '.a85'
        imagedata = open(cachedname,'rb').readlines()
        #trim off newlines...
        imagedata = map(string.strip, imagedata)
        return imagedata
Ejemplo n.º 9
0
    def PIL_imagedata(self):
        image = self.image
        if image.format == "JPEG":
            fp = image.fp
            fp.seek(0)
            return self._jpg_imagedata(fp)
        self.source = "PIL"
        zlib = import_zlib()
        if not zlib:
            return

        bpc = 8
        # Use the colorSpace in the image
        if image.mode == "CMYK":
            myimage = image
            colorSpace = "DeviceCMYK"
            bpp = 4
        elif image.mode == "1":
            myimage = image
            colorSpace = "DeviceGray"
            bpp = 1
            bpc = 1
        elif image.mode == "L":
            myimage = image
            colorSpace = "DeviceGray"
            bpp = 1
        else:
            myimage = image.convert("RGB")
            colorSpace = "RGB"
            bpp = 3
        imgwidth, imgheight = myimage.size

        # this describes what is in the image itself
        # *NB* according to the spec you can only use the short form in inline images
        imagedata = [
            "BI /W %d /H %d /BPC %d /CS /%s /F [%s/Fl] ID"
            % (imgwidth, imgheight, bpc, colorSpace, rl_config.useA85 and "/A85 " or "")
        ]

        # use a flate filter and, optionally, Ascii Base 85 to compress
        raw = (myimage.tobytes if hasattr(myimage, "tobytes") else myimage.tostring)()
        rowstride = (imgwidth * bpc * bpp + 7) >> 3
        assert len(raw) == rowstride * imgheight, "Wrong amount of data for image"
        data = zlib.compress(raw)  # this bit is very fast...
        if rl_config.useA85:
            data = asciiBase85Encode(data)  # ...sadly this may not be
        # append in blocks of 60 characters
        pdfutils._chunker(data, imagedata)
        imagedata.append("EI")
        return (imagedata, imgwidth, imgheight)
Ejemplo n.º 10
0
    def cache_imagedata(self):
        image = self.image
        if not pdfutils.cachedImageExists(image):
            zlib = import_zlib()
            if not zlib: return
            if not haveImages: return
            pdfutils.cacheImageFile(image)

        #now we have one cached, slurp it in
        cachedname = os.path.splitext(image)[0] + (rl_config.useA85 and '.a85'
                                                   or '.bin')
        imagedata = open(cachedname, 'rb').readlines()
        #trim off newlines...
        imagedata = [s.strip() for s in imagedata]
        return imagedata
Ejemplo n.º 11
0
def PIL_imagedata(self):
    '''
	Add ability to output greyscale and 1-bit PIL images without conversion to RGB.

	The upstream Python 2.7 version of reportlab converts 1-bit PIL images to RGB
	instead of saving them in a lower BPP format.  They have since added the following
	fix to their Python 3.3 branch, but it has not been back-ported.

	https://bitbucket.org/rptlab/reportlab/commits/177ddcbe4df6f9b461dac62612df9b8da3966a5d
	'''
    image = self.image
    if image.format == 'JPEG':
        fp = image.fp
        fp.seek(0)
        return self._jpg_imagedata(fp)

    from reportlab.lib.utils import import_zlib
    from reportlab import rl_config
    from reportlab.pdfbase.pdfutils import _chunker
    # in order to support both newer and older versions of reportlab
    try:
        from reportlab.pdfbase.pdfutils import _AsciiBase85Encode
    except ImportError:
        from reportlab.pdfbase.pdfutils import asciiBase85Encode as _AsciiBase85Encode

    self.source = 'PIL'
    zlib = import_zlib()
    if not zlib:
        return

    bpc = 8
    # Use the colorSpace in the image
    if image.mode == 'CMYK':
        myimage = image
        colorSpace = 'DeviceCMYK'
        bpp = 4
    elif image.mode == '1':
        myimage = image
        colorSpace = 'DeviceGray'
        bpp = 1
        bpc = 1
    elif image.mode == 'L':
        myimage = image
        colorSpace = 'DeviceGray'
        bpp = 1
    else:
        myimage = image.convert('RGB')
        colorSpace = 'RGB'
        bpp = 3
    imgwidth, imgheight = myimage.size

    # this describes what is in the image itself
    # *NB* according to the spec you can only use the short form in inline images

    imagedata = [
        'BI /W %d /H %d /BPC %d /CS /%s /F [%s/Fl] ID' %
        (imgwidth, imgheight, bpc, colorSpace, rl_config.useA85 and '/A85 '
         or '')
    ]

    # use a flate filter and, optionally, Ascii Base 85 to compress
    raw = myimage.tostring()
    rowstride = (imgwidth * bpc * bpp + 7) / 8
    assert len(raw) == rowstride * imgheight, "Wrong amount of data for image"
    data = zlib.compress(raw)  # this bit is very fast...

    if rl_config.useA85:
        # ...sadly this may not be
        data = _AsciiBase85Encode(data)
    # append in blocks of 60 characters
    _chunker(data, imagedata)
    imagedata.append('EI')
    return (imagedata, imgwidth, imgheight)
Ejemplo n.º 12
0
def PIL_imagedata(self):
	'''
	Add ability to output greyscale and 1-bit PIL images without conversion to RGB.

	The upstream Python 2.7 version of reportlab converts 1-bit PIL images to RGB
	instead of saving them in a lower BPP format.  They have since added the following
	fix to their Python 3.3 branch, but it has not been back-ported.

	https://bitbucket.org/rptlab/reportlab/commits/177ddcbe4df6f9b461dac62612df9b8da3966a5d
	'''
	image = self.image
	if image.format == 'JPEG':
		fp = image.fp
		fp.seek(0)
		return self._jpg_imagedata(fp)

	from reportlab.lib.utils import import_zlib
	from reportlab import rl_config
	from reportlab.pdfbase.pdfutils import _AsciiBase85Encode, _chunker

	self.source = 'PIL'
	zlib = import_zlib()
	if not zlib:
		return

	bpc = 8
	# Use the colorSpace in the image
	if image.mode == 'CMYK':
		myimage = image
		colorSpace = 'DeviceCMYK'
		bpp = 4
	elif image.mode == '1':
		myimage = image
		colorSpace = 'DeviceGray'
		bpp = 1
		bpc = 1
	elif image.mode == 'L':
		myimage = image
		colorSpace = 'DeviceGray'
		bpp = 1
	else:
		myimage = image.convert('RGB')
		colorSpace = 'RGB'
		bpp = 3
	imgwidth, imgheight = myimage.size

	# this describes what is in the image itself
	# *NB* according to the spec you can only use the short form in inline images

	imagedata = ['BI /W %d /H %d /BPC %d /CS /%s /F [%s/Fl] ID' %
				 (imgwidth, imgheight, bpc, colorSpace, rl_config.useA85 and '/A85 ' or '')]

	# use a flate filter and, optionally, Ascii Base 85 to compress
	raw = myimage.tostring()
	rowstride = (imgwidth * bpc * bpp + 7) / 8
	assert len(raw) == rowstride * imgheight, "Wrong amount of data for image"
	data = zlib.compress(raw)  # this bit is very fast...

	if rl_config.useA85:
		# ...sadly this may not be
		data = _AsciiBase85Encode(data)
	# append in blocks of 60 characters
	_chunker(data, imagedata)
	imagedata.append('EI')
	return (imagedata, imgwidth, imgheight)