about summary refs log tree commit diff
path: root/kcc/image.py
diff options
context:
space:
mode:
authorPaweł Jastrzębski <pawelj@iosphe.re>2014-08-01 07:23:55 +0200
committerPaweł Jastrzębski <pawelj@iosphe.re>2014-08-01 07:23:55 +0200
commit50574632e6c2dd987cff8a0510cac3a7c5741470 (patch)
tree868b28377d96948f8381c4495cc5f99c74283232 /kcc/image.py
parentKindle: Fixed high quality mode (close #106) (diff)
downloadkcc-50574632e6c2dd987cff8a0510cac3a7c5741470.tar.gz
kcc-50574632e6c2dd987cff8a0510cac3a7c5741470.tar.bz2
kcc-50574632e6c2dd987cff8a0510cac3a7c5741470.zip
Replaced margin color detection algorithm
Diffstat (limited to '')
-rwxr-xr-xkcc/image.py68
1 files changed, 31 insertions, 37 deletions
diff --git a/kcc/image.py b/kcc/image.py
index fd00b90..fd7379c 100755
--- a/kcc/image.py
+++ b/kcc/image.py
@@ -404,50 +404,44 @@ class ComicPage:
 
     def getImageHistogram(self, image):
         histogram = image.histogram()
-        RBGW = []
-        pixelCount = 0
-        for i in range(256):
-            pixelCount += histogram[i] + histogram[256 + i] + histogram[512 + i]
-            RBGW.append(histogram[i] + histogram[256 + i] + histogram[512 + i])
-        white = 0
-        black = 0
-        for i in range(251, 256):
-            white += RBGW[i]
-        for i in range(5):
-            black += RBGW[i]
-        if black > pixelCount*0.8 and white == 0:
-            return 1
-        elif white > pixelCount*0.8 and black == 0:
+        if histogram[0] == 0:
             return -1
+        elif histogram[255] == 0:
+            return 1
         else:
-            return False
+            return 0
 
-    def getImageFill(self, webtoon):
-        fill = 0
-        if not webtoon and not self.rotated:
-            # Search for horizontal solid lines
+    def getImageFill(self):
+        bw = self.image.convert('L').point(lambda x: 0 if x < 128 else 255, '1')
+        imageBoxA = bw.getbbox()
+        imageBoxB = ImageChops.invert(bw).getbbox()
+        if imageBoxA is None or imageBoxB is None:
+            surfaceB, surfaceW = 0, 0
+        else:
+            surfaceB = (imageBoxA[2] - imageBoxA[0]) * (imageBoxA[3] - imageBoxA[1])
+            surfaceW = (imageBoxB[2] - imageBoxB[0]) * (imageBoxB[3] - imageBoxB[1])
+        if surfaceW < surfaceB:
+            self.fill = 'white'
+        elif surfaceW > surfaceB:
+            self.fill = 'black'
+        else:
+            fill = 0
             startY = 0
-            while startY < self.image.size[1]:
-                if startY + 5 > self.image.size[1]:
-                    startY = self.image.size[1] - 5
-                checkSolid = self.getImageHistogram(self.image.crop((0, startY, self.image.size[0], startY+5)))
-                if checkSolid:
-                    fill += checkSolid
+            while startY < bw.size[1]:
+                if startY + 5 > bw.size[1]:
+                    startY = bw.size[1] - 5
+                fill += self.getImageHistogram(bw.crop((0, startY, bw.size[0], startY+5)))
                 startY += 5
-        else:
-            # Search for vertical solid lines
             startX = 0
-            while startX < self.image.size[0]:
-                if startX + 5 > self.image.size[0]:
-                    startX = self.image.size[0] - 5
-                checkSolid = self.getImageHistogram(self.image.crop((startX, 0, startX+5, self.image.size[1])))
-                if checkSolid:
-                    fill += checkSolid
+            while startX < bw.size[0]:
+                if startX + 5 > bw.size[0]:
+                    startX = bw.size[0] - 5
+                fill += self.getImageHistogram(bw.crop((startX, 0, startX+5, bw.size[1])))
                 startX += 5
-        if fill > 0:
-            self.fill = 'black'
-        else:
-            self.fill = 'white'
+            if fill > 0:
+                self.fill = 'black'
+            else:
+                self.fill = 'white'
 
     def isImageColor(self):
         v = ImageStat.Stat(self.image).var