[Image-SIG] Odd behavior in PIL 1.1.4 equalization

Fredrik Lundh fredrik at pythonware.com
Thu May 5 23:14:10 CEST 2005


I wrote:

> I'll into this for 1.1.6

make that "I'll look into this for 1.1.6".

> (patches are welcome).

I've attached a (preliminary) patch.

</F>

Index: PIL/ImageOps.py
===================================================================
--- PIL/ImageOps.py     (revision 2382)
+++ PIL/ImageOps.py     (working copy)
@@ -207,13 +207,15 @@
     h = image.histogram(mask)
     lut = []
     for b in range(0, len(h), 256):
-        step = reduce(operator.add, h[b:b+256]) / 255
-        if step == 0:
-            step = 1
-        n = 0
-        for i in range(256):
-            lut.append(n / step)
-            n = n + h[i+b]
+        histo = filter(None, h[b:b+256])
+        if len(histo) <= 1:
+            lut.append(range(256))
+        else:
+            step = (reduce(operator.add, histo) - histo[-1]) / 255
+            n = step / 2
+            for i in range(256):
+                lut.append(n / step)
+                n = n + h[i+b]
     return _lut(image, lut)

 ##





More information about the Image-SIG mailing list