The Problem

Using repotlab and following the instructions from py2exeAndPIL.

Still, when

   1 from reportlab.lib.utils import ImageReader
   2 im=ImageReader(someCstringIOStream())

raises "cannot identify image file"

Explanation

Reportlab does some different things when importing Image from PIL

excerpt from reportlab.lib.utils

   1 class ImageReader:
   2     "Wraps up either PIL or Java to get data from bitmaps"
   3     def __init__(self, fileName):
   4         if not haveImages:
   5             warnOnce('Imaging Library not available, unable to import bitmaps')
   6             return
   7         #start wih lots of null private fields, to be populated by
   8         #the relevant engine.
   9         self.fileName = fileName
  10         self._image = None
  11         self._width = None
  12         self._height = None
  13         self._transparent = None
  14         self._data = None
  15 
  16         #detect which library we are using and open the image
  17         if sys.platform[0:4] == 'java':
  18             from javax.imageio import ImageIO
  19             if type(fileName) is type(''):
  20                 fp = open(fileName,'rb')
  21             else:
  22                 fp = fileName
  23             self._image = ImageIO.read(fp)
  24         else:
  25             import PIL.Image
  26             self._image = PIL.Image.open(fileName)

Solution

Make it dumb. We know: This is not Java, this is Windows. We have PIL, because we use py2exe and ARE responsible to put it into the package. So...

   1 class MyImageReader(ImageReader):
   2     """ Kopie des Report-Lab Image Readers, kommt besser mit
   3         py2exeter PIL zurande"""
   4     def __init__(self, fileName):
   5         #start wih lots of null private fields, to be populated by
   6         #the relevant engine.
   7         self.fileName = fileName
   8         self._image = None
   9         self._width = None
  10         self._height = None
  11         self._transparent = None
  12         self._data = None
  13 
  14         #änderung: kein Detect oder so, DIES IST WINDOWS
  15         # PIL IST VORHANDEN
  16             
  17         self._image = Image.open(fileName)

Image has to be imported and "tuned" as described in py2exeAndPIL

HAM2004-08-09

PILAndReportlab (last edited 2015-05-23 05:37:03 by JimmyRetzlaff)