mirror of
https://github.com/ciromattia/kcc
synced 2026-05-17 13:01:42 +00:00
comic2panel: Multiprocessing support
This commit is contained in:
@@ -26,6 +26,7 @@ import sys
|
|||||||
import os
|
import os
|
||||||
from shutil import rmtree, copytree
|
from shutil import rmtree, copytree
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
from multiprocessing import Pool, freeze_support
|
||||||
from PIL import Image, ImageStat
|
from PIL import Image, ImageStat
|
||||||
|
|
||||||
|
|
||||||
@@ -43,7 +44,7 @@ def getImageFileName(imgfile):
|
|||||||
return filename
|
return filename
|
||||||
|
|
||||||
|
|
||||||
def sanitizePanelSize(panel):
|
def sanitizePanelSize(panel, options):
|
||||||
newPanels = []
|
newPanels = []
|
||||||
if panel[2] > 1.5 * options.height:
|
if panel[2] > 1.5 * options.height:
|
||||||
if (panel[2] / 2) > 1.5 * options.height:
|
if (panel[2] / 2) > 1.5 * options.height:
|
||||||
@@ -60,7 +61,15 @@ def sanitizePanelSize(panel):
|
|||||||
return newPanels
|
return newPanels
|
||||||
|
|
||||||
|
|
||||||
def splitImage(path, name):
|
def splitImage_init(options):
|
||||||
|
splitImage.options = options
|
||||||
|
|
||||||
|
|
||||||
|
# noinspection PyUnresolvedReferences
|
||||||
|
def splitImage(work):
|
||||||
|
path = work[0]
|
||||||
|
name = work[1]
|
||||||
|
options = splitImage.options
|
||||||
# Harcoded options
|
# Harcoded options
|
||||||
threshold = 10.0
|
threshold = 10.0
|
||||||
delta = 10
|
delta = 10
|
||||||
@@ -96,7 +105,7 @@ def splitImage(path, name):
|
|||||||
panelHeight = y2Temp - y1Temp
|
panelHeight = y2Temp - y1Temp
|
||||||
if y2Temp != heightImg:
|
if y2Temp != heightImg:
|
||||||
# Panels that can't be cut nicely will be forcefully splitted
|
# Panels that can't be cut nicely will be forcefully splitted
|
||||||
panelsCleaned = sanitizePanelSize([y1Temp, y2Temp, panelHeight])
|
panelsCleaned = sanitizePanelSize([y1Temp, y2Temp, panelHeight], options)
|
||||||
for panel in panelsCleaned:
|
for panel in panelsCleaned:
|
||||||
panels.append(panel)
|
panels.append(panel)
|
||||||
if options.debug:
|
if options.debug:
|
||||||
@@ -145,6 +154,7 @@ def Copyright():
|
|||||||
'Written 2013 by Ciro Mattia Gonano and Pawel Jastrzebski.' % globals())
|
'Written 2013 by Ciro Mattia Gonano and Pawel Jastrzebski.' % globals())
|
||||||
|
|
||||||
|
|
||||||
|
# noinspection PyBroadException
|
||||||
def main(argv=None):
|
def main(argv=None):
|
||||||
global options
|
global options
|
||||||
usage = "Usage: %prog [options] comic_folder"
|
usage = "Usage: %prog [options] comic_folder"
|
||||||
@@ -164,12 +174,28 @@ def main(argv=None):
|
|||||||
if os.path.isdir(options.sourceDir):
|
if os.path.isdir(options.sourceDir):
|
||||||
rmtree(options.targetDir, True)
|
rmtree(options.targetDir, True)
|
||||||
copytree(options.sourceDir, options.targetDir)
|
copytree(options.sourceDir, options.targetDir)
|
||||||
|
work = []
|
||||||
|
pool = Pool(None, splitImage_init, [options])
|
||||||
for root, dirs, files in os.walk(options.targetDir, False):
|
for root, dirs, files in os.walk(options.targetDir, False):
|
||||||
for name in files:
|
for name in files:
|
||||||
if getImageFileName(name) is not None:
|
if getImageFileName(name) is not None:
|
||||||
splitImage(root, name)
|
work.append([root, name])
|
||||||
else:
|
else:
|
||||||
os.remove(os.path.join(root, name))
|
os.remove(os.path.join(root, name))
|
||||||
|
if len(work) > 0:
|
||||||
|
workers = pool.map_async(func=splitImage, iterable=work)
|
||||||
|
pool.close()
|
||||||
|
pool.join()
|
||||||
|
try:
|
||||||
|
workers.get()
|
||||||
|
except:
|
||||||
|
rmtree(options.targetDir)
|
||||||
|
print "ERROR: One of workers crashed. Cause: " + str(sys.exc_info()[1])
|
||||||
|
sys.exit(1)
|
||||||
|
else:
|
||||||
|
rmtree(options.targetDir)
|
||||||
|
print "ERROR: Source directory is empty!!"
|
||||||
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
print "ERROR: Provided path is not a directory!"
|
print "ERROR: Provided path is not a directory!"
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@@ -178,7 +204,7 @@ def main(argv=None):
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# freeze_support()
|
freeze_support()
|
||||||
Copyright()
|
Copyright()
|
||||||
main(sys.argv[1:])
|
main(sys.argv[1:])
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
Reference in New Issue
Block a user