5 __doc__=""" |
5 __doc__=""" |
6 Image functionality sliced out of canvas.py for generalization |
6 Image functionality sliced out of canvas.py for generalization |
7 """ |
7 """ |
8 |
8 |
9 import os |
9 import os |
10 import string |
|
11 from types import StringType |
|
12 import reportlab |
10 import reportlab |
13 from reportlab import rl_config |
11 from reportlab import rl_config |
14 from reportlab.pdfbase import pdfutils |
12 from reportlab.pdfbase import pdfutils |
15 from reportlab.pdfbase import pdfdoc |
13 from reportlab.pdfbase import pdfdoc |
16 from reportlab.lib.utils import fp_str, getStringIO |
14 from reportlab.lib.utils import fp_str, getBytesIO, isStrType |
17 from reportlab.lib.utils import import_zlib, haveImages |
15 from reportlab.lib.utils import import_zlib, haveImages |
18 from reportlab.lib.boxstuff import aspectRatioFix |
16 from reportlab.lib.boxstuff import aspectRatioFix |
19 |
17 |
20 |
18 |
21 class PDFImage: |
19 class PDFImage: |
83 |
81 |
84 #now we have one cached, slurp it in |
82 #now we have one cached, slurp it in |
85 cachedname = os.path.splitext(image)[0] + (rl_config.useA85 and '.a85' or '.bin') |
83 cachedname = os.path.splitext(image)[0] + (rl_config.useA85 and '.a85' or '.bin') |
86 imagedata = open(cachedname,'rb').readlines() |
84 imagedata = open(cachedname,'rb').readlines() |
87 #trim off newlines... |
85 #trim off newlines... |
88 imagedata = list(map(string.strip, imagedata)) |
86 imagedata = list(map(str.strip, imagedata)) |
89 return imagedata |
87 return imagedata |
90 |
88 |
91 def PIL_imagedata(self): |
89 def PIL_imagedata(self): |
92 image = self.image |
90 image = self.image |
93 if image.format=='JPEG': |
91 if image.format=='JPEG': |
127 def non_jpg_imagedata(self,image): |
125 def non_jpg_imagedata(self,image): |
128 if not self.imageCaching: |
126 if not self.imageCaching: |
129 imagedata = pdfutils.cacheImageFile(image,returnInMemory=1) |
127 imagedata = pdfutils.cacheImageFile(image,returnInMemory=1) |
130 else: |
128 else: |
131 imagedata = self.cache_imagedata() |
129 imagedata = self.cache_imagedata() |
132 words = string.split(imagedata[1]) |
130 words = imagedata[1].split() |
133 imgwidth = string.atoi(words[1]) |
131 imgwidth = int(words[1]) |
134 imgheight = string.atoi(words[3]) |
132 imgheight = int(words[3]) |
135 return imagedata, imgwidth, imgheight |
133 return imagedata, imgwidth, imgheight |
136 |
134 |
137 def getImageData(self,preserveAspectRatio=False): |
135 def getImageData(self,preserveAspectRatio=False): |
138 "Gets data, height, width - whatever type of image" |
136 "Gets data, height, width - whatever type of image" |
139 image = self.image |
137 image = self.image |
140 |
138 |
141 if type(image) == StringType: |
139 if isStrType(image): |
142 self.filename = image |
140 self.filename = image |
143 if os.path.splitext(image)[1] in ['.jpg', '.JPG', '.jpeg', '.JPEG']: |
141 if os.path.splitext(image)[1] in ['.jpg', '.JPG', '.jpeg', '.JPEG']: |
144 try: |
142 try: |
145 imagedata, imgwidth, imgheight = self.jpg_imagedata() |
143 imagedata, imgwidth, imgheight = self.jpg_imagedata() |
146 except: |
144 except: |
187 dict['Subtype'] = '/Image' |
185 dict['Subtype'] = '/Image' |
188 dict['Width'] = self.width |
186 dict['Width'] = self.width |
189 dict['Height'] = self.height |
187 dict['Height'] = self.height |
190 dict['BitsPerComponent'] = 8 |
188 dict['BitsPerComponent'] = 8 |
191 dict['ColorSpace'] = pdfdoc.PDFName(self.colorSpace) |
189 dict['ColorSpace'] = pdfdoc.PDFName(self.colorSpace) |
192 content = string.join(self.imageData[3:-1], '\n') + '\n' |
190 content = '\n'.join(self.imageData[3:-1]) + '\n' |
193 strm = pdfdoc.PDFStream(dictionary=dict, content=content) |
191 strm = pdfdoc.PDFStream(dictionary=dict, content=content) |
194 return strm.format(document) |
192 return strm.format(document) |
195 |
193 |
196 if __name__=='__main__': |
194 if __name__=='__main__': |
197 srcfile = os.path.join( |
195 srcfile = os.path.join( |