From e3127ed516fc881ce70101ffcb0eb03eb415fe35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Tue, 12 Mar 2013 14:00:52 +0100 Subject: [PATCH] Code cleanup --- kcc/comic2ebook.py | 100 ++++++++---------------------------------- kcc/image.py | 105 ++++++++++++++++++++------------------------- 2 files changed, 65 insertions(+), 140 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index f21570b..d0d9b1c 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -272,12 +272,7 @@ def applyImgOptimization(img, isSplit=False, toRight=False): img.cropWhiteSpace(10.0) if options.cutpagenumbers: img.cutPageNumber() - if options.fakepanelview: - img.resizeImage(True, False, options.black_borders, False, False, False) - elif options.fakepanelviewlandscape: - img.resizeImage(False, False, options.black_borders, False, False, False, True) - else: - img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight, options.landscapemode) + img.resizeImage(options.upscale, options.stretch, options.black_borders, options.fakepanelviewlandscape, isSplit, toRight, options.landscapemode) if not options.notquantize: img.quantizeImage() @@ -318,58 +313,19 @@ def dirImgProcess(path): if facing == "right": splitCount += 1 facing = "left" - if options.fakepanelview: + if options.fakepanelview or options.fakepanelviewlandscape: img0 = image.ComicPage(split[0], options.profile) - splitA = img0.splitPageFakePanelView(dirpath, options.righttoleft) - img00 = image.ComicPage(splitA[0], options.profile) - applyImgOptimization(img00) - img00.saveToDir(dirpath, options.notquantize) - img01 = image.ComicPage(splitA[1], options.profile) - applyImgOptimization(img01) - img01.saveToDir(dirpath, options.notquantize) - img02 = image.ComicPage(splitA[2], options.profile) - applyImgOptimization(img02) - img02.saveToDir(dirpath, options.notquantize) - img03 = image.ComicPage(splitA[3], options.profile) - applyImgOptimization(img03) - img03.saveToDir(dirpath, options.notquantize) - img04 = image.ComicPage(splitA[4], options.profile) - applyImgOptimization(img04) - img04.saveToDir(dirpath, options.notquantize) img1 = image.ComicPage(split[1], options.profile) - splitB = img1.splitPageFakePanelView(dirpath, options.righttoleft) - img10 = image.ComicPage(splitB[0], options.profile) - applyImgOptimization(img10) - img10.saveToDir(dirpath, options.notquantize) - img11 = image.ComicPage(splitB[1], options.profile) - applyImgOptimization(img11) - img11.saveToDir(dirpath, options.notquantize) - img12 = image.ComicPage(splitB[2], options.profile) - applyImgOptimization(img12) - img12.saveToDir(dirpath, options.notquantize) - img13 = image.ComicPage(splitB[3], options.profile) - applyImgOptimization(img13) - img13.saveToDir(dirpath, options.notquantize) - img14 = image.ComicPage(splitB[4], options.profile) - applyImgOptimization(img14) - img14.saveToDir(dirpath, options.notquantize) - elif options.fakepanelviewlandscape: - img0 = image.ComicPage(split[0], options.profile) - splitA = img0.splitPageFakePanelViewLandscape(dirpath, options.righttoleft) - img01 = image.ComicPage(splitA[0], options.profile) - applyImgOptimization(img01) - img01.saveToDir(dirpath, options.notquantize) - img02 = image.ComicPage(splitA[1], options.profile) - applyImgOptimization(img02) - img02.saveToDir(dirpath, options.notquantize) - img1 = image.ComicPage(split[1], options.profile) - splitB = img1.splitPageFakePanelViewLandscape(dirpath, options.righttoleft) - img11 = image.ComicPage(splitB[0], options.profile) - applyImgOptimization(img11) - img11.saveToDir(dirpath, options.notquantize) - img12 = image.ComicPage(splitB[1], options.profile) - applyImgOptimization(img12) - img12.saveToDir(dirpath, options.notquantize) + splitA = img0.splitPageFakePanelView(dirpath, options.righttoleft, options.fakepanelviewlandscape) + splitB = img1.splitPageFakePanelView(dirpath, options.righttoleft, options.fakepanelviewlandscape) + for img in splitA: + tempImg = image.ComicPage(img, options.profile) + applyImgOptimization(tempImg) + tempImg.saveToDir(dirpath, options.notquantize) + for img in splitB: + tempImg = image.ComicPage(img, options.profile) + applyImgOptimization(tempImg) + tempImg.saveToDir(dirpath, options.notquantize) else: img0 = image.ComicPage(split[0], options.profile) applyImgOptimization(img0, True, toRight1) @@ -382,31 +338,12 @@ def dirImgProcess(path): facing = "left" else: facing = "right" - if options.fakepanelview: - split = img.splitPageFakePanelView(dirpath, options.righttoleft) - img0 = image.ComicPage(split[0], options.profile) - applyImgOptimization(img0) - img0.saveToDir(dirpath, options.notquantize) - img1 = image.ComicPage(split[1], options.profile) - applyImgOptimization(img1) - img1.saveToDir(dirpath, options.notquantize) - img2 = image.ComicPage(split[2], options.profile) - applyImgOptimization(img2) - img2.saveToDir(dirpath, options.notquantize) - img3 = image.ComicPage(split[3], options.profile) - applyImgOptimization(img3) - img3.saveToDir(dirpath, options.notquantize) - img4 = image.ComicPage(split[4], options.profile) - applyImgOptimization(img4) - img4.saveToDir(dirpath, options.notquantize) - elif options.fakepanelviewlandscape: - split = img.splitPageFakePanelViewLandscape(dirpath, options.righttoleft) - img1 = image.ComicPage(split[0], options.profile) - applyImgOptimization(img1) - img1.saveToDir(dirpath, options.notquantize) - img2 = image.ComicPage(split[1], options.profile) - applyImgOptimization(img2) - img2.saveToDir(dirpath, options.notquantize) + if options.fakepanelview or options.fakepanelviewlandscape: + split = img.splitPageFakePanelView(dirpath, options.righttoleft, options.fakepanelviewlandscape) + for img in split: + tempImg = image.ComicPage(img, options.profile) + applyImgOptimization(tempImg) + tempImg.saveToDir(dirpath, options.notquantize) else: applyImgOptimization(img) img.saveToDir(dirpath, options.notquantize) @@ -595,6 +532,7 @@ def checkOptions(): options.landscapemode = False if options.fakepanelview or options.fakepanelviewlandscape: options.imgproc = True + options.upscale = True options.rotate = False options.nosplitrotate = False if options.fakepanelview and options.fakepanelviewlandscape: diff --git a/kcc/image.py b/kcc/image.py index 86e8228..715a5a8 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -139,13 +139,13 @@ class ComicPage: palImg.putpalette(self.palette) self.image = self.image.quantize(palette=palImg) - def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, toRight=False, landscapeMode=False, fakepanelviewlandscape=False): + def resizeImage(self, upscale=False, stretch=False, black_borders=False, fakePanelViewLandscape=False, isSplit=False, toRight=False, landscapeMode=False): method = Image.ANTIALIAS if black_borders: fill = 'black' else: fill = 'white' - if fakepanelviewlandscape: + if fakePanelViewLandscape: self.image = self.image.rotate(90) if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]: if not upscale: @@ -221,69 +221,56 @@ class ComicPage: return None - def splitPageFakePanelView(self, targetdir, righttoleft=False): + def splitPageFakePanelView(self, targetdir, righttoleft=False, fakePanelWiewLandscape=False): width, height = self.image.size - topleftbox = (0, 0, ((width / 2) + (width/9)), ((height / 2) + (height/9))) - toprightbox = ((width / 2) - (width/9)), 0, width, ((height / 2) + (height/9)) - bottomleftbox = (0, ((height / 2) - (height/9)), ((width / 2) + (width/9)), height) - bototmrightbox = (((width / 2) - (width/9)), ((height / 2) - (height/9)), width, height) - filename = os.path.splitext(os.path.basename(self.origFileName)) - file0 = targetdir + '/' + filename[0] + '-0' + filename[1] - file1 = targetdir + '/' + filename[0] + '-1' + filename[1] - file2 = targetdir + '/' + filename[0] + '-2' + filename[1] - file3 = targetdir + '/' + filename[0] + '-3' + filename[1] - file4 = targetdir + '/' + filename[0] + '-4' + filename[1] - try: - if righttoleft: - page0 = self.image - page1 = self.image.crop(toprightbox) - page2 = self.image.crop(topleftbox) - page3 = self.image.crop(bototmrightbox) - page4 = self.image.crop(bottomleftbox) - else: - page0 = self.image - page1 = self.image.crop(topleftbox) - page2 = self.image.crop(toprightbox) - page3 = self.image.crop(bottomleftbox) - page4 = self.image.crop(bototmrightbox) - if page0.mode == "P": - page0.save(file0, "PNG") - page1.save(file1, "PNG") - page2.save(file2, "PNG") - page3.save(file3, "PNG") - page4.save(file4, "PNG") - else: + if fakePanelWiewLandscape: + topbox = (0, 0, width, ((height / 2) + (height/9))) + bottombox = (0, ((height / 2) - (height/9)), width, height) + filename = os.path.splitext(os.path.basename(self.origFileName)) + file1 = targetdir + '/' + filename[0] + '-1' + filename[1] + file2 = targetdir + '/' + filename[0] + '-2' + filename[1] + try: + page1 = self.image.crop(topbox) + page2 = self.image.crop(bottombox) + page1.save(file1) + page2.save(file2) + os.remove(self.origFileName) + except IOError as e: + raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, e)) + return file1, file2 + else: + topleftbox = (0, 0, ((width / 2) + (width/9)), ((height / 2) + (height/9))) + toprightbox = ((width / 2) - (width/9)), 0, width, ((height / 2) + (height/9)) + bottomleftbox = (0, ((height / 2) - (height/9)), ((width / 2) + (width/9)), height) + bototmrightbox = (((width / 2) - (width/9)), ((height / 2) - (height/9)), width, height) + filename = os.path.splitext(os.path.basename(self.origFileName)) + file0 = targetdir + '/' + filename[0] + '-0' + filename[1] + file1 = targetdir + '/' + filename[0] + '-1' + filename[1] + file2 = targetdir + '/' + filename[0] + '-2' + filename[1] + file3 = targetdir + '/' + filename[0] + '-3' + filename[1] + file4 = targetdir + '/' + filename[0] + '-4' + filename[1] + try: + if righttoleft: + page0 = self.image + page1 = self.image.crop(toprightbox) + page2 = self.image.crop(topleftbox) + page3 = self.image.crop(bototmrightbox) + page4 = self.image.crop(bottomleftbox) + else: + page0 = self.image + page1 = self.image.crop(topleftbox) + page2 = self.image.crop(toprightbox) + page3 = self.image.crop(bottomleftbox) + page4 = self.image.crop(bototmrightbox) page0.save(file0) page1.save(file1) page2.save(file2) page3.save(file3) page4.save(file4) - os.remove(self.origFileName) - except IOError as e: - raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, e)) - return file0, file1, file2, file3, file4 - - - def splitPageFakePanelViewLandscape(self, targetdir, righttoleft=False): - width, height = self.image.size - topbox = (0, 0, width, ((height / 2) + (height/9))) - bottombox = (0, ((height / 2) - (height/9)), width, height) - filename = os.path.splitext(os.path.basename(self.origFileName)) - file1 = targetdir + '/' + filename[0] + '-1' + filename[1] - file2 = targetdir + '/' + filename[0] + '-2' + filename[1] - try: - page1 = self.image.crop(topbox) - page2 = self.image.crop(bottombox) - if page1.mode == "P": - page1.save(file1, "PNG") - page2.save(file2, "PNG") - else: - page1.save(file1) - page2.save(file2) - os.remove(self.origFileName) - except IOError as e: - raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, e)) - return file1, file2 + os.remove(self.origFileName) + except IOError as e: + raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, e)) + return file0, file1, file2, file3, file4 # def frameImage(self):