[reportlab-users] Image opacity

Robin Becker robin at reportlab.com
Tue Aug 31 08:23:10 EDT 2010


On 29/08/2010 20:36, Hugh Macdonald wrote:

> Hi,

>

> When using drawImage with just an image filename, is there any way to set the opacity of the image? I want to be able to put an image over other elements in the document.

>

> I've not investigated putting the image in using PIL yet, so that may be the option... But if I can, I'd love to just have an opacity value that I can pass in at some point...

>

> Cheers

...........

I'm fairly sure that is not possible with the canvas.drawImage method. The mask
argument may be used to specify which image pixels will be treated as
transparent, but that's clearly not what you want here. With the ImageReader
class you can have full rgb alpha images and the mask may be created properly
using 'auto'.


If your image doesn't support alpha itself (I think it's only PNG at present)
then you can fake the transparency mask by doing the following (untested so be
careful).

1) get the ordinary 3 layer image using
from reportlab.lib.utils import ImageReader
im=ImageReader('my.gif')

2) Use pil to create a mask (L) Image of the required size

3) set the bytes of the mask to the required transparency level ie 50% =127
using some PIL manipulations

4) set im._dataA = mask

5) pass im into canvas.drawImage with a mask of 'auto'.

Hopefully the code in drawImage should extract the mask and use it reasonably.

I suspect this will not work with jpegs as they follow a different path through
the RL code (because jpeg is natively supported in PDF).
--
Robin Becker


More information about the reportlab-users mailing list