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")
im.save('output.png')

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

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)
#im3.save('output.png')
im3.show()

to replicate what I had been using in a shell script

$ composite $Symbol-gmma.gif $tmp/$$3.gif -compose darken $Symbol-gmma.gif
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.