From 50574632e6c2dd987cff8a0510cac3a7c5741470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Fri, 1 Aug 2014 07:23:55 +0200 Subject: [PATCH] Replaced margin color detection algorithm --- kcc/comic2ebook.py | 2 +- kcc/image.py | 70 +++++++++++++++++++++------------------------- 2 files changed, 33 insertions(+), 39 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 77a4c85..bc18b5e 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -425,7 +425,7 @@ def buildEPUB(path, chapterNames, tomeNumber): def imgOptimization(img, opt, hqImage=None): if not img.fill: - img.getImageFill(opt.webtoon) + img.getImageFill() if not opt.webtoon: img.cropWhiteSpace() if opt.cutpagenumbers and not opt.webtoon: 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 - 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 - startY += 5 + 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: - # 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 - startX += 5 - if fill > 0: + 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: - self.fill = 'white' + fill = 0 + startY = 0 + 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 + startX = 0 + 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' def isImageColor(self): v = ImageStat.Stat(self.image).var