about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPaweł Jastrzębski <[email protected]>2015-09-04 16:06:18 +0200
committerPaweł Jastrzębski <[email protected]>2015-09-04 16:06:18 +0200
commit0988601842bb5bc366c82812fb7ba1f404df7abc (patch)
tree190ccd601f0ed4622448ada21920c26224894439
parentCode cleanup (diff)
downloadkcc-0988601842bb5bc366c82812fb7ba1f404df7abc.tar.gz
kcc-0988601842bb5bc366c82812fb7ba1f404df7abc.tar.bz2
kcc-0988601842bb5bc366c82812fb7ba1f404df7abc.zip
Implemented new method to detect color images
-rwxr-xr-xkcc/image.py38
1 files changed, 17 insertions, 21 deletions
diff --git a/kcc/image.py b/kcc/image.py
index c0f0bf2..0cf4ac7 100755
--- a/kcc/image.py
+++ b/kcc/image.py
@@ -442,29 +442,25 @@ class ComicPage:
                 self.fill = 'white'
 
     def isImageColor(self):
-        v = ImageStat.Stat(self.image).var
-        isMonochromatic = reduce(lambda x, y: x and y < 0.005, v, True)
-        if isMonochromatic:
-            # Monochromatic
-            return False
-        else:
-            if len(v) == 3:
-                maxmin = abs(max(v) - min(v))
-                if maxmin > 1000:
-                    # Color
-                    return True
-                elif maxmin > 100:
-                    # Probably color
-                    return True
-                else:
-                    # Grayscale
-                    return False
-            elif len(v) == 1:
-                # Black and white
+        img = self.image.copy()
+        bands = img.getbands()
+        if bands == ('R', 'G', 'B') or bands == ('R', 'G', 'B', 'A'):
+            thumb = img.resize((40, 40))
+            SSE, bias = 0, [0, 0, 0]
+            bias = ImageStat.Stat(thumb).mean[:3]
+            bias = [b - sum(bias) / 3 for b in bias]
+            for pixel in thumb.getdata():
+                mu = sum(pixel) / 3
+                SSE += sum((pixel[i] - mu - bias[i]) * (pixel[i] - mu - bias[i]) for i in [0, 1, 2])
+            MSE = float(SSE) / (40 * 40)
+            if MSE <= 22:
                 return False
             else:
-                # Detection failed
-                return False
+                return True
+        elif len(bands) == 1:
+            return False
+        else:
+            return False
 
 
 class Cover: