[Image-SIG] Odd behavior in PIL 1.1.4 equalization

Fredrik Lundh fredrik at pythonware.com
Sat May 7 11:09:14 CEST 2005

```> I've attached a (preliminary) patch.

here's a sligtly less broken patch:

Index: PIL/ImageOps.py
===================================================================
--- PIL/ImageOps.py     (revision 2134)
+++ PIL/ImageOps.py     (working copy)
@@ -9,6 +9,7 @@
# 2001-10-23 fl   Added autocontrast operator
# 2001-12-18 fl   Added Kevin's fit operator
# 2004-03-14 fl   Fixed potential division by zero in equalize
+# 2005-05-05 fl   Fixed equalize for low number of values
#
# Copyright (c) 2001-2004 by Secret Labs AB
# Copyright (c) 2001-2004 by Fredrik Lundh
@@ -207,13 +208,18 @@
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.extend(range(256))
+        else:
+            step = (reduce(operator.add, histo) - histo[-1]) / 255
+            if not step:
+                lut.extend(range(256))
+            else:
+                n = step / 2
+                for i in range(256):
+                    lut.append(n / step)
+                    n = n + h[i+b]
return _lut(image, lut)

##

```