Insert image into Reportlab either from PIL image or StringIO

I had no luck with the proposed methods.

Checking the code in pdfdoc.py shows, that the AttributError results from treating the StringIO as a filename:

    if source is None:
        pass # use the canned one.
    elif hasattr(source,'jpeg_fh'):
        self.loadImageFromSRC(source)   #it is already a PIL Image
    else:
        # it is a filename

Further checking the source, shows that jpeg_fh is an attribute of class ImageReader in reportlab.lib.utils. ImageReader accepts both StringIO and PIL images.

So wrapping the StringIO in an ImageReader solved the problem for me:

import PIL
from reportlab.lib.utils import ImageReader

io_img = StringIO(data)
pil_img = PIL.Image.open(StringIO(data))

reportlab_io_img = ImageReader(io_img)
reportlab_pil_img = ImageReader(pil_img)

canvas.drawImage(reportlab_io_img, ...)
canvas.drawImage(reportlab_pil_img, ...)

The repetitive declaration "Formats supported by PIL/Java 1.4 (the Python/Java Imaging Library) are supported" simply means that data formats supported by PIL are supported by reportlab (since it uses PIL to read them).

Now, from peeking in reportlab.platypus.flowables.Image code it is possible to see that it accepts either a filename or a file object as input. The former is not what you want, so let us focus on the later. You said StringIO didn't seem to work, but it does if you take some care. You probably did something wrong with it, here are two correct ways to use StringIO:

import sys
import PIL
from cStringIO import StringIO
from reportlab.platypus.flowables import Image

# Method 1
data = open(sys.argv[1]).read()
img1 = StringIO(data)

# Method 2
img2 = StringIO()
PIL.Image.open(sys.argv[2]).save(img2, 'PNG')
img2.seek(0)

# Method 3 (fails)
img3 = StringIO(PIL.Image.open(sys.argv[2]).tostring())

story = [Image(img1), Image(img2)]
#Image(img3)

The method 3 fails because img3 now holds the raw data of the image, so it has no idea about the actual format of this data. There is no reason to attempt to use this method for such task.

If you have raw data and you know the image mode of your data ('L', 'RGB', etc) and also its width, height, then you can use a fourth (correct) method based on PIL.Image.fromstring(...).save(mystrio, 'someformat').


I found two different patterns useful depending on whether I'm drawing on the canvas or building flowable elements for multiBuild. I am using matplotlib figures saved to a BytesIO buffer, but I assume the same buffer will serve your barcode needs.

For both:

import io

For the canvas:

from reportlab.lib.units import inch
from reportlab.lib.utils import ImageReader

# assume a proper Canvas object is instantiated as c

buf = io.BytesIO(the_barcode.getvalue())
buf.seek(0)
c.drawImage(ImageReader(buf), 0.5*inch, 5.5*inch, )

For a flowable element for a document:

from reportlab.platypus.flowables import Image

# assume story is a list of flowable elements for multiBuild

buf = io.BytesIO(the_barcode.getvalue())
buf.seek(0)
story.append(Image(buf))

After much trial and error, these worked for me.