[reportlab-users] PIL-disabled patch

Dariusz Rybi jiivan at o2.pl
Sat Dec 4 02:52:12 EST 2004


Hi,
This is small but usefull patch that put a black box instead of
raising an exception when you try to use images without PIL
installed.

Regards.

--- /usr/local/lib/reportlab-1_19/reportlab/lib/utils.py	2004-01-21 09:33:04.000000000 +0100
+++ utils.py.new	2004-12-03 12:04:02.000000000 +0100
@@ -291,16 +291,33 @@
     except:
         return open(name,'r'+mode)
 
+class FakeImage:
+    """Imitates PIL Image object so that you won't get errors when trying
+    to produce PDF without the PIL library.
+    """
+    def __init__(self):
+        self.size = (1,1)
+        self.info = {}
+
+    def convert(self, *args):
+        if args[0] == 'RGB':
+            class FakeRGB:
+                def tostring(self):
+                    # This returns a black pixel
+                    return '\x00\x00\x00'
+            return FakeRGB()
+
 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
+            self._image = FakeImage()
+            ##return
         #start wih lots of null private fields, to be populated by
         #the relevant engine.
         self.fileName = fileName
-        self._image = None
+        ##self._image = None
         self._width = None
         self._height = None
         self._transparent = None
@@ -314,7 +331,8 @@
             else:
                 fp = fileName
             self._image = ImageIO.read(fp)
-        else:
+        ##else:
+        elif haveImages:
             import PIL.Image
             self._image = PIL.Image.open(fileName)
 




More information about the reportlab-users mailing list