Пример #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)
Пример #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)
Пример #3
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)
 def jpg_imagedata(self):
     #directly process JPEG files
     #open file, needs some error handling!!
     self.source = 'JPEG'
     imageFile = open(self.image, 'rb')
     info = pdfutils.readJPEGInfo(imageFile)
     imgwidth, imgheight = info[0], info[1]
     if info[2] == 1:
         colorSpace = 'DeviceGray'
     elif info[2] == 3:
         colorSpace = 'DeviceRGB'
     else: #maybe should generate an error, is this right for CMYK?
         colorSpace = 'DeviceCMYK'
     imageFile.seek(0) #reset file pointer
     imagedata = []
     #imagedata.append('BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /DCTDecode] ID' % (info[0], info[1], colorSpace))
     imagedata.append('BI /W %d /H %d /BPC 8 /CS /%s /F [/A85 /DCT] ID' % (imgwidth, imgheight, colorSpace))
     #write in blocks of (??) 60 characters per line to a list
     compressed = imageFile.read()
     encoded = pdfutils._AsciiBase85Encode(compressed)
     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)
    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)
Пример #6
0
def main(filters,fileNames):
    import base64, zlib, bz2
    from xml.sax.saxutils import escape
    from reportlab.pdfbase import pdfutils
    if isinstance(filters,str):
        filters = filters.split()
    for fn in fileNames:
        f = open(fn,'rb')
        try:
            data = f.read()
        finally:
            f.close()
        for filter in filters:
            filter = filter.strip()
            if not filter: continue
            if filter=='base64':
                data = base64.standard_b64encode(data)
            elif filter=='ascii85':
                data = pdfutils._AsciiBase85Encode(data)
            elif filter=='bzip2':
                data = bz2.compress(data)
            elif filter=='gzip':
                data = zlib.compress(data)
            else:
                raise ValueError('unknown inlinedata filter %r not one of ascii85, base64, gzip or bzip2' % filter)
        print("<inlineData filters=%r>%s</inlineData>" % (' '.join(reversed(filters)),escape(data)))
Пример #7
0
    def testAsciiBase85(self):
        "Test if the obvious test for whether ASCII-Base85 encoding works."

        msg = "Round-trip AsciiBase85 encoding failed."
        plain = 'What is the average velocity of a sparrow?'

        #the remainder block can be absent or from 1 to 4 bytes
        for i in xrange(55):
            encoded = _AsciiBase85Encode(plain)
            decoded = _AsciiBase85Decode(encoded)
            assert decoded == plain, msg
            plain = plain + chr(i)
Пример #8
0
    def testAsciiBase85(self):
        "Test if the obvious test for whether ASCII-Base85 encoding works."

        msg = "Round-trip AsciiBase85 encoding failed."
        plain = 'What is the average velocity of a sparrow?'

        #the remainder block can be absent or from 1 to 4 bytes
        for i in xrange(55):
            encoded = _AsciiBase85Encode(plain)
            decoded = _AsciiBase85Decode(encoded)
            assert decoded == plain, msg
            plain = plain + chr(i)
Пример #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.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:
            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)
Пример #10
0
 def _jpg_imagedata(self,imageFile):
     self.source = 'JPEG'
     info = pdfutils.readJPEGInfo(imageFile)
     imgwidth, imgheight = info[0], info[1]
     if info[2] == 1:
         colorSpace = 'DeviceGray'
     elif info[2] == 3:
         colorSpace = 'DeviceRGB'
     else: #maybe should generate an error, is this right for CMYK?
         colorSpace = 'DeviceCMYK'
     imageFile.seek(0) #reset file pointer
     imagedata = []
     #imagedata.append('BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /DCTDecode] ID' % (info[0], info[1], colorSpace))
     imagedata.append('BI /W %d /H %d /BPC 8 /CS /%s /F [/A85 /DCT] ID' % (imgwidth, imgheight, colorSpace))
     #write in blocks of (??) 60 characters per line to a list
     compressed = imageFile.read()
     encoded = pdfutils._AsciiBase85Encode(compressed)
     pdfutils._chunker(encoded,imagedata)
     imagedata.append('EI')
     return (imagedata, imgwidth, imgheight)
Пример #11
0
 def _jpg_imagedata(self,imageFile):
     info = pdfutils.readJPEGInfo(imageFile)
     self.source = 'JPEG'
     imgwidth, imgheight = info[0], info[1]
     if info[2] == 1:
         colorSpace = 'DeviceGray'
     elif info[2] == 3:
         colorSpace = 'DeviceRGB'
     else: #maybe should generate an error, is this right for CMYK?
         colorSpace = 'DeviceCMYK'
     imageFile.seek(0) #reset file pointer
     imagedata = []
     #imagedata.append('BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /DCTDecode] ID' % (info[0], info[1], colorSpace))
     imagedata.append('BI /W %d /H %d /BPC 8 /CS /%s /F [/A85 /DCT] ID' % (imgwidth, imgheight, colorSpace))
     #write in blocks of (??) 60 characters per line to a list
     compressed = imageFile.read()
     encoded = pdfutils._AsciiBase85Encode(compressed)
     pdfutils._chunker(encoded,imagedata)
     imagedata.append('EI')
     return (imagedata, imgwidth, imgheight)
Пример #12
0
 def _jpg_imagedata(self, imageFile):
     info = pdfutils.readJPEGInfo(imageFile)
     self.source = "JPEG"
     imgwidth, imgheight = info[0], info[1]
     if info[2] == 1:
         colorSpace = "DeviceGray"
     elif info[2] == 3:
         colorSpace = "DeviceRGB"
     else:  # maybe should generate an error, is this right for CMYK?
         colorSpace = "DeviceCMYK"
     imageFile.seek(0)  # reset file pointer
     imagedata = []
     # imagedata.append('BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /DCTDecode] ID' % (info[0], info[1], colorSpace))
     imagedata.append(
         "BI /W %d /H %d /BPC 8 /CS /%s /F [%s/DCT] ID"
         % (imgwidth, imgheight, colorSpace, rl_config.useA85 and "/A85 " or "")
     )
     # write in blocks of (??) 60 characters per line to a list
     data = imageFile.read()
     if rl_config.useA85:
         data = pdfutils._AsciiBase85Encode(data)
     pdfutils._chunker(data, imagedata)
     imagedata.append("EI")
     return (imagedata, imgwidth, imgheight)
Пример #13
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)
Пример #14
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)