def __init__(self, filename, width=None, height=None, kind='direct', mask="auto", lazy=1): """If size to draw at not specified, get it from the image.""" self.hAlign = 'CENTER' self._mask = mask fp = hasattr(filename,'read') if fp: self._file = filename self.filename = repr(filename) else: self._file = self.filename = filename if not fp and os.path.splitext(filename)[1] in ['.jpg', '.JPG', '.jpeg', '.JPEG']: # if it is a JPEG, will be inlined within the file - # but we still need to know its size now from reportlab.lib.utils import open_for_read f = open_for_read(filename, 'b') try: try: info = pdfutils.readJPEGInfo(f) except: #couldn't read as a JPEG, try like normal self._setup(width,height,kind,lazy) return finally: f.close() self.imageWidth = info[0] self.imageHeight = info[1] self._img = None self._setup(width,height,kind,0) elif fp: self._setup(width,height,kind,0) else: self._setup(width,height,kind,lazy)
def __init__(self, fileName,ident=None): if isinstance(fileName,ImageReader): self.__dict__ = fileName.__dict__ #borgize return self._ident = ident #start wih lots of null private fields, to be populated by #the relevant engine. self.fileName = fileName self._image = None self._width = None self._height = None self._transparent = None self._data = None if _isPILImage(fileName): self._image = fileName self.fp = getattr(fileName,'fp',None) try: self.fileName = self._image.fileName except AttributeError: self.fileName = 'PILIMAGE_%d' % id(self) else: try: from reportlab.rl_config import imageReaderFlags self.fp = open_for_read(fileName,'b') if isinstance(self.fp,_StringIOKlass): imageReaderFlags=0 #avoid messing with already internal files if imageReaderFlags>0: #interning data = self.fp.read() if imageReaderFlags&2: #autoclose try: self.fp.close() except: pass if imageReaderFlags&4: #cache the data if not self._cache: from rl_config import register_reset register_reset(self._cache.clear) data=self._cache.setdefault(md5(data).digest(),data) self.fp=getStringIO(data) elif imageReaderFlags==-1 and isinstance(fileName,(str,unicode)): #try Ralf Schmitt's re-opening technique of avoiding too many open files self.fp.close() del self.fp #will become a property in the next statement self.__class__=LazyImageReader if haveImages: #detect which library we are using and open the image if not self._image: self._image = self._read_image(self.fp) if getattr(self._image,'format',None)=='JPEG': self.jpeg_fh = self._jpeg_fh else: from reportlab.pdfbase.pdfutils import readJPEGInfo try: self._width,self._height,c=readJPEGInfo(self.fp) except: annotateException('\nImaging Library not available, unable to import bitmaps only jpegs\nfileName=%r identity=%s'%(fileName,self.identity())) self.jpeg_fh = self._jpeg_fh self._data = self.fp.read() self._dataA=None self.fp.seek(0) except: annotateException('\nfileName=%r identity=%s'%(fileName,self.identity()))
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 __init__(self, filename, width=None, height=None, kind='direct', mask="auto", lazy=1): """If size to draw at not specified, get it from the image.""" self.hAlign = 'CENTER' self._mask = mask # if it is a JPEG, will be inlined within the file - # but we still need to know its size now fp = hasattr(filename,'read') if fp: self._file = filename self.filename = `filename` else: self._file = self.filename = filename if not fp and os.path.splitext(filename)[1] in ['.jpg', '.JPG', '.jpeg', '.JPEG']: from reportlab.lib.utils import open_for_read f = open_for_read(filename, 'b') try: info = pdfutils.readJPEGInfo(f) except: #this is where we normally find a JPEG is corrupt. #if so, include filename to help people track it down. raise ValueError("Corrupt JPEG '%s', pdfutils.readJPEGInfo failed." % self.filename) f.close() self.imageWidth = info[0] self.imageHeight = info[1] self._img = None self._setup(width,height,kind,0) elif fp: self._setup(width,height,kind,0) else: self._setup(width,height,kind,lazy)
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 [%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)
def __init__(self, fileName): if isinstance(fileName, PmlImageReader): self.__dict__ = fileName.__dict__ # borgize return #start wih lots of null private fields, to be populated by #the relevant engine. self.fileName = fileName self._image = None self._width = None self._height = None self._transparent = None self._data = None imageReaderFlags = 0 if PILImage and isinstance(fileName, PILImage.Image): self._image = fileName self.fp = getattr(fileName, 'fp', None) try: self.fileName = self._image.fileName except AttributeError: self.fileName = 'PILIMAGE_%d' % id(self) else: try: self.fp = open_for_read(fileName, 'b') if isinstance(self.fp, StringIO.StringIO().__class__): imageReaderFlags = 0 # avoid messing with already internal files if imageReaderFlags > 0: # interning data = self.fp.read() if imageReaderFlags & 2: # autoclose try: self.fp.close() except: pass if imageReaderFlags & 4: # cache the data if not self._cache: from rl_config import register_reset register_reset(self._cache.clear) data = self._cache.setdefault(md5(data).digest(), data) self.fp = getStringIO(data) elif imageReaderFlags == - 1 and isinstance(fileName, (str, unicode)): #try Ralf Schmitt's re-opening technique of avoiding too many open files self.fp.close() del self.fp # will become a property in the next statement self.__class__ = LazyImageReader if haveImages: #detect which library we are using and open the image if not self._image: self._image = self._read_image(self.fp) if getattr(self._image, 'format', None) == 'JPEG': self.jpeg_fh = self._jpeg_fh else: from reportlab.pdfbase.pdfutils import readJPEGInfo try: self._width, self._height, c = readJPEGInfo(self.fp) except: raise RuntimeError('Imaging Library not available, unable to import bitmaps only jpegs') self.jpeg_fh = self._jpeg_fh self._data = self.fp.read() self._dataA = None self.fp.seek(0) except: # TODO: Kill the catch-all et, ev, tb = sys.exc_info() if hasattr(ev, 'args'): a = str(ev.args[- 1]) + (' fileName=%r' % fileName) ev.args = ev.args[: - 1] + (a,) raise et, ev, tb else: raise
def __init__(self, fileName): if isinstance(fileName, PmlImageReader): self.__dict__ = fileName.__dict__ # borgize return # start wih lots of null private fields, to be populated by the relevant engine self.fileName = fileName self._image = None self._width = None self._height = None self._transparent = None self._data = None imageReaderFlags = 0 if PILImage and isinstance(fileName, PILImage.Image): self._image = fileName self.fp = getattr(fileName, 'fp', None) try: self.fileName = self._image.fileName except AttributeError: self.fileName = 'PILIMAGE_%d' % id(self) else: try: self.fp = open_for_read(fileName, 'b') if isinstance(self.fp, six.BytesIO().__class__): # avoid messing with already internal files imageReaderFlags = 0 if imageReaderFlags > 0: # interning data = self.fp.read() if imageReaderFlags & 2: # autoclose try: self.fp.close() except: pass if imageReaderFlags & 4: # cache the data if not self._cache: from rl_config import register_reset register_reset(self._cache.clear) data = self._cache.setdefault(md5(data).digest(), data) self.fp = getStringIO(data) elif imageReaderFlags == - 1 and isinstance(fileName, six.text_type): # try Ralf Schmitt's re-opening technique of avoiding too many open files self.fp.close() del self.fp # will become a property in the next statement self.__class__ = LazyImageReader if haveImages: # detect which library we are using and open the image if not self._image: self._image = self._read_image(self.fp) if getattr(self._image, 'format', None) == 'JPEG': self.jpeg_fh = self._jpeg_fh else: from reportlab.pdfbase.pdfutils import readJPEGInfo try: self._width, self._height, c = readJPEGInfo(self.fp) except: raise RuntimeError('Imaging Library not available, unable to import bitmaps only jpegs') self.jpeg_fh = self._jpeg_fh self._data = self.fp.read() self._dataA = None self.fp.seek(0) except: # TODO: Kill the catch-all et, ev, tb = sys.exc_info() if hasattr(ev, 'args'): a = str(ev.args[- 1]) + (' fileName=%r' % fileName) ev.args = ev.args[: - 1] + (a,) raise RuntimeError("{0} {1} {2}".format(et, ev, tb)) else: raise
def __init__(self, fileName, ident=None): if isinstance(fileName, ImageReader): self.__dict__ = fileName.__dict__ #borgize return self._ident = ident #start wih lots of null private fields, to be populated by #the relevant engine. self.fileName = fileName self._image = None self._width = None self._height = None self._transparent = None self._data = None if _isPILImage(fileName): self._image = fileName self.fp = getattr(fileName, 'fp', None) try: self.fileName = self._image.fileName except AttributeError: self.fileName = 'PILIMAGE_%d' % id(self) else: try: from reportlab.rl_config import imageReaderFlags self.fp = open_for_read(fileName, 'b') if isinstance(self.fp, _bytesIOType): imageReaderFlags = 0 #avoid messing with already internal files if imageReaderFlags > 0: #interning data = self.fp.read() if imageReaderFlags & 2: #autoclose try: self.fp.close() except: pass if imageReaderFlags & 4: #cache the data if not self._cache: from rl_config import register_reset register_reset(self._cache.clear) data = self._cache.setdefault(_digester(data), data) self.fp = getBytesIO(data) elif imageReaderFlags == -1 and isinstance(fileName, str): #try Ralf Schmitt's re-opening technique of avoiding too many open files self.fp.close() del self.fp #will become a property in the next statement self.__class__ = LazyImageReader if haveImages: #detect which library we are using and open the image if not self._image: self._image = self._read_image(self.fp) if getattr(self._image, 'format', None) == 'JPEG': self.jpeg_fh = self._jpeg_fh else: from reportlab.pdfbase.pdfutils import readJPEGInfo try: self._width, self._height, c = readJPEGInfo(self.fp) except: annotateException( '\nImaging Library not available, unable to import bitmaps only jpegs\nfileName=%r identity=%s' % (fileName, self.identity())) self.jpeg_fh = self._jpeg_fh self._data = self.fp.read() self._dataA = None self.fp.seek(0) except: annotateException('\nfileName=%r identity=%s' % (fileName, self.identity()))