[Image-SIG] passing parameters to functions used by Image.point?

Fredrik Lundh fredrik@pythonware.com
Wed, 9 Aug 2000 12:22:24 +0200

michael miller wrote:
>     def apply_thresholds(self):
>         self.out = self.image.point(lambda i: min(i,self.low))
> This doesn't work because self.low is not accessible to the
> lambda.  The traceback is 
> self.out = self.image.point(lambda i: min(i,self.low))
> NameError: self
> Can anyone help me with passing parameters such as self.low to
> lambda functions for Image.point?

you can use default arguments to explicitly pass objects from
your current local namespace to the lambda's namespace:

    self.out = self.image.point(lambda i, self=self: min(i, self.low))


    self.out = self.image.point(lambda i, low=self.low: min(i, low))

see also: http://www.python.org/doc/FAQ.html#4.5


or you can forget about lambdas and build the table "by hand":

    lut = range(0, low) + [low] * (256 - low)
    self.out = self.image.point(lut)

(or use a for loop)