diff options
author | Paweł Jastrzębski <pawelj@vulturis.eu> | 2014-01-23 18:06:14 +0100 |
---|---|---|
committer | Paweł Jastrzębski <pawelj@vulturis.eu> | 2014-01-23 18:06:14 +0100 |
commit | 3d8bcb4020a49ec9d0157df601a19e72981b7e4b (patch) | |
tree | 26de04279ba07a75c38366ed792d92720f0ed60f | |
parent | Windows: Fixed possible problems with file locks (diff) | |
download | kcc-3d8bcb4020a49ec9d0157df601a19e72981b7e4b.tar.gz kcc-3d8bcb4020a49ec9d0157df601a19e72981b7e4b.tar.bz2 kcc-3d8bcb4020a49ec9d0157df601a19e72981b7e4b.zip |
Image flags are not part of filename anymore
-rwxr-xr-x | kcc/comic2ebook.py | 107 | ||||
-rwxr-xr-x | kcc/image.py | 41 |
2 files changed, 83 insertions, 65 deletions
diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 22c4dae..06682e7 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -45,22 +45,22 @@ from . import cbxarchive from . import pdfjpgextract -def buildHTML(path, imgfile): +def buildHTML(path, imgfile, imgfilepath): filename = getImageFileName(imgfile) if filename is not None: - if "-kccrot" in str(filename): + if "Rotated" in theGreatIndex[imgfilepath]: rotatedPage = True else: rotatedPage = False - if "-kccnpv" in str(filename): + if "NoPanelView" in theGreatIndex[imgfilepath]: noPV = True else: noPV = False - if "-kccnh" in str(filename): + if "NoHorizontalPanelView" in theGreatIndex[imgfilepath]: noHorizontalPV = True else: noHorizontalPV = False - if "-kccnv" in str(filename): + if "NoVerticalPanelView" in theGreatIndex[imgfilepath]: noVerticalPV = True else: noVerticalPV = False @@ -133,39 +133,11 @@ def buildHTML(path, imgfile): "}'></a></div>\n"]) if options.quality == 2: imgfilepv = str.split(imgfile, ".") - imgfilepv[0] = imgfilepv[0].split("-kccxl")[0].replace("-kccnh", "").replace("-kccnv", "") - imgfilepv[0] += "-kcchq" + imgfilepv[0] += "-hq" imgfilepv = ".".join(imgfilepv) else: imgfilepv = imgfile - 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(float(xl)/100) + "%" - else: - xl = "0%" - if xr != "": - xr = "-" + str(float(xr)/100) + "%" - else: - xr = "0%" - if yu != "": - yu = "-" + str(float(yu)/100) + "%" - else: - yu = "0%" - if yd != "": - yd = "-" + str(float(yd)/100) + "%" - else: - yd = "0%" - else: - xl = "0%" - yu = "0%" - xr = "0%" - yd = "0%" + xl, yu, xr, yd = checkMargins(imgfilepath) boxStyles = {"BoxTL": "left:" + xl + ";top:" + yu + ";", "BoxTR": "right:" + xr + ";top:" + yu + ";", "BoxBL": "left:" + xl + ";bottom:" + yd + ";", @@ -187,6 +159,34 @@ def buildHTML(path, imgfile): return path, imgfile +def checkMargins(path): + for flag in theGreatIndex[path]: + if "Margins-" in flag: + flag = flag.split('-') + xl = flag[1] + yu = flag[2] + xr = flag[3] + yd = flag[4] + if xl != "0": + xl = "-" + str(float(xl)/100) + "%" + else: + xl = "0%" + if xr != "0": + xr = "-" + str(float(xr)/100) + "%" + else: + xr = "0%" + if yu != "0": + yu = "-" + str(float(yu)/100) + "%" + else: + yu = "0%" + if yd != "0": + yd = "-" + str(float(yd)/100) + "%" + else: + yd = "0%" + return xl, yu, xr, yd + return '0%', '0%', '0%', '0%' + + def buildNCX(dstdir, title, chapters, chapterNames): options.uuid = str(uuid4()) ncxfile = os.path.join(dstdir, 'OEBPS', 'toc.ncx') @@ -331,10 +331,11 @@ def applyImgOptimization(img, opt, hqImage=None): def dirImgProcess(path): - global workerPool, workerOutput + global workerPool, workerOutput, theGreatIndex workerPool = Pool() workerOutput = [] work = [] + theGreatIndex = {} pagenumber = 0 for (dirpath, dirnames, filenames) in os.walk(path): for afile in filenames: @@ -360,9 +361,15 @@ def dirImgProcess(path): def fileImgProcess_tick(output): - if output: + if isinstance(output, str): workerOutput.append(output) workerPool.terminate() + else: + for page in output: + if page is not None: + if sys.platform.startswith('win'): + page[0] = page[0].replace('/', '\\') + theGreatIndex[page[0]] = page[1] if GUI: GUI.progressBarTick.emit('tick') if not GUI.conversionAlive: @@ -374,6 +381,7 @@ def fileImgProcess(work): afile = work[0] dirpath = work[1] opt = work[2] + output = [] if opt.verbose: print("Optimizing " + afile + " for " + opt.profile) else: @@ -392,26 +400,21 @@ def fileImgProcess(work): print("Splitted " + afile) img0 = image.ComicPage(split[0], opt.profileData) applyImgOptimization(img0, opt) - img0.saveToDir(dirpath, opt.forcepng, opt.forcecolor) + output.append(img0.saveToDir(dirpath, opt.forcepng, opt.forcecolor)) img1 = image.ComicPage(split[1], opt.profileData) applyImgOptimization(img1, opt) - img1.saveToDir(dirpath, opt.forcepng, opt.forcecolor) - if wipe: - os.remove(os.path.join(dirpath, img0.filename)) - os.remove(os.path.join(dirpath, img1.filename)) + output.append(img1.saveToDir(dirpath, opt.forcepng, opt.forcecolor)) if opt.quality == 2: img0b = image.ComicPage(split[0], opt.profileData, img0.fill) applyImgOptimization(img0b, opt, img0) - img0b.saveToDir(dirpath, opt.forcepng, opt.forcecolor) + output.append(img0b.saveToDir(dirpath, opt.forcepng, opt.forcecolor)) img1b = image.ComicPage(split[1], opt.profileData, img1.fill) applyImgOptimization(img1b, opt, img1) - img1b.saveToDir(dirpath, opt.forcepng, opt.forcecolor) - os.remove(os.path.join(dirpath, img0b.filename)) - os.remove(os.path.join(dirpath, img1b.filename)) + output.append(img1b.saveToDir(dirpath, opt.forcepng, opt.forcecolor)) os.remove(img.origFileName) else: applyImgOptimization(img, opt) - img.saveToDir(dirpath, opt.forcepng, opt.forcecolor) + output.append(img.saveToDir(dirpath, opt.forcepng, opt.forcecolor)) if wipe: os.remove(os.path.join(dirpath, img.filename)) if opt.quality == 2: @@ -420,8 +423,9 @@ def fileImgProcess(work): img2.image = img2.image.rotate(90) img2.rotated = True applyImgOptimization(img2, opt, img) - img2.saveToDir(dirpath, opt.forcepng, opt.forcecolor) + output.append(img2.saveToDir(dirpath, opt.forcepng, opt.forcecolor)) os.remove(os.path.join(dirpath, img2.filename)) + return output except Exception: return str(sys.exc_info()[1]) @@ -546,8 +550,9 @@ def genEpubStruct(path, chapterNames): chapter = False for afile in filenames: filename = getImageFileName(afile) - if filename is not None and not "-kcchq" in filename[0]: - filelist.append(buildHTML(dirpath, afile)) + if filename is not None and not ('-kcc-hq' in filename[0] or '-kcc-a-hq' in filename[0] + or '-kcc-b-hq' in filename[0]): + filelist.append(buildHTML(dirpath, afile, os.path.join(dirpath, afile))) if not chapter: chapterlist.append((dirpath.replace('Images', 'Text'), filelist[-1][1])) chapter = True @@ -674,6 +679,8 @@ def sanitizeTree(filetree): != slugified.upper(): slugified += "A" os.rename(os.path.join(root, name), os.path.join(root, slugified + splitname[1])) + theGreatIndex[os.path.join(root, slugified + splitname[1])] = theGreatIndex[os.path.join(root, name)] + del theGreatIndex[os.path.join(root, name)] for name in dirs: if name.startswith('.'): os.remove(os.path.join(root, name)) diff --git a/kcc/image.py b/kcc/image.py index 90055c9..d879e24 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -112,6 +112,7 @@ class ComicPage: self.noVPV = None self.noPV = None self.purge = False + self.hq = False if fill: self.fill = fill else: @@ -119,30 +120,37 @@ class ComicPage: def saveToDir(self, targetdir, forcepng, color): try: - suffix = "" + flags = [] + filename = os.path.join(targetdir, os.path.splitext(self.filename)[0]) + if not filename.endswith('-KCC-A') and not filename.endswith('-KCC-B'): + filename += '-KCC' if not color and not forcepng: self.image = self.image.convert('L') if self.rotated: - suffix += "-kccrot" - else: - suffix += "-kcchq" + flags.append('Rotated') + if self.hq: + flags.append('HighQuality') + filename += '-HQ' if self.noPV: - suffix += "-kccnpv" + flags.append('NoPanelView') else: if self.noHPV: - suffix += "-kccnh" + flags.append('NoHorizontalPanelView') if self.noVPV: - suffix += "-kccnv" + flags.append('NoVerticalPanelView') if self.border: - suffix += "-kccxl" + str(self.border[0]) + "-kccyu" + str(self.border[1]) + "-kccxr" +\ - str(self.border[2]) + "-kccyd" + str(self.border[3]) + flags.append("Margins-" + str(self.border[0]) + "-" + str(self.border[1]) + "-" + + str(self.border[2]) + "-" + str(self.border[3])) if not self.purge: if forcepng: - self.image.save(os.path.join(targetdir, os.path.splitext(self.filename)[0] + suffix + ".png"), - "PNG", optimize=1) + filename += ".png" + self.image.save(filename, "PNG", optimize=1) else: - self.image.save(os.path.join(targetdir, os.path.splitext(self.filename)[0] + suffix + ".jpg"), - "JPEG", optimize=1) + filename += ".jpg" + self.image.save(filename, "JPEG", optimize=1) + return [filename, flags] + else: + return None except IOError as e: raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, e)) @@ -211,7 +219,10 @@ class ComicPage: # Set target size if qualityMode == 0: size = (self.size[0], self.size[1]) + elif qualityMode == 1: + size = (self.panelviewsize[0], self.panelviewsize[1]) else: + self.hq = True size = (self.panelviewsize[0], self.panelviewsize[1]) # If image is small and HQ mode is on we have to force upscaling. Otherwise non-zoomed image will be distorted if self.image.size[0] <= size[0] and self.image.size[1] <= size[1] and qualityMode == 1 and not stretch: @@ -271,8 +282,8 @@ class ComicPage: leftbox = (0, 0, width, int(height / 2)) rightbox = (0, int(height / 2), width, height) filename = os.path.splitext(self.filename) - fileone = targetdir + '/' + filename[0] + '-kcca' + filename[1] - filetwo = targetdir + '/' + filename[0] + '-kccb' + filename[1] + fileone = targetdir + '/' + filename[0] + '-KCC-A' + filename[1] + filetwo = targetdir + '/' + filename[0] + '-KCC-B' + filename[1] try: if righttoleft: pageone = self.image.crop(rightbox) |