From c5983276e5f24e87b5c4b3b4d064a7bacad7de80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sun, 10 Mar 2013 00:03:11 +0100 Subject: [PATCH 01/22] Added --fakepanelview option --- kcc/comic2ebook.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index e1fd41f..8328867 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -430,6 +430,8 @@ def main(argv=None): help="Comic title [Default=filename]") parser.add_option("-m", "--manga-style", action="store_true", dest="righttoleft", default=False, help="Manga style (Right-to-left reading and splitting) [Default=False]") + parser.add_option("--fakepanelview", action="store_true", dest="fakepanelview", default=False, + help="Emulate Panel View feature (For Kindle 4 NT or older) [Default=False]") parser.add_option("--noprocessing", action="store_false", dest="imgproc", default=True, help="Do not apply image preprocessing (Page splitting and optimizations) [Default=True]") parser.add_option("--nodithering", action="store_true", dest="notquantize", default=False, @@ -454,6 +456,8 @@ def main(argv=None): parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Verbose output [Default=False]") options, args = parser.parse_args(argv) + if options.fakepanelview = True and options.profile = "KHD": + options.fakepanelview = False if len(args) != 1: parser.print_help() return From 0cabbfde961393d9cf8a4cc5b650a74d6760e916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sun, 10 Mar 2013 10:43:34 +0100 Subject: [PATCH 02/22] Additional tweaks of Landscape mode --- kcc/comic2ebook.py | 61 +++++++++++++++++++++++++++++++--------------- kcc/image.py | 6 ++--- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 8328867..cb21c7e 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -130,7 +130,6 @@ def buildNCX(dstdir, title, chapters): def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False): opffile = os.path.join(dstdir, 'OEBPS', 'content.opf') - # read the first file resolution profilelabel, deviceres, palette, gamma = image.ProfileData.Profiles[profile] imgres = str(deviceres[0]) + "x" + str(deviceres[1]) if righttoleft: @@ -159,16 +158,22 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False): "\n", "\n", "\n", - "\n", - "\n", - "\n", + "\n" + ]) + if options.landscapemode: + f.writelines(["\n", + "\n" + ]) + else: + f.writelines(["\n", + "\n" + ]) + f.writelines(["\n", "\n", "\n", - "\n", "\n\n\n" ]) - # set cover if cover is not None: filename = getImageFileName(cover.replace(os.path.join(dstdir, 'OEBPS'), '').lstrip('/').lstrip('\\\\')) if '.png' == filename[1]: @@ -191,7 +196,7 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False): mt = 'image/jpeg' f.write("\n") - if (options.profile == 'K4' or options.profile == 'KHD') and splitCount > 0: + if options.landscapemode and splitCount > 0: splitCountUsed = 1 while splitCountUsed <= splitCount: f.write("\n") splitCountUsed += 1 - f.write("\n") + if options.landscapemode: + f.write("\n") + else: + f.write("\n") elif entry.endswith("-2"): - f.write("\n") + if options.landscapemode: + f.write("\n") + else: + f.write("\n") if righttoleft: facing = "right" else: facing = "left" else: - f.write("\n") + if options.landscapemode: + f.write("\n") + else: + f.write("\n") if facing == 'right': facing = 'left' else: facing = 'right' f.write("\n\n\n\n") f.close() - # finish with standard ePub folders os.mkdir(os.path.join(dstdir, 'META-INF')) f = open(os.path.join(dstdir, 'mimetype'), 'w') f.write('application/epub+zip') @@ -355,7 +367,6 @@ def genEpubStruct(path): for afile in filenames: filename = getImageFileName(afile) if filename is not None: - # put credits at the end if "credit" in afile.lower(): os.rename(os.path.join(dirpath, afile), os.path.join(dirpath, 'ZZZ999_' + afile)) afile = 'ZZZ999_' + afile @@ -371,12 +382,11 @@ def genEpubStruct(path): cover = os.path.join(filelist[-1][0], 'cover' + getImageFileName(filelist[-1][1])[1]) copyfile(os.path.join(filelist[-1][0], filelist[-1][1]), cover) buildNCX(path, options.title, chapterlist) - # ensure we're sorting files alphabetically convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)] filelist.sort(key=lambda name: (alphanum_key(name[0].lower()), alphanum_key(name[1].lower()))) buildOPF(options.profile, path, options.title, filelist, cover, options.righttoleft) - if (options.profile == 'K4' or options.profile == 'KHD') and splitCount > 0: + if options.landscapemode and splitCount > 0: filelist.append(buildBlankHTML(os.path.join(path, 'OEBPS', 'Text'))) @@ -456,8 +466,7 @@ def main(argv=None): parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Verbose output [Default=False]") options, args = parser.parse_args(argv) - if options.fakepanelview = True and options.profile = "KHD": - options.fakepanelview = False + checkOptions() if len(args) != 1: parser.print_help() return @@ -470,7 +479,6 @@ def main(argv=None): dirImgProcess(path + "/OEBPS/Images/") print "\nCreating ePub structure..." genEpubStruct(path) - # actually zip the ePub if options.output is not None: if options.output.endswith('.epub'): epubpath = os.path.abspath(options.output) @@ -488,6 +496,21 @@ def main(argv=None): rmtree(path) return epubpath +def checkOptions(): + global options + if options.profile == 'K4' or options.profile == 'KHD': + options.landscapemode = True + else: + options.landscapemode = False + if options.fakepanelview and options.profile == 'KHD': + options.fakepanelview = False + if options.fakepanelview and options.landscapemode: + options.landscapemode = False + if options.fakepanelview: + options.imgproc = True + options.rotate = False + options.nosplitrotate = False + def getEpubPath(): global epub_path diff --git a/kcc/image.py b/kcc/image.py index 8eeae69..725956f 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -147,7 +147,7 @@ class ComicPage: fill = 'white' if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]: if not upscale: - if isSplit and (self.profile == 'K4' or self.profile == 'KHD'): + if isSplit and options.landscapemode: borderw = (self.size[0] - self.image.size[0]) borderh = (self.size[1] - self.image.size[1]) / 2 self.image = ImageOps.expand(self.image, border=(0, borderh), fill=fill) @@ -165,14 +165,14 @@ class ComicPage: else: method = Image.NEAREST - if stretch: # if stretching call directly resize() without other considerations. + if stretch: self.image = self.image.resize(self.size, method) return self.image ratioDev = float(self.size[0]) / float(self.size[1]) if (float(self.image.size[0]) / float(self.image.size[1])) < ratioDev: diff = int(self.image.size[1] * ratioDev) - self.image.size[0] - if isSplit and (self.profile == 'K4' or self.profile == 'KHD'): + if isSplit and options.landscapemode: diff = 2 self.image = ImageOps.expand(self.image, border=(diff / 2, 0), fill=fill) elif (float(self.image.size[0]) / float(self.image.size[1])) > ratioDev: From eb7766a5e5f9675c41117ef7c908afe7065dc7a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sun, 10 Mar 2013 11:58:52 +0100 Subject: [PATCH 03/22] Draft of FakePanelView mode --- kcc/comic2ebook.py | 60 +++++++++++++++++++++++++++++++++++++++------- kcc/image.py | 50 +++++++++++++++++++++++++++++++++++--- 2 files changed, 98 insertions(+), 12 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index cb21c7e..1684bdd 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -272,7 +272,7 @@ def applyImgOptimization(img, isSplit=False, toRight=False): img.cropWhiteSpace(10.0) if options.cutpagenumbers: img.cutPageNumber() - img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight) + img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight, options.landscapemode) if not options.notquantize: img.quantizeImage() @@ -313,19 +313,61 @@ def dirImgProcess(path): if facing == "right": splitCount += 1 facing = "left" - img0 = image.ComicPage(split[0], options.profile) - applyImgOptimization(img0, True, toRight1) - img0.saveToDir(dirpath, options.notquantize) - img1 = image.ComicPage(split[1], options.profile) - applyImgOptimization(img1, True, toRight2) - img1.saveToDir(dirpath, options.notquantize) + if options.fakepanelview: + img0 = image.ComicPage(split[0], options.profile) + applyImgOptimization(img0, True) + splitA = img0.splitPageFakePanelView(dirpath, options.righttoleft) + img00 = image.ComicPage(splitA[0], options.profile) + img00.saveToDir(dirpath, options.notquantize) + img01 = image.ComicPage(splitA[1], options.profile) + img01.saveToDir(dirpath, options.notquantize) + img02 = image.ComicPage(splitA[2], options.profile) + img02.saveToDir(dirpath, options.notquantize) + img03 = image.ComicPage(splitA[3], options.profile) + img03.saveToDir(dirpath, options.notquantize) + img04 = image.ComicPage(splitA[4], options.profile) + img04.saveToDir(dirpath, options.notquantize) + img1 = image.ComicPage(split[1], options.profile) + applyImgOptimization(img1, True) + splitB = img1.splitPageFakePanelView(dirpath, options.righttoleft) + img10 = image.ComicPage(splitB[0], options.profile) + img10.saveToDir(dirpath, options.notquantize) + img11 = image.ComicPage(splitB[1], options.profile) + img11.saveToDir(dirpath, options.notquantize) + img12 = image.ComicPage(splitB[2], options.profile) + img12.saveToDir(dirpath, options.notquantize) + img13 = image.ComicPage(splitB[3], options.profile) + img13.saveToDir(dirpath, options.notquantize) + img14 = image.ComicPage(splitB[4], options.profile) + img14.saveToDir(dirpath, options.notquantize) + else: + img0 = image.ComicPage(split[0], options.profile) + applyImgOptimization(img0, True, toRight1) + img0.saveToDir(dirpath, options.notquantize) + img1 = image.ComicPage(split[1], options.profile) + applyImgOptimization(img1, True, toRight2) + img1.saveToDir(dirpath, options.notquantize) else: if facing == "right": facing = "left" else: facing = "right" - applyImgOptimization(img) - img.saveToDir(dirpath, options.notquantize) + if options.fakepanelview: + applyImgOptimization(img) + split = img.splitPageFakePanelView(dirpath, options.righttoleft) + img0 = image.ComicPage(split[0], options.profile) + img0.saveToDir(dirpath, options.notquantize) + img1 = image.ComicPage(split[1], options.profile) + img1.saveToDir(dirpath, options.notquantize) + img2 = image.ComicPage(split[2], options.profile) + img2.saveToDir(dirpath, options.notquantize) + img3 = image.ComicPage(split[3], options.profile) + img3.saveToDir(dirpath, options.notquantize) + img4 = image.ComicPage(split[4], options.profile) + img4.saveToDir(dirpath, options.notquantize) + else: + applyImgOptimization(img) + img.saveToDir(dirpath, options.notquantize) def genEpubStruct(path): diff --git a/kcc/image.py b/kcc/image.py index 725956f..8121c84 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -139,7 +139,7 @@ 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): + def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, toRight=False, landscapeMode=False): method = Image.ANTIALIAS if black_borders: fill = 'black' @@ -147,7 +147,7 @@ class ComicPage: fill = 'white' if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]: if not upscale: - if isSplit and options.landscapemode: + if isSplit and landscapeMode: borderw = (self.size[0] - self.image.size[0]) borderh = (self.size[1] - self.image.size[1]) / 2 self.image = ImageOps.expand(self.image, border=(0, borderh), fill=fill) @@ -172,7 +172,7 @@ class ComicPage: ratioDev = float(self.size[0]) / float(self.size[1]) if (float(self.image.size[0]) / float(self.image.size[1])) < ratioDev: diff = int(self.image.size[1] * ratioDev) - self.image.size[0] - if isSplit and options.landscapemode: + if isSplit and landscapeMode: diff = 2 self.image = ImageOps.expand(self.image, border=(diff / 2, 0), fill=fill) elif (float(self.image.size[0]) / float(self.image.size[1])) > ratioDev: @@ -218,6 +218,50 @@ class ComicPage: else: return None + + def splitPageFakePanelView(self, targetdir, righttoleft=False): + width, height = self.image.size + topleftbox = (0, 0, width / 2, height / 2) + toprightbox = (width / 2, 0, width, height / 2) + bottomleftbox = (0, height / 2, width / 2, height) + bototmrightbox = (width / 2, height / 2, 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: + 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 frameImage(self): foreground = tuple(self.palette[:3]) background = tuple(self.palette[-3:]) From 712f728a5efe584f5d62bc108e706010eab8ff8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sun, 10 Mar 2013 17:29:06 +0100 Subject: [PATCH 04/22] Draft of FakePanelView mode #2 --- kcc/comic2ebook.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 1684bdd..cf8dfa4 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -272,7 +272,10 @@ def applyImgOptimization(img, isSplit=False, toRight=False): img.cropWhiteSpace(10.0) if options.cutpagenumbers: img.cutPageNumber() - img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight, options.landscapemode) + if options.fakepanelview: + img.resizeImage(True, False, options.black_borders, False, False, False) + else: + img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight, options.landscapemode) if not options.notquantize: img.quantizeImage() @@ -315,30 +318,38 @@ def dirImgProcess(path): facing = "left" if options.fakepanelview: img0 = image.ComicPage(split[0], options.profile) - applyImgOptimization(img0, True) 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) - applyImgOptimization(img1, True) 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) else: img0 = image.ComicPage(split[0], options.profile) @@ -353,17 +364,21 @@ def dirImgProcess(path): else: facing = "right" if options.fakepanelview: - applyImgOptimization(img) 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) else: applyImgOptimization(img) From ca5854a8bd88b6034f975cb6062f3b4959cec7bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sun, 10 Mar 2013 18:18:20 +0100 Subject: [PATCH 05/22] Real Panel View don't split in half --- kcc/image.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kcc/image.py b/kcc/image.py index 8121c84..52ef7e9 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -221,10 +221,10 @@ class ComicPage: def splitPageFakePanelView(self, targetdir, righttoleft=False): width, height = self.image.size - topleftbox = (0, 0, width / 2, height / 2) - toprightbox = (width / 2, 0, width, height / 2) - bottomleftbox = (0, height / 2, width / 2, height) - bototmrightbox = (width / 2, height / 2, width, height) + 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] From 750b55649a942d266d812735c21db15355cad1ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Tue, 12 Mar 2013 10:47:37 +0100 Subject: [PATCH 06/22] Draft of FakePanelView Landscape mode --- kcc/comic2ebook.py | 38 +++++++++++++++++++++++++++++++++++--- kcc/image.py | 26 +++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index cf8dfa4..f21570b 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -274,6 +274,8 @@ def applyImgOptimization(img, isSplit=False, toRight=False): 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) if not options.notquantize: @@ -351,6 +353,23 @@ def dirImgProcess(path): 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) else: img0 = image.ComicPage(split[0], options.profile) applyImgOptimization(img0, True, toRight1) @@ -380,6 +399,14 @@ def dirImgProcess(path): 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) else: applyImgOptimization(img) img.saveToDir(dirpath, options.notquantize) @@ -499,6 +526,8 @@ def main(argv=None): help="Manga style (Right-to-left reading and splitting) [Default=False]") parser.add_option("--fakepanelview", action="store_true", dest="fakepanelview", default=False, help="Emulate Panel View feature (For Kindle 4 NT or older) [Default=False]") + parser.add_option("--fakepanelviewlandscape", action="store_true", dest="fakepanelviewlandscape", default=False, + help="Emulate Panel View feature - Landscape mode (For Kindle 4 NT or older) [Default=False]") parser.add_option("--noprocessing", action="store_false", dest="imgproc", default=True, help="Do not apply image preprocessing (Page splitting and optimizations) [Default=True]") parser.add_option("--nodithering", action="store_true", dest="notquantize", default=False, @@ -559,14 +588,17 @@ def checkOptions(): options.landscapemode = True else: options.landscapemode = False - if options.fakepanelview and options.profile == 'KHD': + if (options.fakepanelview or options.fakepanelviewlandscape) and options.profile == 'KHD': options.fakepanelview = False - if options.fakepanelview and options.landscapemode: + options.fakepanelviewlandscape = False + if (options.fakepanelview or options.fakepanelviewlandscape) and options.landscapemode: options.landscapemode = False - if options.fakepanelview: + if options.fakepanelview or options.fakepanelviewlandscape: options.imgproc = True options.rotate = False options.nosplitrotate = False + if options.fakepanelview and options.fakepanelviewlandscape: + options.fakepanelviewlandscape = False def getEpubPath(): diff --git a/kcc/image.py b/kcc/image.py index 52ef7e9..809c0c0 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -139,12 +139,14 @@ 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): + def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, toRight=False, landscapeMode=False, fakepanelviewlandscape=False): method = Image.ANTIALIAS if black_borders: fill = 'black' else: fill = 'white' + 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: if isSplit and landscapeMode: @@ -262,6 +264,28 @@ class ComicPage: 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 + + def frameImage(self): foreground = tuple(self.palette[:3]) background = tuple(self.palette[-3:]) From 93a98db43ed599fadfccc2c540f89288c1486a85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Tue, 12 Mar 2013 10:48:30 +0100 Subject: [PATCH 07/22] Disabled unused functions --- kcc/image.py | 112 +++++++++++++++++++++++++-------------------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/kcc/image.py b/kcc/image.py index 809c0c0..86e8228 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -286,28 +286,28 @@ class ComicPage: return file1, file2 - def frameImage(self): - foreground = tuple(self.palette[:3]) - background = tuple(self.palette[-3:]) - widthDev, heightDev = self.size - widthImg, heightImg = self.image.size - pastePt = ( - max(0, (widthDev - widthImg) / 2), - max(0, (heightDev - heightImg) / 2) - ) - corner1 = ( - pastePt[0] - 1, - pastePt[1] - 1 - ) - corner2 = ( - pastePt[0] + widthImg + 1, - pastePt[1] + heightImg + 1 - ) - imageBg = Image.new(self.image.mode, self.size, background) - imageBg.paste(self.image, pastePt) - draw = ImageDraw.Draw(imageBg) - draw.rectangle([corner1, corner2], outline=foreground) - self.image = imageBg + # def frameImage(self): + # foreground = tuple(self.palette[:3]) + # background = tuple(self.palette[-3:]) + # widthDev, heightDev = self.size + # widthImg, heightImg = self.image.size + # pastePt = ( + # max(0, (widthDev - widthImg) / 2), + # max(0, (heightDev - heightImg) / 2) + # ) + # corner1 = ( + # pastePt[0] - 1, + # pastePt[1] - 1 + # ) + # corner2 = ( + # pastePt[0] + widthImg + 1, + # pastePt[1] + heightImg + 1 + # ) + # imageBg = Image.new(self.image.mode, self.size, background) + # imageBg.paste(self.image, pastePt) + # draw = ImageDraw.Draw(imageBg) + # draw.rectangle([corner1, corner2], outline=foreground) + # self.image = imageBg def cutPageNumber(self): widthImg, heightImg = self.image.size @@ -401,37 +401,37 @@ class ComicPage: # print "New size: %sx%s"%(self.image.size[0],self.image.size[1]) return self.image - def addProgressbar(self, file_number, files_totalnumber, size, howoften): - if file_number // howoften != float(file_number) / howoften: - return self.image - white = (255, 255, 255) - black = (0, 0, 0) - widthDev, heightDev = size - widthImg, heightImg = self.image.size - pastePt = ( - max(0, (widthDev - widthImg) / 2), - max(0, (heightDev - heightImg) / 2) - ) - imageBg = Image.new('RGB', size, white) - imageBg.paste(self.image, pastePt) - self.image = imageBg - widthImg, heightImg = self.image.size - draw = ImageDraw.Draw(self.image) - #Black rectangle - draw.rectangle([(0, heightImg - 3), (widthImg, heightImg)], outline=black, fill=black) - #White rectangle - draw.rectangle([(widthImg * file_number / files_totalnumber, heightImg - 3), (widthImg - 1, heightImg)], - outline=black, fill=white) - #Making notches - for i in range(1, 10): - if i <= (10 * file_number / files_totalnumber): - notch_colour = white # White - else: - notch_colour = black # Black - draw.line([(widthImg * float(i) / 10, heightImg - 3), (widthImg * float(i) / 10, heightImg)], - fill=notch_colour) - #The 50% - if i == 5: - draw.rectangle([(widthImg / 2 - 1, heightImg - 5), (widthImg / 2 + 1, heightImg)], - outline=black, fill=notch_colour) - return self.image + # def addProgressbar(self, file_number, files_totalnumber, size, howoften): + # if file_number // howoften != float(file_number) / howoften: + # return self.image + # white = (255, 255, 255) + # black = (0, 0, 0) + # widthDev, heightDev = size + # widthImg, heightImg = self.image.size + # pastePt = ( + # max(0, (widthDev - widthImg) / 2), + # max(0, (heightDev - heightImg) / 2) + # ) + # imageBg = Image.new('RGB', size, white) + # imageBg.paste(self.image, pastePt) + # self.image = imageBg + # widthImg, heightImg = self.image.size + # draw = ImageDraw.Draw(self.image) + # Black rectangle + # draw.rectangle([(0, heightImg - 3), (widthImg, heightImg)], outline=black, fill=black) + # White rectangle + # draw.rectangle([(widthImg * file_number / files_totalnumber, heightImg - 3), (widthImg - 1, heightImg)], + # outline=black, fill=white) + # Making notches + # for i in range(1, 10): + # if i <= (10 * file_number / files_totalnumber): + # notch_colour = white # White + # else: + # notch_colour = black # Black + # draw.line([(widthImg * float(i) / 10, heightImg - 3), (widthImg * float(i) / 10, heightImg)], + # fill=notch_colour) + # The 50% + # if i == 5: + # draw.rectangle([(widthImg / 2 - 1, heightImg - 5), (widthImg / 2 + 1, heightImg)], + # outline=black, fill=notch_colour) + # return self.image 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 08/22] 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): From 07bf41ea6c3fd299afdd2f9d1c3fad7a9dd1853a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Tue, 12 Mar 2013 19:56:19 +0100 Subject: [PATCH 09/22] Made commands little shorter --- kcc/comic2ebook.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index d0d9b1c..24a8995 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -461,9 +461,9 @@ def main(argv=None): help="Comic title [Default=filename]") parser.add_option("-m", "--manga-style", action="store_true", dest="righttoleft", default=False, help="Manga style (Right-to-left reading and splitting) [Default=False]") - parser.add_option("--fakepanelview", action="store_true", dest="fakepanelview", default=False, + parser.add_option("--panelview", action="store_true", dest="fakepanelview", default=False, help="Emulate Panel View feature (For Kindle 4 NT or older) [Default=False]") - parser.add_option("--fakepanelviewlandscape", action="store_true", dest="fakepanelviewlandscape", default=False, + parser.add_option("--panelviewlandscape", action="store_true", dest="fakepanelviewlandscape", default=False, help="Emulate Panel View feature - Landscape mode (For Kindle 4 NT or older) [Default=False]") parser.add_option("--noprocessing", action="store_false", dest="imgproc", default=True, help="Do not apply image preprocessing (Page splitting and optimizations) [Default=True]") From 19dc5cc6f2bcde233693ef41bee57cee6f316807 Mon Sep 17 00:00:00 2001 From: Ciro Mattia Gonano Date: Wed, 13 Mar 2013 00:21:09 +0100 Subject: [PATCH 10/22] Merge branch 'master' into kcc-fakepanelview Apply PEP-8 codestyle --- kcc/comic2ebook.py | 23 ++++++++++++----------- kcc/image.py | 17 ++++++++--------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index cac4854..8af411c 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -163,18 +163,15 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False): ]) if options.landscapemode: f.writelines(["\n", - "\n" - ]) + "\n"]) else: f.writelines(["\n", - "\n" - ]) + "\n"]) f.writelines(["\n", "\n", "\n", "\n\n\n" - ]) + "media-type=\"application/x-dtbncx+xml\"/>\n"]) if cover is not None: filename = getImageFileName(cover.replace(os.path.join(dstdir, 'OEBPS'), '').lstrip('/').lstrip('\\\\')) if '.png' == filename[1]: @@ -207,7 +204,8 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False): splitCountUsed = 1 for entry in reflist: if entry.endswith("-1"): - if ((righttoleft and facing == 'left') or (not righttoleft and facing == 'right')) and options.landscapemode: + if ((righttoleft and facing == 'left') or (not righttoleft and facing == 'right')) and\ + options.landscapemode: f.write("\n") splitCountUsed += 1 if options.landscapemode: @@ -227,7 +225,7 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False): if options.landscapemode: f.write("\n") else: - f.write("\n") + f.write("\n") if facing == 'right': facing = 'left' else: @@ -317,8 +315,10 @@ def dirImgProcess(path): if options.fakepanelview or options.fakepanelviewlandscape: img0 = image.ComicPage(split[0], options.profile) img1 = image.ComicPage(split[1], options.profile) - splitA = img0.splitPageFakePanelView(dirpath, options.righttoleft, options.fakepanelviewlandscape) - splitB = img1.splitPageFakePanelView(dirpath, options.righttoleft, options.fakepanelviewlandscape) + 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) @@ -326,7 +326,7 @@ def dirImgProcess(path): for img in splitB: tempImg = image.ComicPage(img, options.profile) applyImgOptimization(tempImg) - tempImg.saveToDir(dirpath, options.notquantize) + tempImg.saveToDir(dirpath, options.notquantize) else: img0 = image.ComicPage(split[0], options.profile) applyImgOptimization(img0, True, toRight1) @@ -522,6 +522,7 @@ def main(argv=None): rmtree(path) return epubpath + def checkOptions(): global options if options.profile == 'K4' or options.profile == 'KHD': diff --git a/kcc/image.py b/kcc/image.py index a555c76..89be1cc 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -141,7 +141,8 @@ class ComicPage: palImg.putpalette(self.palette) self.image = self.image.quantize(palette=palImg) - def resizeImage(self, upscale=False, stretch=False, black_borders=False, fakePanelViewLandscape=False, isSplit=False, toRight=False, landscapeMode=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' @@ -222,12 +223,11 @@ class ComicPage: else: return None - def splitPageFakePanelView(self, targetdir, righttoleft=False, fakePanelWiewLandscape=False): width, height = self.image.size if fakePanelWiewLandscape: - topbox = (0, 0, width, ((height / 2) + (height/9))) - bottombox = (0, ((height / 2) - (height/9)), width, height) + 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] @@ -241,10 +241,10 @@ class ComicPage: 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) + 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] @@ -274,7 +274,6 @@ class ComicPage: raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, e)) return file0, file1, file2, file3, file4 - # def frameImage(self): # foreground = tuple(self.palette[:3]) # background = tuple(self.palette[-3:]) From 6d704b382d064161a013b6c14fb4499ef3ba4de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Wed, 13 Mar 2013 21:10:50 +0100 Subject: [PATCH 11/22] Fixed merge bug --- kcc/comic2ebook.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 46180ed..240e0a8 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -270,7 +270,8 @@ def applyImgOptimization(img, isSplit=False, toRight=False): img.cropWhiteSpace(10.0) if options.cutpagenumbers: img.cutPageNumber() - img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight) + img.resizeImage(options.upscale, options.stretch, options.black_borders, options.fakepanelviewlandscape, isSplit, + toRight, options.landscapemode) img.optimizeImage(options.gamma) if not options.notquantize: img.quantizeImage() From cec9356fc22b9a2860e083e1364db2b63db709a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sat, 16 Mar 2013 10:10:19 +0100 Subject: [PATCH 12/22] Code cleaning --- kcc/comic2ebook.py | 48 +++---------- kcc/image.py | 169 ++++++++++++++++----------------------------- 2 files changed, 69 insertions(+), 148 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 240e0a8..80a3d5b 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -270,8 +270,7 @@ def applyImgOptimization(img, isSplit=False, toRight=False): img.cropWhiteSpace(10.0) if options.cutpagenumbers: img.cutPageNumber() - img.resizeImage(options.upscale, options.stretch, options.black_borders, options.fakepanelviewlandscape, isSplit, - toRight, options.landscapemode) + img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight, options.landscapemode) img.optimizeImage(options.gamma) if not options.notquantize: img.quantizeImage() @@ -313,42 +312,19 @@ def dirImgProcess(path): if facing == "right": splitCount += 1 facing = "left" - if options.fakepanelview or options.fakepanelviewlandscape: - img0 = image.ComicPage(split[0], options.profile) - img1 = image.ComicPage(split[1], options.profile) - 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) - img0.saveToDir(dirpath, options.notquantize) - img1 = image.ComicPage(split[1], options.profile) - applyImgOptimization(img1, True, toRight2) - img1.saveToDir(dirpath, options.notquantize) + img0 = image.ComicPage(split[0], options.profile) + applyImgOptimization(img0, True, toRight1) + img0.saveToDir(dirpath, options.notquantize) + img1 = image.ComicPage(split[1], options.profile) + applyImgOptimization(img1, True, toRight2) + img1.saveToDir(dirpath, options.notquantize) else: if facing == "right": facing = "left" else: facing = "right" - 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) + applyImgOptimization(img) + img.saveToDir(dirpath, options.notquantize) def genEpubStruct(path): @@ -464,10 +440,8 @@ def main(argv=None): help="Comic title [Default=filename]") parser.add_option("-m", "--manga-style", action="store_true", dest="righttoleft", default=False, help="Manga style (Right-to-left reading and splitting) [Default=False]") - parser.add_option("--panelview", action="store_true", dest="fakepanelview", default=False, - help="Emulate Panel View feature (For Kindle 4 NT or older) [Default=False]") - parser.add_option("--panelviewlandscape", action="store_true", dest="fakepanelviewlandscape", default=False, - help="Emulate Panel View feature - Landscape mode (For Kindle 4 NT or older) [Default=False]") + parser.add_option("--panelview", action="store_true", dest="panelview", default=False, + help="Add Panel View support (For Kindle Classic or older) [Default=False]") parser.add_option("--noprocessing", action="store_false", dest="imgproc", default=True, help="Do not apply image preprocessing (Page splitting and optimizations) [Default=True]") parser.add_option("--nodithering", action="store_true", dest="notquantize", default=False, diff --git a/kcc/image.py b/kcc/image.py index 89be1cc..a7d1407 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -141,15 +141,13 @@ class ComicPage: palImg.putpalette(self.palette) self.image = self.image.quantize(palette=palImg) - def resizeImage(self, upscale=False, stretch=False, black_borders=False, fakePanelViewLandscape=False, - isSplit=False, toRight=False, landscapeMode=False): + def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, toRight=False, + landscapeMode=False): method = Image.ANTIALIAS if black_borders: fill = 'black' else: fill = 'white' - 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: if isSplit and landscapeMode: @@ -223,80 +221,6 @@ class ComicPage: else: return None - def splitPageFakePanelView(self, targetdir, righttoleft=False, fakePanelWiewLandscape=False): - width, height = self.image.size - 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 frameImage(self): - # foreground = tuple(self.palette[:3]) - # background = tuple(self.palette[-3:]) - # widthDev, heightDev = self.size - # widthImg, heightImg = self.image.size - # pastePt = ( - # max(0, (widthDev - widthImg) / 2), - # max(0, (heightDev - heightImg) / 2) - # ) - # corner1 = ( - # pastePt[0] - 1, - # pastePt[1] - 1 - # ) - # corner2 = ( - # pastePt[0] + widthImg + 1, - # pastePt[1] + heightImg + 1 - # ) - # imageBg = Image.new(self.image.mode, self.size, background) - # imageBg.paste(self.image, pastePt) - # draw = ImageDraw.Draw(imageBg) - # draw.rectangle([corner1, corner2], outline=foreground) - # self.image = imageBg - def cutPageNumber(self): widthImg, heightImg = self.image.size delta = 2 @@ -390,36 +314,59 @@ class ComicPage: return self.image # def addProgressbar(self, file_number, files_totalnumber, size, howoften): - # if file_number // howoften != float(file_number) / howoften: - # return self.image - # white = (255, 255, 255) - # black = (0, 0, 0) - # widthDev, heightDev = size - # widthImg, heightImg = self.image.size - # pastePt = ( - # max(0, (widthDev - widthImg) / 2), - # max(0, (heightDev - heightImg) / 2) - # ) - # imageBg = Image.new('RGB', size, white) - # imageBg.paste(self.image, pastePt) - # self.image = imageBg - # widthImg, heightImg = self.image.size - # draw = ImageDraw.Draw(self.image) - # Black rectangle - # draw.rectangle([(0, heightImg - 3), (widthImg, heightImg)], outline=black, fill=black) - # White rectangle - # draw.rectangle([(widthImg * file_number / files_totalnumber, heightImg - 3), (widthImg - 1, heightImg)], - # outline=black, fill=white) - # Making notches - # for i in range(1, 10): - # if i <= (10 * file_number / files_totalnumber): - # notch_colour = white # White - # else: - # notch_colour = black # Black - # draw.line([(widthImg * float(i) / 10, heightImg - 3), (widthImg * float(i) / 10, heightImg)], - # fill=notch_colour) - # The 50% - # if i == 5: - # draw.rectangle([(widthImg / 2 - 1, heightImg - 5), (widthImg / 2 + 1, heightImg)], - # outline=black, fill=notch_colour) - # return self.image + # if file_number // howoften != float(file_number) / howoften: + # return self.image + # white = (255, 255, 255) + # black = (0, 0, 0) + # widthDev, heightDev = size + # widthImg, heightImg = self.image.size + # pastePt = ( + # max(0, (widthDev - widthImg) / 2), + # max(0, (heightDev - heightImg) / 2) + # ) + # imageBg = Image.new('RGB', size, white) + # imageBg.paste(self.image, pastePt) + # self.image = imageBg + # widthImg, heightImg = self.image.size + # draw = ImageDraw.Draw(self.image) + # #Black rectangle + # draw.rectangle([(0, heightImg - 3), (widthImg, heightImg)], outline=black, fill=black) + # #White rectangle + # draw.rectangle([(widthImg * file_number / files_totalnumber, heightImg - 3), (widthImg - 1, heightImg)], + # outline=black, fill=white) + # #Making notches + # for i in range(1, 10): + # if i <= (10 * file_number / files_totalnumber): + # notch_colour = white # White + # else: + # notch_colour = black # Black + # draw.line([(widthImg * float(i) / 10, heightImg - 3), (widthImg * float(i) / 10, heightImg)], + # fill=notch_colour) + # #The 50% + # if i == 5: + # draw.rectangle([(widthImg / 2 - 1, heightImg - 5), (widthImg / 2 + 1, heightImg)], + # outline=black, fill=notch_colour) + # return self.image + # + # def frameImage(self): + # foreground = tuple(self.palette[:3]) + # background = tuple(self.palette[-3:]) + # widthDev, heightDev = self.size + # widthImg, heightImg = self.image.size + # pastePt = ( + # max(0, (widthDev - widthImg) / 2), + # max(0, (heightDev - heightImg) / 2) + # ) + # corner1 = ( + # pastePt[0] - 1, + # pastePt[1] - 1 + # ) + # corner2 = ( + # pastePt[0] + widthImg + 1, + # pastePt[1] + heightImg + 1 + # ) + # imageBg = Image.new(self.image.mode, self.size, background) + # imageBg.paste(self.image, pastePt) + # draw = ImageDraw.Draw(imageBg) + # draw.rectangle([corner1, corner2], outline=foreground) + # self.image = imageBg From e7f49f8330fa110bb1ef1dfa325118f26dc35663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sat, 16 Mar 2013 10:19:57 +0100 Subject: [PATCH 13/22] New profiles Kindle Touch support Virtual Panel View. Kindle Classic/NT is not. --- kcc/comic2ebook.py | 2 +- kcc/image.py | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 80a3d5b..da8e3b5 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -441,7 +441,7 @@ def main(argv=None): parser.add_option("-m", "--manga-style", action="store_true", dest="righttoleft", default=False, help="Manga style (Right-to-left reading and splitting) [Default=False]") parser.add_option("--panelview", action="store_true", dest="panelview", default=False, - help="Add Panel View support (For Kindle Classic or older) [Default=False]") + help="Add Panel View support (For Kindle Classic and Kindle Keyboard) [Default=False]") parser.add_option("--noprocessing", action="store_false", dest="imgproc", default=True, help="Do not apply image preprocessing (Page splitting and optimizations) [Default=True]") parser.add_option("--nodithering", action="store_true", dest="notquantize", default=False, diff --git a/kcc/image.py b/kcc/image.py index a7d1407..ac5dc79 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -77,20 +77,22 @@ class ProfileData: ] Profiles = { - 'K1': ("Kindle", (600, 800), Palette4, 1.8), - 'K2': ("Kindle 2", (600, 800), Palette15, 1.8), - 'K3': ("Kindle 3/Keyboard", (600, 800), Palette16, 1.8), - 'K4': ("Kindle 4/NT/Touch", (600, 800), Palette16, 1.8), - 'KHD': ("Kindle Paperwhite", (758, 1024), Palette16, 1.8), - 'KDX': ("Kindle DX", (824, 1200), Palette15, 1.8), - 'KDXG': ("Kindle DXG", (824, 1200), Palette16, 1.8) + 'K1': ("Kindle", (600, 800), Palette4, 1.8, (900, 1200)), + 'K2': ("Kindle 2", (600, 800), Palette15, 1.8, (900, 1200)), + 'K3': ("Kindle Keyboard", (600, 800), Palette16, 1.8, (900, 1200)), + 'K4NT': ("Kindle Classic", (600, 800), Palette16, 1.8, (900, 1200)), + 'K4T': ("Kindle Touch", (600, 800), Palette16, 1.8, (900, 1200)), + 'KHD': ("Kindle Paperwhite", (758, 1024), Palette16, 1.8, (1137, 1536)), + 'KDX': ("Kindle DX", (824, 1200), Palette15, 1.8, (1236, 1800)), + 'KDXG': ("Kindle DXG", (824, 1200), Palette16, 1.8, (1236, 1800)) } ProfileLabels = { "Kindle": 'K1', "Kindle 2": 'K2', - "Kindle 3/Keyboard": 'K3', - "Kindle 4/NT/Touch": 'K4', + "Kindle Keyboard": 'K3', + "Kindle Classic": 'K4NT', + "Kindle Touch": 'K4T', "Kindle Paperwhite": 'KHD', "Kindle DX": 'KDX', "Kindle DXG": 'KDXG' @@ -101,7 +103,7 @@ class ComicPage: def __init__(self, source, device): try: self.profile = device - self.profile_label, self.size, self.palette, self.gamma = ProfileData.Profiles[device] + self.profile_label, self.size, self.palette, self.gamma, self.panelviewsize = ProfileData.Profiles[device] except KeyError: raise RuntimeError('Unexpected output device %s' % device) try: From f55bb6dce6dacd2254974f959de4b29ff692ef8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sat, 16 Mar 2013 13:33:13 +0100 Subject: [PATCH 14/22] Code cleaning --- kcc/comic2ebook.py | 20 +++++++------------- kcc/image.py | 8 ++++---- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index da8e3b5..77fc85d 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -131,7 +131,7 @@ def buildNCX(dstdir, title, chapters): def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False): opffile = os.path.join(dstdir, 'OEBPS', 'content.opf') # read the first file resolution - profilelabel, deviceres, palette, gamma = image.ProfileData.Profiles[profile] + profilelabel, deviceres, palette, gamma, panelviewsize = image.ProfileData.Profiles[profile] imgres = str(deviceres[0]) + "x" + str(deviceres[1]) if righttoleft: writingmode = "horizontal-rl" @@ -435,13 +435,13 @@ def main(argv=None): usage = "Usage: %prog [options] comic_file|comic_folder" parser = OptionParser(usage=usage, version=__version__) parser.add_option("-p", "--profile", action="store", dest="profile", default="KHD", - help="Device profile (Choose one among K1, K2, K3, K4, KDX, KDXG or KHD) [Default=KHD]") + help="Device profile (Choose one among K1, K2, K3, K4NT, K4T, KDX, KDXG or KHD) [Default=KHD]") parser.add_option("-t", "--title", action="store", dest="title", default="defaulttitle", help="Comic title [Default=filename]") parser.add_option("-m", "--manga-style", action="store_true", dest="righttoleft", default=False, help="Manga style (Right-to-left reading and splitting) [Default=False]") parser.add_option("--panelview", action="store_true", dest="panelview", default=False, - help="Add Panel View support (For Kindle Classic and Kindle Keyboard) [Default=False]") + help="Add Panel View support (For Kindle Non-Touch and Kindle Keyboard) [Default=False]") parser.add_option("--noprocessing", action="store_false", dest="imgproc", default=True, help="Do not apply image preprocessing (Page splitting and optimizations) [Default=True]") parser.add_option("--nodithering", action="store_true", dest="notquantize", default=False, @@ -500,22 +500,16 @@ def main(argv=None): def checkOptions(): global options - if options.profile == 'K4' or options.profile == 'KHD': + if options.profile == 'K4NT' or options.profile == 'K4T' or options.profile == 'KHD': options.landscapemode = True else: options.landscapemode = False - if (options.fakepanelview or options.fakepanelviewlandscape) and options.profile == 'KHD': - options.fakepanelview = False - options.fakepanelviewlandscape = False - if (options.fakepanelview or options.fakepanelviewlandscape) and options.landscapemode: + if options.panelview and not (options.profile == 'K3' or options.profile == 'K4NT'): + options.panelview = False + if options.panelview: 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: - options.fakepanelviewlandscape = False def getEpubPath(): diff --git a/kcc/image.py b/kcc/image.py index ac5dc79..6fd3b17 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -77,10 +77,10 @@ class ProfileData: ] Profiles = { - 'K1': ("Kindle", (600, 800), Palette4, 1.8, (900, 1200)), + 'K1': ("Kindle 1", (600, 800), Palette4, 1.8, (900, 1200)), 'K2': ("Kindle 2", (600, 800), Palette15, 1.8, (900, 1200)), 'K3': ("Kindle Keyboard", (600, 800), Palette16, 1.8, (900, 1200)), - 'K4NT': ("Kindle Classic", (600, 800), Palette16, 1.8, (900, 1200)), + 'K4NT': ("Kindle Non-Touch", (600, 800), Palette16, 1.8, (900, 1200)), 'K4T': ("Kindle Touch", (600, 800), Palette16, 1.8, (900, 1200)), 'KHD': ("Kindle Paperwhite", (758, 1024), Palette16, 1.8, (1137, 1536)), 'KDX': ("Kindle DX", (824, 1200), Palette15, 1.8, (1236, 1800)), @@ -88,10 +88,10 @@ class ProfileData: } ProfileLabels = { - "Kindle": 'K1', + "Kindle 1": 'K1', "Kindle 2": 'K2', "Kindle Keyboard": 'K3', - "Kindle Classic": 'K4NT', + "Kindle Non-Touch": 'K4NT', "Kindle Touch": 'K4T', "Kindle Paperwhite": 'KHD', "Kindle DX": 'KDX', From f195a4ccbf30c84daf744f37b425b019c577aa96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sat, 16 Mar 2013 19:51:23 +0100 Subject: [PATCH 15/22] Preliminary Panel View support for K3/KNT --- kcc/comic2ebook.py | 225 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 170 insertions(+), 55 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 77fc85d..da8e5fc 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -61,26 +61,48 @@ def buildHTML(path, imgfile): "", filename[0], "\n", "\n", "\n", - "\n", + "style.css\" type=\"text/css\" rel=\"stylesheet\"/>\n", "\n", - "\n", - "
\"",
\n", - #"
\n", - #"\n", - #"
\n", - #"
\n", - #"
\n", - #"
\n", - #"
\n", - #"\"",\n", - #"
\n", - "\n", - "" + "\n", + "
\n", + "
\"",
\n" ]) + if options.panelview: + if options.righttoleft: + f.writelines(["
\n", + "
\n", + "
\n", + "
\n" + ]) + else: + f.writelines(["
\n", + "
\n", + "
\n", + "
\n" + ]) + f.writelines(["
\"",
\n", + "
\"",
\n", + "
\"",
\n", + "
\"",
\n" + ]) + f.writelines(["
\n\n"]) f.close() return path, imgfile @@ -128,12 +150,12 @@ def buildNCX(dstdir, title, chapters): return -def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False): +def buildOPF(profile, dstdir, title, filelist, cover=None): opffile = os.path.join(dstdir, 'OEBPS', 'content.opf') # read the first file resolution profilelabel, deviceres, palette, gamma, panelviewsize = image.ProfileData.Profiles[profile] imgres = str(deviceres[0]) + "x" + str(deviceres[1]) - if righttoleft: + if options.righttoleft: writingmode = "horizontal-rl" facing = "right" facing1 = "right" @@ -204,7 +226,7 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False): splitCountUsed = 1 for entry in reflist: if entry.endswith("-1"): - if ((righttoleft and facing == 'left') or (not righttoleft and facing == 'right')) and\ + if ((options.righttoleft and facing == 'left') or (not options.righttoleft and facing == 'right')) and\ options.landscapemode: f.write("\n") splitCountUsed += 1 @@ -217,7 +239,7 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False): f.write("\n") else: f.write("\n") - if righttoleft: + if options.righttoleft: facing = "right" else: facing = "left" @@ -332,34 +354,127 @@ def genEpubStruct(path): filelist = [] chapterlist = [] cover = None + _, deviceres, _, _, panelviewsize = image.ProfileData.Profiles[options.profile] os.mkdir(os.path.join(path, 'OEBPS', 'Text')) - f = open(os.path.join(path, 'OEBPS', 'Text', 'page_styles.css'), 'w') + f = open(os.path.join(path, 'OEBPS', 'Text', 'style.css'), 'w') + #DON'T COMPRESS CSS. KINDLE WILL FAIL TO PARSE IT. + #Generic Panel View support + Margins fix for Non-Kindle devices. f.writelines(["@page {\n", - " margin-bottom: 0;\n", - " margin-top: 0\n", - "}\n"]) - f.close() - f = open(os.path.join(path, 'OEBPS', 'Text', 'stylesheet.css'), 'w') - f.writelines([".kcc {\n", - " display: block;\n", - " margin-bottom: 0;\n", - " margin-left: 0;\n", - " margin-right: 0;\n", - " margin-top: 0;\n", - " padding-bottom: 0;\n", - " padding-left: 0;\n", - " padding-right: 0;\n", - " padding-top: 0;\n", - " text-align: left\n", + "margin-bottom: 0;\n", + "margin-top: 0\n", "}\n", - ".kcc1 {\n", - " display: block;\n", - " text-align: center\n", + "body {\n", + "display: block;\n", + "margin-bottom: 0;\n", + "margin-left: 0;\n", + "margin-right: 0;\n", + "margin-top: 0;\n", + "padding-bottom: 0;\n", + "padding-left: 0;\n", + "padding-right: 0;\n", + "padding-top: 0;\n", + "text-align: left\n", "}\n", - ".kcc2 {\n", - " height: auto;\n", - " width: auto\n", - "}\n"]) + "div.fs {\n", + "height: ", str(deviceres[1]), "px;\n", + "width: ", str(deviceres[0]), "px;\n", + "position: relative;\n", + "display: block;\n", + "text-align: center\n", + "}\n", + "div.fs a {\n", + "display: block;\n", + "width : 100%;\n", + "height: 100%;\n", + "}\n", + "div.fs div {\n", + "position: absolute;\n", + "}\n", + "img.singlePage {\n", + "position: absolute;\n", + "height: ", str(deviceres[1]), "px;\n", + "width: ", str(deviceres[0]), "px;\n", + "}\n", + "div.target-mag-parent {\n", + "width:100%;\n", + "height:100%;\n", + "display:none;\n", + "}\n", + "div.target-mag {\n", + "position: absolute;\n", + "display: block;\n", + "overflow: hidden;\n", + "}\n", + "div.target-mag img {\n", + "position: absolute;\n", + "height: ", str(panelviewsize[1]), "px;\n", + "width: ", str(panelviewsize[0]), "px;\n", + "}\n", + "#BoxTL {\n", + "top: 0;\n", + "left: 0;\n", + "height: 50%;\n", + "width: 50%;\n", + "}\n", + "#BoxTR {\n", + "top: 0;\n", + "right: 0;\n", + "height: 50%;\n", + "width: 50%;\n", + "}\n", + "#BoxBL {\n", + "bottom: 0;\n", + "left: 0;\n", + "height: 50%;\n", + "width: 50%;\n", + "}\n", + "#BoxBR {\n", + "bottom: 0;\n", + "right: 0;\n", + "height: 50%;\n", + "width: 50%;\n", + "}\n", + "#BoxTL-Panel {\n", + "top: 0;\n", + "left: 0;\n", + "height: 100%;\n", + "width: 100%;\n", + "}\n", + "#BoxTL-Panel img {\n", + "top: 0%;\n", + "left: 0%;\n", + "}\n", + "#BoxTR-Panel {\n", + "top: 0;\n", + "right: 0;\n", + "height: 100%;\n", + "width: 100%;\n", + "}\n", + "#BoxTR-Panel img {\n", + "top: 0%;\n", + "right: 0%;\n", + "}\n", + "#BoxBL-Panel {\n", + "bottom: 0;\n", + "left: 0;\n", + "height: 100%;\n", + "width: 100%;\n", + "}\n", + "#BoxBL-Panel img {\n", + "bottom: 0%;\n", + "left: 0%;\n", + "}\n", + "#BoxBR-Panel {\n", + "bottom: 0;\n", + "right: 0;\n", + "height: 100%;\n", + "width: 100%;\n", + "}\n", + "#BoxBR-Panel img {\n", + "bottom: 0%;\n", + "right: 0%;\n", + "}" + ]) f.close() for (dirpath, dirnames, filenames) in os.walk(os.path.join(path, 'OEBPS', 'Images')): chapter = False @@ -385,7 +500,7 @@ def genEpubStruct(path): convert = lambda text: int(text) if text.isdigit() else text alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)] filelist.sort(key=lambda name: (alphanum_key(name[0].lower()), alphanum_key(name[1].lower()))) - buildOPF(options.profile, path, options.title, filelist, cover, options.righttoleft) + buildOPF(options.profile, path, options.title, filelist, cover) if options.landscapemode and splitCount > 0: filelist.append(buildBlankHTML(os.path.join(path, 'OEBPS', 'Text'))) @@ -440,8 +555,8 @@ def main(argv=None): help="Comic title [Default=filename]") parser.add_option("-m", "--manga-style", action="store_true", dest="righttoleft", default=False, help="Manga style (Right-to-left reading and splitting) [Default=False]") - parser.add_option("--panelview", action="store_true", dest="panelview", default=False, - help="Add Panel View support (For Kindle Non-Touch and Kindle Keyboard) [Default=False]") + parser.add_option("--panelviewhq", action="store_true", dest="panelviewhq", default=False, + help="Enable high quality Panel View [Default=False]") parser.add_option("--noprocessing", action="store_false", dest="imgproc", default=True, help="Do not apply image preprocessing (Page splitting and optimizations) [Default=True]") parser.add_option("--nodithering", action="store_true", dest="notquantize", default=False, @@ -500,16 +615,16 @@ def main(argv=None): def checkOptions(): global options - if options.profile == 'K4NT' or options.profile == 'K4T' or options.profile == 'KHD': + if options.profile == 'K4T' or options.profile == 'KHD': options.landscapemode = True else: options.landscapemode = False - if options.panelview and not (options.profile == 'K3' or options.profile == 'K4NT'): + if options.profile == 'K3' or options.profile == 'K4NT': + #Real Panel View + options.panelview = True + else: + #Virtual Panel View options.panelview = False - if options.panelview: - options.landscapemode = False - options.rotate = False - options.nosplitrotate = False def getEpubPath(): From c484cc8fff273f45939a7cc90880c9881b66f886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sat, 16 Mar 2013 20:05:27 +0100 Subject: [PATCH 16/22] Code inspection --- kcc/comic2ebook.py | 1 + kcc/gui.py | 1 - kcc/image.py | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index da8e5fc..369817d 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -226,6 +226,7 @@ def buildOPF(profile, dstdir, title, filelist, cover=None): splitCountUsed = 1 for entry in reflist: if entry.endswith("-1"): + # noinspection PyRedundantParentheses if ((options.righttoleft and facing == 'left') or (not options.righttoleft and facing == 'right')) and\ options.landscapemode: f.write("\n") diff --git a/kcc/gui.py b/kcc/gui.py index b9abcda..28bbdd4 100644 --- a/kcc/gui.py +++ b/kcc/gui.py @@ -28,7 +28,6 @@ import comic2ebook import kindlestrip from image import ProfileData from subprocess import call -from subprocess import check_call import os import shutil import stat diff --git a/kcc/image.py b/kcc/image.py index 6fd3b17..d7be775 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -20,7 +20,7 @@ __copyright__ = '2012-2013, Ciro Mattia Gonano ' __docformat__ = 'restructuredtext en' import os -from PIL import Image, ImageOps, ImageDraw, ImageStat +from PIL import Image, ImageOps, ImageStat class ImageFlags: From a597173b7133998dc8901f990f2a6b84ec3c084b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sat, 16 Mar 2013 21:53:23 +0100 Subject: [PATCH 17/22] Added high quality Panel View --- kcc/comic2ebook.py | 3 ++- kcc/image.py | 10 ++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 369817d..168fe2b 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -293,7 +293,8 @@ def applyImgOptimization(img, isSplit=False, toRight=False): img.cropWhiteSpace(10.0) if options.cutpagenumbers: img.cutPageNumber() - img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight, options.landscapemode) + img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight, options.landscapemode, + options.panelviewhq) img.optimizeImage(options.gamma) if not options.notquantize: img.quantizeImage() diff --git a/kcc/image.py b/kcc/image.py index d7be775..3a4f136 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -144,12 +144,16 @@ class ComicPage: self.image = self.image.quantize(palette=palImg) def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, toRight=False, - landscapeMode=False): + landscapeMode=False, panelViewHQ=False): method = Image.ANTIALIAS if black_borders: fill = 'black' else: fill = 'white' + if panelViewHQ: + size = (self.panelviewsize[0], self.panelviewsize[1]) + else: + size = (self.size[0], self.size[1]) if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]: if not upscale: if isSplit and landscapeMode: @@ -169,11 +173,9 @@ class ComicPage: return self.image else: method = Image.NEAREST - if stretch: self.image = self.image.resize(self.size, method) return self.image - ratioDev = float(self.size[0]) / float(self.size[1]) if (float(self.image.size[0]) / float(self.image.size[1])) < ratioDev: diff = int(self.image.size[1] * ratioDev) - self.image.size[0] @@ -183,7 +185,7 @@ class ComicPage: elif (float(self.image.size[0]) / float(self.image.size[1])) > ratioDev: diff = int(self.image.size[0] / ratioDev) - self.image.size[1] self.image = ImageOps.expand(self.image, border=(0, diff / 2), fill=fill) - self.image = ImageOps.fit(self.image, self.size, method=method, centering=(0.5, 0.5)) + self.image = ImageOps.fit(self.image, size, method=method, centering=(0.5, 0.5)) return self.image def splitPage(self, targetdir, righttoleft=False, rotate=False): From 943431346dfc1cbb20c7f572fff6b21f57bb5661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sun, 17 Mar 2013 08:47:35 +0100 Subject: [PATCH 18/22] MAJOR Landscape Mode improvement No idea how this change will impact Non-Kindle devices but quality increase of spitted pages is amazing. --- kcc/comic2ebook.py | 14 ++++---------- kcc/image.py | 12 ++---------- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 168fe2b..7aeb440 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -289,11 +289,11 @@ def isInFilelist(filename, filelist): return seen -def applyImgOptimization(img, isSplit=False, toRight=False): +def applyImgOptimization(img, isSplit=False): img.cropWhiteSpace(10.0) if options.cutpagenumbers: img.cutPageNumber() - img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight, options.landscapemode, + img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, options.landscapemode, options.panelviewhq) img.optimizeImage(options.gamma) if not options.notquantize: @@ -322,12 +322,6 @@ def dirImgProcess(path): if split is not None: if options.verbose: print "Splitted " + afile - if options.righttoleft: - toRight1 = False - toRight2 = True - else: - toRight1 = True - toRight2 = False if options.righttoleft: if facing == "left": splitCount += 1 @@ -337,10 +331,10 @@ def dirImgProcess(path): splitCount += 1 facing = "left" img0 = image.ComicPage(split[0], options.profile) - applyImgOptimization(img0, True, toRight1) + applyImgOptimization(img0, True) img0.saveToDir(dirpath, options.notquantize) img1 = image.ComicPage(split[1], options.profile) - applyImgOptimization(img1, True, toRight2) + applyImgOptimization(img1, True) img1.saveToDir(dirpath, options.notquantize) else: if facing == "right": diff --git a/kcc/image.py b/kcc/image.py index 3a4f136..43a6932 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -143,8 +143,8 @@ 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, panelViewHQ=False): + def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, landscapeMode=False, + panelViewHQ=False): method = Image.ANTIALIAS if black_borders: fill = 'black' @@ -157,20 +157,12 @@ class ComicPage: if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]: if not upscale: if isSplit and landscapeMode: - borderw = (self.size[0] - self.image.size[0]) borderh = (self.size[1] - self.image.size[1]) / 2 self.image = ImageOps.expand(self.image, border=(0, borderh), fill=fill) - tempImg = Image.new(self.image.mode, (self.image.size[0] + borderw, self.image.size[1]), fill) - if toRight: - tempImg.paste(self.image, (borderw, 0)) - else: - tempImg.paste(self.image, (0, 0)) - self.image = tempImg else: borderw = (self.size[0] - self.image.size[0]) / 2 borderh = (self.size[1] - self.image.size[1]) / 2 self.image = ImageOps.expand(self.image, border=(borderw, borderh), fill=fill) - return self.image else: method = Image.NEAREST if stretch: From f5b515ef79be7c3b8826e16df9ebe6af07d04300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sun, 17 Mar 2013 09:37:57 +0100 Subject: [PATCH 19/22] GUI update --- kcc/comic2ebook.py | 8 ++++-- kcc/gui.py | 72 ++++++++++++++++++++++++---------------------- kcc/image.py | 12 ++++---- 3 files changed, 49 insertions(+), 43 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 7aeb440..18e455e 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -294,7 +294,7 @@ def applyImgOptimization(img, isSplit=False): if options.cutpagenumbers: img.cutPageNumber() img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, options.landscapemode, - options.panelviewhq) + options.nopanelviewhq) img.optimizeImage(options.gamma) if not options.notquantize: img.quantizeImage() @@ -551,8 +551,8 @@ def main(argv=None): help="Comic title [Default=filename]") parser.add_option("-m", "--manga-style", action="store_true", dest="righttoleft", default=False, help="Manga style (Right-to-left reading and splitting) [Default=False]") - parser.add_option("--panelviewhq", action="store_true", dest="panelviewhq", default=False, - help="Enable high quality Panel View [Default=False]") + parser.add_option("--nopanelviewhq", action="store_true", dest="nopanelviewhq", default=False, + help="Disable high quality Panel View [Default=False]") parser.add_option("--noprocessing", action="store_false", dest="imgproc", default=True, help="Do not apply image preprocessing (Page splitting and optimizations) [Default=True]") parser.add_option("--nodithering", action="store_true", dest="notquantize", default=False, @@ -621,6 +621,8 @@ def checkOptions(): else: #Virtual Panel View options.panelview = False + if options.profile == 'K1' or options.profile == 'K2' or options.profile == 'KDX' or options.profile == 'KDXG': + options.nopanelviewhq = True def getEpubPath(): diff --git a/kcc/gui.py b/kcc/gui.py index 28bbdd4..2e5978a 100644 --- a/kcc/gui.py +++ b/kcc/gui.py @@ -94,28 +94,30 @@ class MainWindow: self.options = { 'Aepub_only': IntVar(None, 0), 'Bmangastyle': IntVar(None, 0), - 'Cimage_preprocess': IntVar(None, 0), - 'Dnotquantize': IntVar(None, 0), - 'Eimage_gamma': DoubleVar(None, 0.0), - 'Fimage_upscale': IntVar(None, 0), - 'Gimage_stretch': IntVar(None, 0), - 'Hblack_borders': IntVar(None, 0), - 'Irotate': IntVar(None, 0), - 'Jnosplitrotate': IntVar(None, 0), - 'Kcut_page_numbers': IntVar(None, 0) + 'Cnopanelviewhq': IntVar(None, 0), + 'Dimage_preprocess': IntVar(None, 0), + 'Enotquantize': IntVar(None, 0), + 'Fimage_gamma': DoubleVar(None, 0.0), + 'Gimage_upscale': IntVar(None, 0), + 'Himage_stretch': IntVar(None, 0), + 'Iblack_borders': IntVar(None, 0), + 'Jrotate': IntVar(None, 0), + 'Knosplitrotate': IntVar(None, 0), + 'Lcut_page_numbers': IntVar(None, 0) } self.optionlabels = { 'Aepub_only': "Generate EPUB only", - 'Cimage_preprocess': "Disable image optimizations", - 'Dnotquantize': "Disable image quantization", - 'Jnosplitrotate': "Disable splitting and rotation", - 'Irotate': "Rotate images instead splitting them", - 'Kcut_page_numbers': "Disable page numbers cutting", 'Bmangastyle': "Manga mode", - 'Eimage_gamma': "Custom gamma correction", - 'Fimage_upscale': "Allow image upscaling", - 'Gimage_stretch': "Stretch images", - 'Hblack_borders': "Use black borders" + 'Cnopanelviewhq': "Disable high quality Panel View", + 'Dimage_preprocess': "Disable image optimizations", + 'Enotquantize': "Disable image quantization", + 'Fimage_gamma': "Custom gamma correction", + 'Gimage_upscale': "Allow image upscaling", + 'Himage_stretch': "Stretch images", + 'Iblack_borders': "Use black borders", + 'Jrotate': "Rotate images instead splitting them", + 'Knosplitrotate': "Disable splitting and rotation", + 'Lcut_page_numbers': "Disable page numbers cutting" } self.optionsButtons = {} for key in sorted(self.options): @@ -160,27 +162,29 @@ class MainWindow: return profilekey = ProfileData.ProfileLabels[self.profile.get()] argv = ["-p", profilekey] - if self.options['Eimage_gamma'].get() != 0.0: - argv.append("--gamma") - argv.append(self.options['Eimage_gamma'].get()) - if self.options['Cimage_preprocess'].get() == 1: - argv.append("--noprocessing") - if self.options['Dnotquantize'].get() == 1: - argv.append("--nodithering") - if self.options['Jnosplitrotate'].get() == 1: - argv.append("--nosplitrotate") - if self.options['Irotate'].get() == 1: - argv.append("--rotate") - if self.options['Kcut_page_numbers'].get() == 1: - argv.append("--nocutpagenumbers") if self.options['Bmangastyle'].get() == 1: argv.append("-m") - if self.options['Fimage_upscale'].get() == 1: + if self.options['Cnopanelviewhq'].get() == 1: + argv.append("--nopanelviewhq") + if self.options['Dimage_preprocess'].get() == 1: + argv.append("--noprocessing") + if self.options['Enotquantize'].get() == 1: + argv.append("--nodithering") + if self.options['Fimage_gamma'].get() != 0.0: + argv.append("--gamma") + argv.append(self.options['Fimage_gamma'].get()) + if self.options['Gimage_upscale'].get() == 1: argv.append("--upscale") - if self.options['Gimage_stretch'].get() == 1: + if self.options['Himage_stretch'].get() == 1: argv.append("--stretch") - if self.options['Hblack_borders'].get() == 1: + if self.options['Iblack_borders'].get() == 1: argv.append("--blackborders") + if self.options['Jrotate'].get() == 1: + argv.append("--rotate") + if self.options['Knosplitrotate'].get() == 1: + argv.append("--nosplitrotate") + if self.options['Lcut_page_numbers'].get() == 1: + argv.append("--nocutpagenumbers") errors = False left_files = len(self.filelist) filenum = 0 diff --git a/kcc/image.py b/kcc/image.py index 43a6932..0e6d929 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -90,9 +90,9 @@ class ProfileData: ProfileLabels = { "Kindle 1": 'K1', "Kindle 2": 'K2', - "Kindle Keyboard": 'K3', - "Kindle Non-Touch": 'K4NT', - "Kindle Touch": 'K4T', + "Kindle 3/Keyboard": 'K3', + "Kindle 4/Non-Touch": 'K4NT', + "Kindle 4/Touch": 'K4T', "Kindle Paperwhite": 'KHD', "Kindle DX": 'KDX', "Kindle DXG": 'KDXG' @@ -144,13 +144,13 @@ class ComicPage: self.image = self.image.quantize(palette=palImg) def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, landscapeMode=False, - panelViewHQ=False): + noPanelViewHQ=False): method = Image.ANTIALIAS if black_borders: fill = 'black' else: fill = 'white' - if panelViewHQ: + if not noPanelViewHQ: size = (self.panelviewsize[0], self.panelviewsize[1]) else: size = (self.size[0], self.size[1]) @@ -164,7 +164,7 @@ class ComicPage: borderh = (self.size[1] - self.image.size[1]) / 2 self.image = ImageOps.expand(self.image, border=(borderw, borderh), fill=fill) else: - method = Image.NEAREST + method = Image.BILINEAR if stretch: self.image = self.image.resize(self.size, method) return self.image From eb7d56c1b96afcbc013693cc0ae83e93e219bdf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sun, 17 Mar 2013 10:45:46 +0100 Subject: [PATCH 20/22] Force upscaling for spitted pages Only when landscape mode is on. Without it smaller splitted pages looks really bad on PW. --- kcc/image.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kcc/image.py b/kcc/image.py index 0e6d929..c8fc450 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -159,10 +159,12 @@ class ComicPage: if isSplit and landscapeMode: borderh = (self.size[1] - self.image.size[1]) / 2 self.image = ImageOps.expand(self.image, border=(0, borderh), fill=fill) + method = Image.BILINEAR else: borderw = (self.size[0] - self.image.size[0]) / 2 borderh = (self.size[1] - self.image.size[1]) / 2 self.image = ImageOps.expand(self.image, border=(borderw, borderh), fill=fill) + return self.image else: method = Image.BILINEAR if stretch: From 6bc80380686c23df9678e5ee630f59c008f7b82c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Sun, 17 Mar 2013 12:52:33 +0100 Subject: [PATCH 21/22] Yet another refactoring of resizeImage I hope all bugs are dead this time! --- kcc/comic2ebook.py | 13 +++++++++---- kcc/image.py | 43 ++++++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 18e455e..311a6a3 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -222,6 +222,7 @@ def buildOPF(profile, dstdir, title, filelist, cover=None): f.write("\n") splitCountUsed += 1 + f.write("\n") f.write("
\n\n") splitCountUsed = 1 for entry in reflist: @@ -289,11 +290,11 @@ def isInFilelist(filename, filelist): return seen -def applyImgOptimization(img, isSplit=False): +def applyImgOptimization(img, isSplit=False, toRight=False): img.cropWhiteSpace(10.0) if options.cutpagenumbers: img.cutPageNumber() - img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, options.landscapemode, + img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight, options.landscapemode, options.nopanelviewhq) img.optimizeImage(options.gamma) if not options.notquantize: @@ -323,18 +324,22 @@ def dirImgProcess(path): if options.verbose: print "Splitted " + afile if options.righttoleft: + toRight1 = False + toRight2 = True if facing == "left": splitCount += 1 facing = "right" else: + toRight1 = True + toRight2 = False if facing == "right": splitCount += 1 facing = "left" img0 = image.ComicPage(split[0], options.profile) - applyImgOptimization(img0, True) + applyImgOptimization(img0, True, toRight1) img0.saveToDir(dirpath, options.notquantize) img1 = image.ComicPage(split[1], options.profile) - applyImgOptimization(img1, True) + applyImgOptimization(img1, True, toRight2) img1.saveToDir(dirpath, options.notquantize) else: if facing == "right": diff --git a/kcc/image.py b/kcc/image.py index c8fc450..743c124 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -143,39 +143,44 @@ 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, landscapeMode=False, - noPanelViewHQ=False): + def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, toRight=False, + landscapeMode=False, noPanelViewHQ=False): method = Image.ANTIALIAS if black_borders: fill = 'black' else: fill = 'white' - if not noPanelViewHQ: - size = (self.panelviewsize[0], self.panelviewsize[1]) - else: + if noPanelViewHQ: size = (self.size[0], self.size[1]) + else: + size = (self.panelviewsize[0], self.panelviewsize[1]) + if isSplit and landscapeMode: + upscale = True if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]: if not upscale: - if isSplit and landscapeMode: - borderh = (self.size[1] - self.image.size[1]) / 2 - self.image = ImageOps.expand(self.image, border=(0, borderh), fill=fill) - method = Image.BILINEAR - else: - borderw = (self.size[0] - self.image.size[0]) / 2 - borderh = (self.size[1] - self.image.size[1]) / 2 - self.image = ImageOps.expand(self.image, border=(borderw, borderh), fill=fill) - return self.image + borderw = (self.size[0] - self.image.size[0]) / 2 + borderh = (self.size[1] - self.image.size[1]) / 2 + self.image = ImageOps.expand(self.image, border=(borderw, borderh), fill=fill) + return self.image else: method = Image.BILINEAR - if stretch: - self.image = self.image.resize(self.size, method) + if stretch: # if stretching call directly resize() without other considerations. + self.image = self.image.resize(size, method) return self.image ratioDev = float(self.size[0]) / float(self.size[1]) if (float(self.image.size[0]) / float(self.image.size[1])) < ratioDev: - diff = int(self.image.size[1] * ratioDev) - self.image.size[0] if isSplit and landscapeMode: - diff = 2 - self.image = ImageOps.expand(self.image, border=(diff / 2, 0), fill=fill) + diff = int(self.image.size[1] * ratioDev) - self.image.size[0] + self.image = ImageOps.expand(self.image, border=(diff / 2, 0), fill=fill) + tempImg = Image.new(self.image.mode, (self.image.size[0] + diff, self.image.size[1]), fill) + if toRight: + tempImg.paste(self.image, (diff, 0)) + else: + tempImg.paste(self.image, (0, 0)) + self.image = tempImg + else: + diff = int(self.image.size[1] * ratioDev) - self.image.size[0] + self.image = ImageOps.expand(self.image, border=(diff / 2, 0), fill=fill) elif (float(self.image.size[0]) / float(self.image.size[1])) > ratioDev: diff = int(self.image.size[0] / ratioDev) - self.image.size[1] self.image = ImageOps.expand(self.image, border=(0, diff / 2), fill=fill) From dfc03aee38dcae8ee0674fcd9ccaeb65917491c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jastrz=C4=99bski?= Date: Mon, 18 Mar 2013 14:13:53 +0100 Subject: [PATCH 22/22] Restoring more safe filesize limit --- kcc/gui.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kcc/gui.py b/kcc/gui.py index 2e5978a..bc790c1 100644 --- a/kcc/gui.py +++ b/kcc/gui.py @@ -209,10 +209,10 @@ class MainWindow: continue if self.options['Aepub_only'].get() == 0: try: - if os.path.getsize(epub_path) > 335544320: - # do not call kindlegen if source is bigger than 320MB + if os.path.getsize(epub_path) > 314572800: + # do not call kindlegen if source is bigger than 300MB tkMessageBox.showwarning('KindleGen Warning', - "ePub file %s is bigger than 320MB, not suitable for kindlegen" % + "ePub file %s is bigger than 300MB, not suitable for kindlegen" % epub_path) continue retcode = call("kindlegen \"" + epub_path + "\"", shell=True)