I'm trying trying to use the Image.composite() function with the code below:

import Image

im1 = Image.open("GetGif1.png")
im2 = Image.open("GetGif2.png")
im3 = Image.composite('im1','im2',"L")

But I get this error:

Traceback (most recent call last):
  File "composite_images.py", line 11, in <module>
    im3 = Image.composite('im1','im2',"L")
  File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1960, in composite
    image = image2.copy()
AttributeError: 'str' object has no attribute 'copy'

Can someone please explain what is wrong with the snippet?

Thank you

10 Years
Discussion Span
Last Post by seamus.boyle

Hi seamus.boyle,

The problem is in line 5:

im3 = Image.composite('im1','im2',"L")

See, what you're doing is sending the string literals "im1" and "im2" as arguments to the function Image.composite(). But Image.composite() doesn't want string literals - it wants actual image objects, the variables themselves! Get rid of the single quotes and you should be fine:

im3 = Image.composite(im1,im2,"L")

Hope this helps!


Thanks for the reply G-Do

I had actually just left the quotes in after doing a trial and error approach with the snippet.

The actual problem was with the third argument (the "L"). It should have been an actual Image object.

Turns out the resulting image wasn't what I was after, now I'm using the ImageChops.darker() function

If anyone is interested I'm using this code:

import Image
import ImageChops

im1 = Image.open('GetGif1.gif').convert("RGB")
im2 = Image.open('GetGif2.gif').convert("RGB")
im3 = ImageChops.darker(im1, im2)

to replicate what I had been using in a shell script

$ composite $Symbol-gmma.gif $tmp/$$3.gif -compose darken $Symbol-gmma.gif
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.