diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 3affd83..830fabf 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -129,34 +129,47 @@ def buildHTML(path, imgfile): "}'>\n"]) if options.quality == 2: imgfilepv = string.split(imgfile, ".") - imgfilepv[0] = imgfilepv[0].split("_kccx")[0].replace("_kccnh", "").replace("_kccnv", "") + imgfilepv[0] = imgfilepv[0].split("_kccxl")[0].replace("_kccnh", "").replace("_kccnv", "") imgfilepv[0] += "_kcchq" imgfilepv = string.join(imgfilepv, ".") else: imgfilepv = imgfile - if "_kccx" in filename[0]: - xy = string.split(filename[0], "_kccx")[1] - x = string.split(xy, "_kccy")[0].lstrip("0") - y = string.split(xy, "_kccy")[1].lstrip("0") - if x != "": - x = "-" + str(float(x)/100) + "%" + if "_kccxl" in filename[0]: + borders = filename[0].split('_kccxl')[1] + borders = re.findall('[0-9]{1,6}', borders) + xl = borders[0].lstrip("0") + yu = borders[1].lstrip("0") + xr = borders[2].lstrip("0") + yd = borders[3].lstrip("0") + if xl != "": + xl = "-" + str(xl) + "px" else: - x = "0%" - if y != "": - y = "-" + str(float(y)/100) + "%" + xl = "0px" + if xr != "": + xr = "-" + str(xr) + "px" else: - y = "0%" + xr = "0px" + if yu != "": + yu = "-" + str(yu) + "px" + else: + yu = "0px" + if yd != "": + yd = "-" + str(yd) + "px" + else: + yd = "0px" else: - x = "0%" - y = "0%" - boxStyles = {"BoxTL": "left:" + x + ";top:" + y + ";", - "BoxTR": "right:" + x + ";top:" + y + ";", - "BoxBL": "left:" + x + ";bottom:" + y + ";", - "BoxBR": "right:" + x + ";bottom:" + y + ";", - "BoxT": "left:-25%;top:" + y + ";", - "BoxB": "left:-25%;bottom:" + y + ";", - "BoxL": "left:" + x + ";top:-25%;", - "BoxR": "right:" + x + ";top:-25%;", + xl = "0px" + yu = "0px" + xr = "0px" + yd = "0px" + boxStyles = {"BoxTL": "left:" + xl + ";top:" + yu + ";", + "BoxTR": "right:" + xr + ";top:" + yu + ";", + "BoxBL": "left:" + xl + ";bottom:" + yd + ";", + "BoxBR": "right:" + xr + ";bottom:" + yd + ";", + "BoxT": "left:-25%;top:" + yu + ";", + "BoxB": "left:-25%;bottom:" + yd + ";", + "BoxL": "left:" + xl + ";top:-25%;", + "BoxR": "right:" + xr + ";top:-25%;", "BoxC": "right:-25%;top:-25%;" } for box in boxes: @@ -295,7 +308,7 @@ def getImageFileName(imgfile): return filename -def applyImgOptimization(img, opt, overrideQuality=5): +def applyImgOptimization(img, opt, hqImage=None): if not img.fill: img.getImageFill(opt.webtoon) if not opt.webtoon: @@ -303,8 +316,9 @@ def applyImgOptimization(img, opt, overrideQuality=5): if opt.cutpagenumbers and not opt.webtoon: img.cutPageNumber() img.optimizeImage(opt.gamma) - if overrideQuality != 5: - img.resizeImage(opt.upscale, opt.stretch, opt.bordersColor, overrideQuality) + if hqImage: + img.resizeImage(opt.upscale, opt.stretch, opt.bordersColor, 0) + img.calculateBorder(hqImage) else: img.resizeImage(opt.upscale, opt.stretch, opt.bordersColor, opt.quality) if opt.forcepng and not opt.forcecolor: @@ -378,12 +392,12 @@ def fileImgProcess(work): applyImgOptimization(img1, opt) img1.saveToDir(dirpath, opt.forcepng, opt.forcecolor, wipe) if opt.quality == 2: - img3 = image.ComicPage(split[0], opt.profileData, img0.fill) - applyImgOptimization(img3, opt, 0) - img3.saveToDir(dirpath, opt.forcepng, opt.forcecolor, True) - img4 = image.ComicPage(split[1], opt.profileData, img1.fill) - applyImgOptimization(img4, opt, 0) - img4.saveToDir(dirpath, opt.forcepng, opt.forcecolor, True) + img0b = image.ComicPage(split[0], opt.profileData, img0.fill) + applyImgOptimization(img0b, opt, img0) + img0b.saveToDir(dirpath, opt.forcepng, opt.forcecolor, True) + img1b = image.ComicPage(split[1], opt.profileData, img1.fill) + applyImgOptimization(img1b, opt, img1) + img1b.saveToDir(dirpath, opt.forcepng, opt.forcecolor, True) else: applyImgOptimization(img, opt) img.saveToDir(dirpath, opt.forcepng, opt.forcecolor, wipe) @@ -392,7 +406,7 @@ def fileImgProcess(work): if img.rotated: img2.image = img2.image.rotate(90) img2.rotated = True - applyImgOptimization(img2, opt, 0) + applyImgOptimization(img2, opt, img) img2.saveToDir(dirpath, opt.forcepng, opt.forcecolor, True) except StandardError: return str(sys.exc_info()[1]) diff --git a/kcc/image.py b/kcc/image.py index 3e848c3..0144556 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -201,7 +201,8 @@ class ComicPage: if self.noVPV: suffix += "_kccnv" if self.border: - suffix += "_kccx" + str(self.border[0]) + "_kccy" + str(self.border[1]) + suffix += "_kccxl" + str(self.border[0]) + "_kccyu" + str(self.border[1]) + "_kccxr" +\ + str(self.border[2]) + "_kccyd" + str(self.border[3]) if forcepng: self.image.save(os.path.join(targetdir, os.path.splitext(self.filename)[0] + suffix + ".png"), "PNG", optimize=1) @@ -232,6 +233,23 @@ class ComicPage: # Quantize is deprecated but new function call it internally anyway... self.image = self.image.quantize(palette=palImg) + def calculateBorder(self, sourceImage): + if self.fill == 'white': + border = ImageOps.invert(sourceImage.image).getbbox() + else: + border = sourceImage.image.getbbox() + if border is not None: + if border[2]-border[0] < self.size[0]: + self.noHPV = True + if border[3]-border[1] < self.size[1]: + self.noVPV = True + self.border = [border[0], border[1], + sourceImage.image.size[0] - border[2], sourceImage.image.size[1] - border[3]] + else: + self.border = [0, 0] + self.noHPV = True + self.noVPV = True + def resizeImage(self, upscale=False, stretch=False, bordersColor=None, qualityMode=0): # High-quality downscaling filter method = Image.ANTIALIAS @@ -239,28 +257,17 @@ class ComicPage: fill = bordersColor else: fill = self.fill + # Set target size if qualityMode == 0: size = (self.size[0], self.size[1]) - generateBorder = True - elif qualityMode == 1: - size = (self.panelviewsize[0], self.panelviewsize[1]) - generateBorder = True else: size = (self.panelviewsize[0], self.panelviewsize[1]) - generateBorder = False - # If image is smaller than screen and upscale is off - Just expand it - if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]: + # If image is smaller than target size and upscale is off - Just expand it by adding margins + if self.image.size[0] <= size[0] and self.image.size[1] <= size[1]: if not upscale: - borderw = (self.size[0] - self.image.size[0]) / 2 - borderh = (self.size[1] - self.image.size[1]) / 2 + borderw = (size[0] - self.image.size[0]) / 2 + borderh = (size[1] - self.image.size[1]) / 2 self.image = ImageOps.expand(self.image, border=(borderw, borderh), fill=fill) - if generateBorder: - if (self.image.size[0]-(2*borderw))*1.5 < self.size[0]: - self.noHPV = True - if (self.image.size[1]-(2*borderh))*1.5 < self.size[1]: - self.noVPV = True - self.border = [int(round(float(borderw)/float(self.image.size[0])*100, 2)*100*1.5), - int(round(float(borderh)/float(self.image.size[1])*100, 2)*100*1.5)] return self.image else: # Cubic spline interpolation in a 4x4 environment @@ -268,25 +275,9 @@ class ComicPage: # If stretching is on - Resize without other considerations if stretch: self.image = self.image.resize(size, method) - if generateBorder: - if fill == 'white': - border = ImageOps.invert(self.image).getbbox() - else: - border = self.image.getbbox() - if border is not None: - if (border[2]-border[0])*1.5 < self.size[0]: - self.noHPV = True - if (border[3]-border[1])*1.5 < self.size[1]: - self.noVPV = True - self.border = [int(round(float(border[0])/float(self.image.size[0])*100, 2)*100*1.5), - int(round(float(border[1])/float(self.image.size[1])*100, 2)*100*1.5)] - else: - self.border = [0, 0] - self.noHPV = True - self.noVPV = True return self.image # Otherwise - Upscale/Downscale - ratioDev = float(self.size[0]) / float(self.size[1]) + ratioDev = float(size[0]) / float(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] self.image = ImageOps.expand(self.image, border=(diff / 2, 0), fill=fill) @@ -294,22 +285,6 @@ class ComicPage: 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, size, method=method, centering=(0.5, 0.5)) - if generateBorder: - if fill == 'white': - border = ImageOps.invert(self.image).getbbox() - else: - border = self.image.getbbox() - if border is not None: - if (border[2]-border[0])*1.5 < self.size[0]: - self.noHPV = True - if (border[3]-border[1])*1.5 < self.size[1]: - self.noVPV = True - self.border = [int(round(float(border[0])/float(self.image.size[0])*100, 2)*100*1.5), - int(round(float(border[1])/float(self.image.size[1])*100, 2)*100*1.5)] - else: - self.border = [0, 0] - self.noHPV = True - self.noVPV = True return self.image def splitPage(self, targetdir, righttoleft=False, rotate=False):