[Image-SIG] combining enhancers

Bob Ippolito bob at redivi.com
Tue Sep 23 15:21:35 EDT 2003


On Tuesday, Sep 23, 2003, at 14:33 America/New_York, Chris Cogdon wrote:

>
> On Tuesday, Sep 23, 2003, at 11:14 US/Pacific, William Baxter wrote:
>
>> Is there a way to combine the output of enhancers in PIL? I'd like 2
>> sliders for brightness and contrast to be used for the same image. But
>> if I try to use ImageEnhance.Brightness and ImageEnhance.Contrast on 
>> the
>> same image, they keep resetting, as if it's 2 separate images.
>
> Note that the output of 'enhance' is a new image; it doesn't affect 
> the current image So you'd need to do this:
>
> # your image is in 'im'
>
> enh = ImageEnhance.Contrast ( im )
> im = enh.enhance ( 1 )
>
> enh = ImageEnhance.Brightness ( im )
> im = enh.enhance ( 1 )
>
>
> Yes, I think this is a little daft. It would have made a LOT more 
> sense to be able to pass the image to be enhanced into the 'enhance' 
> method, but it appears that some of the filters that are set up need 
> the source image as some kind of 'reference'. See the ImageEnhance.py 
> source for examples (eg: Sharpness)
>
> If you don't like it, you could always wrap those two statements in 
> it's own function :)

I would do something like this (in Python 2.2 or later, of course):

def filterChain(image, *filters)
	yield image
	for filter, parameter in filters:
		image = filter(image).enhance(parameter)
		yield image

# the 1's here would be your slider values
for newImage in filterChain(oldImage, (ImageEnhance.Contrast, 1), 
(ImageEnhance.Brightness, 1)):
	pass

newImage is now the fully filtered image, but you can peek at each step 
of the filter during the for loop if you wanted to show progress or 
something.

-bob




More information about the Image-SIG mailing list