ImageEnhance.Contrast - is this fishy or what?

Scott David Daniels Scott.Daniels at Acm.Org
Mon Jun 15 17:05:05 EDT 2009


roop wrote:
> I was browsing ImageEnhace.py, and found something that I thought was
> odd in class Contrast:
> 
> class Contrast(_Enhance):
>     "Adjust image contrast"
>     def __init__(self, image):
>         self.image = image
>         mean = reduce(lambda a,b: a+b, image.convert("L").histogram())/
> 256.0
>         self.degenerate = Image.new("L", image.size, mean).convert
> (image.mode)
> 
> Isn't reduce(lambda a,b: a+b, image.convert("L").histogram()) the same
> as (image.size[0] * image.size[1]) - just count the number of pixels
> in the image? (Afaik, the histogram is a list of 256 elements where
> the ith element gives the number of pixels with i as the pixel value
> (0 <= i < 256)). Is this actually fishy or have I got it all muddled
> up?
> 
> Thanks,
> roop
Good catch [I'll send a copy to the imaging sig].  If you replace class
Contrast like this:

     class Contrast(ImageEnhance._Enhance):
         "Adjust image contrast"
         def __init__(self, image):
             self.image = image
             w, h = image.size
             mean = sum(n * c for n, c in enumerate(
                            image.convert("L").histogram())
                        ) / float(w * h)
             self.degenerate = Image.new("L", image.size,
                                mean).convert(image.mode)


You should get a working Contrast class.
There _is_ another possibility:


     class Contrast(ImageEnhance._Enhance):
         "Adjust image contrast"
         def __init__(self, image):
             self.image = image
             clone = image.copy()
             clone.thumbnail((1, 1))
             self.degenerate = clone.resize(image.size)


The former goes to a grey point, while the latter goes to
the picture's color center.  I'm not quite sure which one
is more properly called "contrast," as I can see wanting
either one.


--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list