views:

89

answers:

1

I have this code from the PIL-handbook but I get an error message.

from PIL import Image, ImageEnhance, ImageChops

im = Image.open("D:\\Python26\\PYTHON-PROGRAMME\\bild.jpg")

# split the image into individual bands
source = im.split()

R, G, B = 0, 1, 2

# select regions where red is less than 100
mask = source[R].point(lambda i: i < 100 and 255)

# process the green band
out = source[G].point(lambda i: i * 0.7)

# paste the processed band back, but only where red was < 100
source[G].paste(out, None, mask)

# build a new multiband image
im = Image.merge(im.mode, source)
im = im.point(lambda i: expression and 255)

im.save("D:\\Python26\\PYTHON-PROGRAMME\\bild2.jpg")
print('done')

Error:

Traceback (most recent call last):
  File "D:\Python26\PYTHON-PROGRAMME\00000000000000000", line 14, in <module>
    out = source[G].point(lambda i: i * 0.7)
IndexError: tuple index out of range
+3  A: 

Code runs fine for me, on a colour image.

I would expect you to get the quoted error if you tried to operate on an image that only had one colour channel — ie. a black-and-white JPEG. How about im= im.convert('RGB') before the split, to be sure?

bobince
wow! you must be a great programmer :) I does work now
kame