[Image-SIG] How to overlay a mostly transparent image over another image?

Matthew Wilson matt at tplus1.com
Fri Feb 16 20:29:03 CET 2007

I have a bunch of 50x50 PNG files that show a little product.  I have
some other 50x50 PNG files that are transparent except for the bottom
right corner, which has some text like "new!" or "fun!".

I want to create a new file that contains the original product
thumbnail, plus the mostly transparent image added over.

I figure this is a very straightforward operation, but I'm not getting
anywhere yet.

This is what I've tried so far:

    In [93]: a = Image.open('a.png')

    In [94]: a.info
    Out[94]: {}

    In [95]: a.size
    Out[95]: (50, 50)

    In [96]: a.mode
    Out[96]: 'RGB'

    In [97]: ff = Image.open('freeframe.png')

    In [98]: ff.info
    {'Creation Time': '02/16/07',
     'Software': 'Macromedia Fireworks 8',
     'dpi': (71, 71),
     'transparency': 0}

    In [99]: ff.mode
    Out[99]: 'P'

    In [100]: ff.size
    Out[100]: (50, 50)

    In [101]: ImageChops.add(a, ff)
    <type 'exceptions.ValueError'>            Traceback (most recent call last)

    C:\Documents and Settings\mwilson\My Documents\pil-fun\<ipython console> in <module>()

    C:\Python25\lib\site-packages\PIL\ImageChops.py in add(image1, image2, scale, offset)
        175     image1.load()
        176     image2.load()
    --> 177     return image1._new(image1.im.chop_add(image2.im, scale, offset))
        179 ##

    <type 'exceptions.ValueError'>: images do not match

    In [102]: a2 = a.convert(mode='P')

    In [103]: ImageChops.add(a2, ff)
    Out[103]: <Image.Image instance at 0x00EE3F30>

    In [104]: _103.show()

The image that I see is a grayscale image of just the a.png file.  I
can't see the first image.

I'm probably making lots of rookie mistakes.  All comments welcome.


A better way of running series of SAS programs:

More information about the Image-SIG mailing list