diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 307c6c5..6f507d9 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -81,6 +81,22 @@ def buildHTML(path, imgfile): return path, imgfile +def buildBlankHTML(path): + f = open(os.path.join(path, 'blank.html'), "w") + f.writelines(["\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + ""]) + f.close() + return path + + def buildNCX(dstdir, title, chapters): ncxfile = os.path.join(dstdir, 'OEBPS', 'toc.ncx') f = open(ncxfile, "w") @@ -115,8 +131,14 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False): imgres = str(deviceres[0]) + "x" + str(deviceres[1]) if righttoleft: writingmode = "horizontal-rl" + facing = "right" + facing1 = "right" + facing2 = "left" else: writingmode = "horizontal-lr" + facing = "left" + facing1 = "left" + facing2 = "right" from uuid import uuid4 uuid = str(uuid4()) uuid = uuid.encode('utf-8') @@ -134,11 +156,11 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False): "\n", "\n", "\n", - "\n", + "\n", "\n", "\n", "\n", - "\n", + "\n", "\n\n\n" ]) @@ -165,9 +187,27 @@ 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 splittedSomething: + f.write("\n") f.write("\n\n") for entry in reflist: - f.write("\n") + if entry.endswith("-1"): + if (righttoleft and facing == 'left') or (not righttoleft and facing == 'right') and \ + (options.profile == 'K4' or options.profile == 'KHD'): + f.write("\n") + f.write("\n") + elif entry.endswith("-2"): + f.write("\n") + if righttoleft: + facing = "right" + else: + facing = "left" + 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 @@ -205,17 +245,19 @@ def isInFilelist(filename, filelist): return seen -def applyImgOptimization(img): +def applyImgOptimization(img, isSplit=False, toRight=False): img.optimizeImage() img.cropWhiteSpace(10.0) if options.cutpagenumbers: img.cutPageNumber() - img.resizeImage(options.upscale, options.stretch, options.black_borders) + img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight) img.quantizeImage() def dirImgProcess(path): global options + global splittedSomething + splittedSomething = False for (dirpath, dirnames, filenames) in os.walk(path): for afile in filenames: @@ -227,13 +269,20 @@ def dirImgProcess(path): img = image.ComicPage(os.path.join(dirpath, afile), options.profile) split = img.splitPage(dirpath, options.righttoleft, options.rotate) if split is not None: + splittedSomething = True if options.verbose: print "Splitted " + afile + if options.righttoleft: + toRight1 = False + toRight2 = True + else: + toRight1 = True + toRight2 = False img0 = image.ComicPage(split[0], options.profile) - applyImgOptimization(img0) + applyImgOptimization(img0, True, toRight1) img0.saveToDir(dirpath) img1 = image.ComicPage(split[1], options.profile) - applyImgOptimization(img1) + applyImgOptimization(img1, True, toRight2) img1.saveToDir(dirpath) else: applyImgOptimization(img) @@ -272,6 +321,8 @@ def genEpubStruct(path): 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 splittedSomething: + filelist.append(buildBlankHTML(os.path.join(path, 'OEBPS', 'Text'))) def getWorkFolder(afile): diff --git a/kcc/image.py b/kcc/image.py index 83fac9c..f3080ed 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -85,7 +85,7 @@ class ProfileData: 'KDX': ("Kindle DX", (824, 1200), Palette15), 'KDXG': ("Kindle DXG", (824, 1200), Palette16) } - + ProfileLabels = { "Kindle": 'K1', "Kindle 2": 'K2', @@ -100,6 +100,7 @@ class ProfileData: class ComicPage: def __init__(self, source, device): try: + self.profile = device self.profile_label, self.size, self.palette = ProfileData.Profiles[device] except KeyError: raise RuntimeError('Unexpected output device %s' % device) @@ -129,7 +130,7 @@ class ComicPage: palImg.putpalette(self.palette) self.image = self.image.quantize(palette=palImg) - def resizeImage(self, upscale=False, stretch=False, black_borders=False): + def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, toRight=False): method = Image.ANTIALIAS if black_borders: fill = 'black' @@ -137,10 +138,20 @@ class ComicPage: fill = 'white' if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]: if not upscale: - # do not upscale but center image in a device-sized 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) + if isSplit and (self.profile == 'K4' or self.profile == 'KHD'): + 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 @@ -152,6 +163,8 @@ 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 (self.profile == 'K4' or self.profile == 'KHD'): + 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: diff = int(self.image.size[0] / ratioDev) - self.image.size[1]