reportlab/pdfgen/pdfimages.py
author rgbecker
Wed, 25 Oct 2000 08:57:46 +0000
changeset 494 54257447cfe9
parent 491 6bf7da76b8fd
child 554 ed6c96d22d1e
permissions -rw-r--r--
Changed to indirect copyright
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 491
diff changeset
     1
#copyright ReportLab Inc. 2000
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 491
diff changeset
     2
#see license.txt for license details
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 491
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/pdfgen/pdfimages.py?cvsroot=reportlab
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 491
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/pdfgen/pdfimages.py,v 1.3 2000/10/25 08:57:45 rgbecker Exp $
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 491
diff changeset
     5
__version__=''' $Id: pdfimages.py,v 1.3 2000/10/25 08:57:45 rgbecker Exp $ '''
490
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
     6
__doc__="""
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
     7
Image functionality sliced out of canvas.py for generalization
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
     8
"""
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
     9
import os
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    10
import string
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    11
import cStringIO
491
6bf7da76b8fd Fix zlib import bug
rgbecker
parents: 490
diff changeset
    12
from types import StringType
6bf7da76b8fd Fix zlib import bug
rgbecker
parents: 490
diff changeset
    13
from reportlab.pdfbase import pdfutils
6bf7da76b8fd Fix zlib import bug
rgbecker
parents: 490
diff changeset
    14
from reportlab.lib.utils import fp_str
490
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    15
491
6bf7da76b8fd Fix zlib import bug
rgbecker
parents: 490
diff changeset
    16
try:
6bf7da76b8fd Fix zlib import bug
rgbecker
parents: 490
diff changeset
    17
	import zlib
6bf7da76b8fd Fix zlib import bug
rgbecker
parents: 490
diff changeset
    18
except ImportError:
6bf7da76b8fd Fix zlib import bug
rgbecker
parents: 490
diff changeset
    19
	zlib = None
490
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    20
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    21
class PDFImage:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    22
    def __init__(self, image, x,y, width=None, height=None):
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    23
        self.image = image
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    24
        self.point = (x,y)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    25
        self.dimensions = (width, height)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    26
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    27
    def jpg_imagedata(self):
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    28
        #directly process JPEG files
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    29
        #open file, needs some error handling!!
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    30
        imageFile = open(self.image, 'rb')
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    31
        info = pdfutils.readJPEGInfo(imageFile)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    32
        imgwidth, imgheight = info[0], info[1]
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    33
        if info[2] == 1:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    34
            colorSpace = 'DeviceGray'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    35
        elif info[2] == 3:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    36
            colorSpace = 'DeviceRGB'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    37
        else: #maybe should generate an error, is this right for CMYK?
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    38
            colorSpace = 'DeviceCMYK'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    39
        imageFile.seek(0) #reset file pointer
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    40
        imagedata = []
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    41
        #imagedata.append('BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /DCTDecode] ID' % (info[0], info[1], colorSpace))
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    42
        imagedata.append('BI /W %d /H %d /BPC 8 /CS /%s /F [/A85 /DCT] ID' % (imgwidth, imgheight, colorSpace))
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    43
        #write in blocks of (??) 60 characters per line to a list
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    44
        compressed = imageFile.read()
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    45
        encoded = pdfutils._AsciiBase85Encode(compressed)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    46
        outstream = cStringIO.StringIO(encoded)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    47
        dataline = outstream.read(60)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    48
        while dataline <> "":
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    49
            imagedata.append(dataline)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    50
            dataline = outstream.read(60)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    51
        imagedata.append('EI')
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    52
        return (imagedata, imgwidth, imgheight)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    53
    
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    54
    def cache_imagedata(self):
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    55
        image = self.image
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    56
        if not pdfutils.cachedImageExists(image):
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    57
            if not zlib:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    58
                print 'zlib not available'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    59
                return
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    60
            try:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    61
                import Image
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    62
            except ImportError:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    63
                print 'Python Imaging Library not available'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    64
                return
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    65
            pdfutils.cacheImageFile(image)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    66
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    67
        #now we have one cached, slurp it in
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    68
        cachedname = os.path.splitext(image)[0] + '.a85'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    69
        imagedata = open(cachedname,'rb').readlines()
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    70
        #trim off newlines...
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    71
        imagedata = map(string.strip, imagedata)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    72
        return imagedata
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    73
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    74
    def PIL_imagedata(self):
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    75
        image = self.image
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    76
        if not zlib:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    77
            print 'zlib not available'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    78
            return
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    79
        myimage = image.convert('RGB')
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    80
        imgwidth, imgheight = myimage.size
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    81
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    82
        # this describes what is in the image itself
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    83
        # *NB* according to the spec you can only use the short form in inline images
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    84
        #imagedata.append('BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /FlateDecode] ID' % (imgwidth, imgheight,'RGB'))
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    85
        imagedata.append('BI /W %d /H %d /BPC 8 /CS /RGB /F [/A85 /Fl] ID' % (imgwidth, imgheight))
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    86
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    87
        #use a flate filter and Ascii Base 85 to compress
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    88
        raw = myimage.tostring()
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    89
        assert(len(raw) == imgwidth * imgheight, "Wrong amount of data for image")
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    90
        compressed = zlib.compress(raw)   #this bit is very fast...
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    91
        encoded = pdfutils._AsciiBase85Encode(compressed) #...sadly this isn't
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    92
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    93
        #write in blocks of (??) 60 characters per line to a list
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    94
        outstream = cStringIO.StringIO(encoded)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    95
        dataline = outstream.read(60)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    96
        while dataline <> "":
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    97
            imagedata.append(dataline)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    98
            dataline = outstream.read(60)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    99
        imagedata.append('EI')
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   100
        return (imagedata, imgwidth, imgheight) 
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   101
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   102
    def drawInlineImage(self, canvas): #, image, x,y, width=None,height=None):
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   103
        """Draw an Image into the specified rectangle.  If width and
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   104
        height are omitted, they are calculated from the image size.
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   105
        Also allow file names as well as images.  This allows a
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   106
        caching mechanism"""
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   107
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   108
        image = self.image 
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   109
        (x,y) = self.point
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   110
        (width, height) = self.dimensions
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   111
        
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   112
        if type(image) == StringType:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   113
            if os.path.splitext(image)[1] in ['.jpg', '.JPG', '.jpeg', '.JPEG']:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   114
                (imagedata, imgwidth, imgheight) = self.jpg_imagedata()
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   115
            else:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   116
                if hasattr(self,'noImageCaching') and canvas.noImageCaching:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   117
                    imagedata = pdfutils.cacheImageFile(image,returnInMemory=1)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   118
                else:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   119
                    imagedata = self.cache_imagedata()
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   120
                #parse line two for width, height
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   121
                words = string.split(imagedata[1])
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   122
                imgwidth = string.atoi(words[1])
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   123
                imgheight = string.atoi(words[3])
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   124
        else:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   125
            (imagedata, imgwidth, imgheight) = self.PIL_imagedata()
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   126
        #now build the PDF for the image.
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   127
        if not width:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   128
            width = imgwidth
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   129
        if not height:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   130
            height = imgheight
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   131
        
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   132
        # this says where and how big to draw it
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   133
        if not canvas.bottomup: y = y+height
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   134
        canvas._code.append('q %s 0 0 %s cm' % (fp_str(width), fp_str(height, x, y)))
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   135
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   136
        # self._code.extend(imagedata) if >=python-1.5.2
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   137
        for line in imagedata:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   138
            canvas._code.append(line)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   139
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   140
        canvas._code.append('Q')