Search code examples
pythonpython-imaging-library

Python PIL combine images results in an all black image


I am trying to use the following to combine three images into one using PIL and Python

import sys
from PIL import Image

images = map(Image.open, ['ib1.jpg', 'ib2.jpg', 'ib3.jpg'])
widths, heights = zip(*(i.size for i in images))

total_width = sum(widths)
max_height = max(heights)

new_im = Image.new('RGB', (total_width, max_height))

x_offset = 0
for im in images:
  new_im.paste(im, (x_offset,0))
  x_offset += im.size[0]

new_im.save('test.jpg')

The test.jpg image seems to be the correct height but the image is totally black.

What can I try next?


Solution

  • 2016-03-29 Edit:

    map in Python 3 returns a generator, and it got exhausted in your zip function call, so the generator just generates an empty list in your for loop. You can change

    images = map(Image.open, ['ib1.jpg', 'ib2.jpg', 'ib3.jpg'])
    

    to

    images = list(map(Image.open, ['ib1.jpg', 'ib2.jpg', 'ib3.jpg']))
    

    It should work as you expect.

    Older post:

    I have tested on my machine and it seems that the same code executed in Python 2(2.7.11 on my machine) works as you expects but Python 3(3.5.1) don't. I am figureing out why.