[reportlab-users] problem with flowables.Image

Robin Becker reportlab-users@reportlab.com
Fri, 28 May 2004 15:06:08 +0100

This is a multi-part message in MIME format.
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Jerome Alet wrote:

> Hi there,
> please could someone tell me what are the accepted "filename"
> values types for flowables.Image()'s __init__ method ?
> we tried with urls (drawImage used to support urls) and it doesn't
> work
> we tried with PIL.Image objects and it fails
> we tried with StringIO objects and it fails
> are the only supported types :
>         - real filename 
>     and     
>         - real file object
> ?        
> or do we do something completely wrong ?        
> FYI PIL 1.1.4 and RL 1.19
> thanks in advance
> Jerome Alet

Jerome I bust this a while back and it just hasn't been fixed. We are now 
attempting to use a java friendly interface so images have to be opened by an 
ImageReader. Unfortunately that means we can no longer obviously accept PIL 
objects as inputs to the ImageReader and hence Image (as we might be working in 

However, I have just fixed the reportlab.lib.utils.open_for_read function and it 
now appears to accept

1) anything with a read method
2) URL's eg 'file://C:/tmp/myimage.gif'
3) ordinary filenames

I will do some testing and put this in CVS later today. Of course fixing the 
read function doesn't necessarily imply that Image will immediately work again, 
but I'll try and fix that later.
Robin Becker

Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;

"""Tests for reportlab.lib.utils
import os
import reportlab
from reportlab.test import unittest
from reportlab.test.utils import makeSuiteForClasses
from reportlab.lib import colors
from reportlab.lib.utils import recursiveImport, recursiveGetAttr, recursiveSetAttr, rl_isfile, \

class ImporterTestCase(unittest.TestCase):
    "Test import utilities"

    def setUp(self):
        from time import time
        from reportlab.lib.utils import get_rl_tempdir
        self._tempdir = get_rl_tempdir('reportlab_test','tmp_%d' % time())
        _testmodulename = os.path.join(self._tempdir,'test_module_%d.py' % time())
        f = open(_testmodulename,'w')
        self._testmodulename = os.path.splitext(os.path.basename(_testmodulename))[0]

    def tearDown(self):
        from shutil import rmtree

    def test1(self):
        "try stuff known to be in the path"
        m1 = recursiveImport('reportlab.pdfgen.canvas')
        import reportlab.pdfgen.canvas
        assert m1 == reportlab.pdfgen.canvas

    def test2(self):
        "try under a well known directory NOT on the path"
        D = os.path.join(os.path.dirname(reportlab.__file__), 'tools','pythonpoint')
        fn = os.path.join(D,'stdparser.py')
        if rl_isfile(fn) or rl_isfile(fn+'c') or rl_isfile(fn+'o'):
            m1 = recursiveImport('stdparser', baseDir=D)

    def test3(self):
        "ensure CWD is on the path"
            cwd = os.getcwd()
            m1 = recursiveImport(self._testmodulename)

    def test4(self):
        "ensure noCWD removes current dir from path"
            cwd = os.getcwd()
            import sys
                del sys.modules[self._testmodulename]
            except KeyError:

    def test5(self):
        "recursive attribute setting/getting on modules"
        import reportlab.lib.units
        inch = recursiveGetAttr(reportlab, 'lib.units.inch')
        assert inch == 72

        recursiveSetAttr(reportlab, 'lib.units.cubit', 18*inch)
        cubit = recursiveGetAttr(reportlab, 'lib.units.cubit')
        assert cubit == 18*inch

    def test6(self):
        "recursive attribute setting/getting on drawings"
        from reportlab.graphics.charts.barcharts import sampleH1
        drawing = sampleH1()
        recursiveSetAttr(drawing, 'barchart.valueAxis.valueMax', 72)
        theMax = recursiveGetAttr(drawing, 'barchart.valueAxis.valueMax')
        assert theMax == 72

    def test7(self):
        "test open and read of a simple relative file"
        from reportlab.lib.utils import open_and_read
        b = open_and_read('../docs/images/Edit_Prefs.gif')

    def test8(self):
        "test open and read of a relative file: URL"
        from reportlab.lib.utils import open_and_read
        b = open_and_read('file:../docs/images/Edit_Prefs.gif')

    def test9(self):
        "test open and read of an http: URL"
        from reportlab.lib.utils import open_and_read
        b = open_and_read('http://www.reportlab.com/rsrc/encryption.gif')

    def test10(self):
        "test open and read of a simple relative file"
        from reportlab.lib.utils import open_and_read, getStringIO
        b = getStringIO(open_and_read('../docs/images/Edit_Prefs.gif'))
        b = open_and_read(b)

def makeSuite():
    return makeSuiteForClasses(ImporterTestCase)

if __name__ == "__main__": #noruntests