[Image-SIG] Re: Getting an error in Point operations code from the pil-handbook.

Fredrik Lundh fredrik@pythonware.com
Thu, 24 Apr 2003 11:33:45 +0200


David McDonnell wrote:

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

where did you get this code?  in the pythonware.com version,
it says:

    mask = source[R].point(lambda i: i < 100 and 255)

note the missing "<" in your version.


as for lambda, it's just another way to write a simple function:

    ...point(lambda i: i < 100 and 255)

is the same thing as

    def somename(i):
        return i < 100 and 255

    ...point(somename)

with the exception that you don't have to make up a name.


as for what the "i < 100 and 255" does, that's briefly explained in
the pil introduction (just below the code sample).


if you don't want to use lambdas and strange expressions, you can
spell it all out as:

    def mymapfunction(i):
        if i < 100:
            return 255
        return 0

    ...point(mymapfunction)

alternatively, you can pass in a list of values, instead of a mapping
function:

    lut = []
    for i in range(255):
        if i < 100:
            lut.append(255)
        else:
            lut.append(0)
    ...point(lut)

for more on lambdas, see the Python language reference (available
from www.python.org)

</F>