mirror of
https://github.com/ciromattia/kcc
synced 2026-10-10 11:32:34 +00:00
dirImgProcess - multiprocessing
This commit is contained in:
+48
-35
@@ -32,6 +32,7 @@ from shutil import copytree
|
|||||||
from shutil import rmtree
|
from shutil import rmtree
|
||||||
from shutil import make_archive
|
from shutil import make_archive
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
from multiprocessing import Pool
|
||||||
import image
|
import image
|
||||||
import cbxarchive
|
import cbxarchive
|
||||||
import pdfjpgextract
|
import pdfjpgextract
|
||||||
@@ -255,7 +256,7 @@ def buildOPF(profile, dstdir, title, filelist, cover=None):
|
|||||||
f.write("</manifest>\n<spine toc=\"ncx\">\n")
|
f.write("</manifest>\n<spine toc=\"ncx\">\n")
|
||||||
splitCountUsed = 1
|
splitCountUsed = 1
|
||||||
for entry in reflist:
|
for entry in reflist:
|
||||||
if entry.endswith("-000001"):
|
if entry.endswith("-000001") or entry.endswith("-1"):
|
||||||
# noinspection PyRedundantParentheses
|
# noinspection PyRedundantParentheses
|
||||||
if ((options.righttoleft and facing == 'left') or (not options.righttoleft and facing == 'right')) and\
|
if ((options.righttoleft and facing == 'left') or (not options.righttoleft and facing == 'right')) and\
|
||||||
options.landscapemode:
|
options.landscapemode:
|
||||||
@@ -265,7 +266,7 @@ def buildOPF(profile, dstdir, title, filelist, cover=None):
|
|||||||
f.write("<itemref idref=\"page_" + entry + "\" properties=\"page-spread-" + facing1 + "\"/>\n")
|
f.write("<itemref idref=\"page_" + entry + "\" properties=\"page-spread-" + facing1 + "\"/>\n")
|
||||||
else:
|
else:
|
||||||
f.write("<itemref idref=\"page_" + entry + "\"/>\n")
|
f.write("<itemref idref=\"page_" + entry + "\"/>\n")
|
||||||
elif entry.endswith("-000002"):
|
elif entry.endswith("-000002") or entry.endswith("-2"):
|
||||||
if options.landscapemode:
|
if options.landscapemode:
|
||||||
f.write("<itemref idref=\"page_" + entry + "\" properties=\"page-spread-" + facing2 + "\"/>\n")
|
f.write("<itemref idref=\"page_" + entry + "\" properties=\"page-spread-" + facing2 + "\"/>\n")
|
||||||
else:
|
else:
|
||||||
@@ -323,7 +324,7 @@ def isInFilelist(filename, filelist):
|
|||||||
return seen
|
return seen
|
||||||
|
|
||||||
|
|
||||||
def applyImgOptimization(img, isSplit=False, toRight=False):
|
def applyImgOptimization(img, isSplit, toRight, options):
|
||||||
img.cropWhiteSpace(10.0)
|
img.cropWhiteSpace(10.0)
|
||||||
if options.cutpagenumbers:
|
if options.cutpagenumbers:
|
||||||
img.cutPageNumber()
|
img.cutPageNumber()
|
||||||
@@ -336,44 +337,19 @@ def applyImgOptimization(img, isSplit=False, toRight=False):
|
|||||||
|
|
||||||
def dirImgProcess(path):
|
def dirImgProcess(path):
|
||||||
global options, splitCount
|
global options, splitCount
|
||||||
|
work = []
|
||||||
pagenumber = 0
|
pagenumber = 0
|
||||||
pagenumbermodifier = 0
|
pagenumbermodifier = 0
|
||||||
splitpages = []
|
pool = Pool()
|
||||||
|
|
||||||
for (dirpath, dirnames, filenames) in os.walk(path):
|
for (dirpath, dirnames, filenames) in os.walk(path):
|
||||||
for afile in filenames:
|
for afile in filenames:
|
||||||
if getImageFileName(afile) is not None:
|
if getImageFileName(afile) is not None:
|
||||||
pagenumber += 1
|
pagenumber += 1
|
||||||
if options.verbose:
|
work.append([afile, dirpath, pagenumber, options])
|
||||||
print "Optimizing " + afile + " for " + options.profile
|
splitpages = pool.map(fileImgProcess, work)
|
||||||
else:
|
pool.close()
|
||||||
print ".",
|
pool.join()
|
||||||
img = image.ComicPage(os.path.join(dirpath, afile), options.profile)
|
splitpages = filter(None, splitpages)
|
||||||
if options.nosplitrotate:
|
|
||||||
split = None
|
|
||||||
else:
|
|
||||||
split = img.splitPage(dirpath, options.righttoleft, options.rotate)
|
|
||||||
if split is not None and split is not "R":
|
|
||||||
if options.verbose:
|
|
||||||
print "Splitted " + afile
|
|
||||||
if options.righttoleft:
|
|
||||||
toRight1 = False
|
|
||||||
toRight2 = True
|
|
||||||
splitpages.append(pagenumber)
|
|
||||||
else:
|
|
||||||
toRight1 = True
|
|
||||||
toRight2 = False
|
|
||||||
splitpages.append(pagenumber)
|
|
||||||
img0 = image.ComicPage(split[0], options.profile)
|
|
||||||
applyImgOptimization(img0, True, toRight1)
|
|
||||||
img0.saveToDir(dirpath, options.forcepng, options.forcecolor, None)
|
|
||||||
img1 = image.ComicPage(split[1], options.profile)
|
|
||||||
applyImgOptimization(img1, True, toRight2)
|
|
||||||
img1.saveToDir(dirpath, options.forcepng, options.forcecolor, None)
|
|
||||||
else:
|
|
||||||
applyImgOptimization(img)
|
|
||||||
img.saveToDir(dirpath, options.forcepng, options.forcecolor, split)
|
|
||||||
|
|
||||||
splitpages.sort()
|
splitpages.sort()
|
||||||
for page in splitpages:
|
for page in splitpages:
|
||||||
if (page + pagenumbermodifier) % 2 == 0:
|
if (page + pagenumbermodifier) % 2 == 0:
|
||||||
@@ -381,6 +357,43 @@ def dirImgProcess(path):
|
|||||||
pagenumbermodifier += 1
|
pagenumbermodifier += 1
|
||||||
|
|
||||||
|
|
||||||
|
def fileImgProcess(work):
|
||||||
|
afile = work[0]
|
||||||
|
dirpath = work[1]
|
||||||
|
pagenumber = work[2]
|
||||||
|
options = work[3]
|
||||||
|
output = None
|
||||||
|
if options.verbose:
|
||||||
|
print "Optimizing " + afile + " for " + options.profile
|
||||||
|
else:
|
||||||
|
print ".",
|
||||||
|
img = image.ComicPage(os.path.join(dirpath, afile), options.profile)
|
||||||
|
if options.nosplitrotate:
|
||||||
|
split = None
|
||||||
|
else:
|
||||||
|
split = img.splitPage(dirpath, options.righttoleft, options.rotate)
|
||||||
|
if split is not None and split is not "R":
|
||||||
|
if options.verbose:
|
||||||
|
print "Splitted " + afile
|
||||||
|
if options.righttoleft:
|
||||||
|
toRight1 = False
|
||||||
|
toRight2 = True
|
||||||
|
else:
|
||||||
|
toRight1 = True
|
||||||
|
toRight2 = False
|
||||||
|
output = pagenumber
|
||||||
|
img0 = image.ComicPage(split[0], options.profile)
|
||||||
|
applyImgOptimization(img0, True, toRight1, options)
|
||||||
|
img0.saveToDir(dirpath, options.forcepng, options.forcecolor)
|
||||||
|
img1 = image.ComicPage(split[1], options.profile)
|
||||||
|
applyImgOptimization(img1, True, toRight2, options)
|
||||||
|
img1.saveToDir(dirpath, options.forcepng, options.forcecolor)
|
||||||
|
else:
|
||||||
|
applyImgOptimization(img, False, False, options)
|
||||||
|
img.saveToDir(dirpath, options.forcepng, options.forcecolor, split)
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
def genEpubStruct(path):
|
def genEpubStruct(path):
|
||||||
global options
|
global options
|
||||||
filelist = []
|
filelist = []
|
||||||
|
|||||||
+1
-1
@@ -119,7 +119,7 @@ class ComicPage:
|
|||||||
raise RuntimeError('Cannot read image file %s' % source)
|
raise RuntimeError('Cannot read image file %s' % source)
|
||||||
self.image = self.image.convert('RGB')
|
self.image = self.image.convert('RGB')
|
||||||
|
|
||||||
def saveToDir(self, targetdir, forcepng, color, sufix):
|
def saveToDir(self, targetdir, forcepng, color, sufix=None):
|
||||||
filename = os.path.basename(self.origFileName)
|
filename = os.path.basename(self.origFileName)
|
||||||
try:
|
try:
|
||||||
if not color:
|
if not color:
|
||||||
|
|||||||
Reference in New Issue
Block a user