Beispiel #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)
Beispiel #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
        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)
Beispiel #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

        # 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):
        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)
Beispiel #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)
Beispiel #6
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)
 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)
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
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)