From 2865915cdf52616d4b941713d2a0432170362090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sun, 11 Aug 2013 14:47:06 +0200 Subject: [PATCH] comic2panel: Autodetect border color --- kcc/comic2panel.py | 15 ++++++++++++++- kcc/image.py | 19 ++++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/kcc/comic2panel.py b/kcc/comic2panel.py index 79767aa..eeab8c8 100644 --- a/kcc/comic2panel.py +++ b/kcc/comic2panel.py @@ -89,6 +89,19 @@ def splitImage(work): debugImage = Image.open(os.path.join(path, name)) draw = ImageDraw.Draw(debugImage) + # Find fill color + white = 0 + black = 0 + for i in image.getdata(): + if i == (255, 255, 255): + white += 1 + elif i == (0, 0, 0): + black += 1 + if white > black: + fill = 'KCCFW' + else: + fill = 'KCCFB' + # Find panels y1 = 0 y2 = 10 @@ -148,7 +161,7 @@ def splitImage(work): panelImg = image.crop([0, panels[panel][0], widthImg, panels[panel][1]]) newPage.paste(panelImg, (0, targetHeight)) targetHeight += panels[panel][2] - newPage.save(os.path.join(path, fileExpanded[0] + '-' + str(pageNumber) + '.png'), 'PNG') + newPage.save(os.path.join(path, fileExpanded[0] + '-' + str(pageNumber) + '-' + fill + '.png'), 'PNG') pageNumber += 1 print ".", os.remove(os.path.join(path, name)) diff --git a/kcc/image.py b/kcc/image.py index 83e772c..b028dd6 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -115,6 +115,7 @@ class ComicPage: # Detect corrupted files - Phase 2 try: self.origFileName = source + self.filename = os.path.basename(self.origFileName) self.image = Image.open(source) except IOError: raise RuntimeError('Cannot read image file %s' % source) @@ -134,7 +135,6 @@ class ComicPage: self.image = self.image.convert('RGB') def saveToDir(self, targetdir, forcepng, color, wipe, suffix=None): - filename = os.path.basename(self.origFileName) try: if not color: self.image = self.image.convert('L') # convert to grayscale @@ -143,13 +143,13 @@ class ComicPage: else: suffix = "" if wipe: - os.remove(os.path.join(targetdir, filename)) + os.remove(os.path.join(targetdir, self.filename)) else: suffix += "_kcchq" if forcepng: - self.image.save(os.path.join(targetdir, os.path.splitext(filename)[0] + suffix + ".png"), "PNG") + self.image.save(os.path.join(targetdir, os.path.splitext(self.filename)[0] + suffix + ".png"), "PNG") else: - self.image.save(os.path.join(targetdir, os.path.splitext(filename)[0] + suffix + ".jpg"), "JPEG") + self.image.save(os.path.join(targetdir, os.path.splitext(self.filename)[0] + suffix + ".jpg"), "JPEG") except IOError as e: raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, e)) @@ -174,10 +174,15 @@ class ComicPage: def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, toRight=False, landscapeMode=False, qualityMode=0): method = Image.ANTIALIAS - if black_borders: + if '-KCCFW' in str(self.filename): + fill = 'white' + elif '-KCCFB' in str(self.filename): fill = 'black' else: - fill = 'white' + if black_borders: + fill = 'black' + else: + fill = 'white' if qualityMode == 0: size = (self.size[0], self.size[1]) else: @@ -234,7 +239,7 @@ class ComicPage: # source is portrait and target is landscape, so split by the height leftbox = (0, 0, width, height / 2) rightbox = (0, height / 2, width, height) - filename = os.path.splitext(os.path.basename(self.origFileName)) + filename = os.path.splitext(self.filename) fileone = targetdir + '/' + filename[0] + '_kcca' + filename[1] filetwo = targetdir + '/' + filename[0] + '_kccb' + filename[1] try: