[reportlab-users] PNG problem

Robin Becker robin at reportlab.com
Wed Mar 23 11:51:38 EST 2005

Lionel Roubeyrie wrote:
> Hi all,
> I try to put an image in a pdf document, but for the first try with a eps 
> image, the result seem bad, the characters are badly retranscribed (fatty, 
> fuzzy), and if I try with a png image (previously opened with PIL), I get a :
> usr/lib/python2.3/site-packages/reportlab/pdfgen/canvas.py in drawImage(self, 
> image, x, y, width, height, mask)
>     584             name = _digester('%s%s' % (image, mask))
>     585         else:
> --> 586             rawdata = image.getRGBData()
>     587             name = _digester(rawdata)
>     588
> AttributeError: Image instance has no attribute 'getRGBData'
> Here the source code (running on a Debian Sarge, reportlab 1.20 and PIL1.1.4):
> import Image
> from reportlab.pdfgen import canvas
> from reportlab.lib import pagesizes, colors
> # from reportlab.lib.colors import green, orange, red, black
> img=Image.open('fond_coul.png')
> x, y = img.size
> img=img.resize(size=(x/4, y/4), resample=1)
> img=img.convert(mode='RGB')
> c=canvas.Canvas("test.pdf", pagesize=pagesizes.A4)
> c.drawImage(img,0,0)
> c.showPage()
> c.save()
> thanks for your help
you cannot pass PIL images into drawImage. You can pass an ImageReader instance 
or a filename, but PIL Image instance is not allowed. In your case where you 
wish to preprocess the image you should either save to a file or create an 
internal file like object to save to. That should be rewound and passed to an 
ImageReader for use with the canvas. If you want to be really clever you could 
try subclassin ImageReader (defined in utils.py) and then get it to do your PIL 
Robin Becker

More information about the reportlab-users mailing list