[reportlab-users] Patch: PIL images with ReportLab

Sam Hunter shunter at dbsupply.com
Wed Apr 27 15:28:53 EDT 2005


 I've been fighting with using PIL images and ReportLab on and off for a 
few days now, and finally took at look at the utils.py code to see how 
ImageReader works.  Basically it looks like it accepts a file name or 
file like object and then loads it as a PIL image if the platform is NOT 
java, and uses some java image type if it is.
 It seemed somewhat circuitous to take a PIL image, put it in a StringIO 
wrapper, create an ImageReader object with it, to have the 
ImageReader.__init__  convert it back to a PIL image.   Why not just 
make the __init__ accept PIL images in the first place?
 So, I made a few small modifications.  They work perfectly in my small 
test script.  If they look good, could it go into the source?
 Also, it looks like the canvas.drawImage() could very easily be 
modified to take PILs as well.

Thanks,

Sam Hunter

---------A small test script---------------
   c1 = canvas.Canvas("test.pdf", pagesize=letter, pageCompression=0) 
#create a canvas of letter size
   Im = Image.open("example_img.jpg")
   ir = ImageReader(Im)
   c1.drawImage(ir, 6,5, xsize, ysize)
------------------------------------------


The Original utils.py:
--------line 504-----------------
class ImageReader:
   "Wraps up either PIL or Java to get data from bitmaps"
   def __init__(self, fileName):
       if not haveImages:
           warnOnce('Imaging Library not available, unable to import 
bitmaps')
           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
       self.fp = open_for_read(fileName,'b')

       #detect which library we are using and open the image
       if sys.platform[0:4] == 'java':
           from javax.imageio import ImageIO
           self._image = ImageIO.read(self.fp)
       else:
           import PIL.Image
           self._image = PIL.Image.open(self.fp)
--------line 528---------------

Changed version:
------------line 504------------
class ImageReader:
   "Wraps up either PIL or Java to get data from bitmaps"
   def __init__(self, fileName):
       if not haveImages:
           warnOnce('Imaging Library not available, unable to import 
bitmaps')
           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
             if(hasattr(fileName, "__class__")):
           type = fileName.__class__.__name__
           if(type != "Image"):
               self.fp = open_for_read(fileName,'b')
       else:
           type = "other"
           self.fp = open_for_read(fileName,'b')

       #detect which library we are using and open the image
       if sys.platform[0:4] == 'java':
           from javax.imageio import ImageIO
           self._image = ImageIO.read(self.fp)
       else:
           import PIL.Image
           if(type == "Image"):
               self._image = fileName
           else:
               self._image = PIL.Image.open(self.fp)
--------------Line 538-------------------

So yeah, it is a few lines longer..  but so much less of a hassle.



More information about the reportlab-users mailing list