mirror of
https://github.com/ciromattia/kcc
synced 2025-12-13 17:56:30 +00:00
Added subfolder support
This commit is contained in:
@@ -15,7 +15,8 @@ It also optimizes comic images by:
|
|||||||
- flat folders
|
- flat folders
|
||||||
- PDF *(extracting only contained JPG images)*
|
- PDF *(extracting only contained JPG images)*
|
||||||
|
|
||||||
For now the script does not understand folder depth, so it will work on flat folders/archives only.
|
~~For now the script does not understand folder depth, so it will work on flat folders/archives only.~~
|
||||||
|
As of v. 1.50, KCC supports subfolders!
|
||||||
|
|
||||||
## REQUIREMENTS
|
## REQUIREMENTS
|
||||||
- `kindlegen` in /usr/local/bin/
|
- `kindlegen` in /usr/local/bin/
|
||||||
@@ -90,6 +91,7 @@ and installed in `/usr/local/bin/`
|
|||||||
- 1.40 - Added some options for controlling image optimization
|
- 1.40 - Added some options for controlling image optimization
|
||||||
Further optimization (ImageOps, page numbering cut, autocontrast)
|
Further optimization (ImageOps, page numbering cut, autocontrast)
|
||||||
- 1.41 - Fixed a serious bug on resizing when img ratio was bigger than device one
|
- 1.41 - Fixed a serious bug on resizing when img ratio was bigger than device one
|
||||||
|
- 1.50 - Added subfolder support for multiple chapters.
|
||||||
- 2.00 - GUI! AppleScript is gone and Tk is used to provide cross-platform GUI support.
|
- 2.00 - GUI! AppleScript is gone and Tk is used to provide cross-platform GUI support.
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
# 1.40 - Added some options for controlling image optimization
|
# 1.40 - Added some options for controlling image optimization
|
||||||
# Further optimization (ImageOps, page numbering cut, autocontrast)
|
# Further optimization (ImageOps, page numbering cut, autocontrast)
|
||||||
# 1.41 - Fixed a serious bug on resizing when img ratio was bigger than device one
|
# 1.41 - Fixed a serious bug on resizing when img ratio was bigger than device one
|
||||||
|
# 1.50 - Support for subfolders
|
||||||
#
|
#
|
||||||
# Todo:
|
# Todo:
|
||||||
# - Add gracefully exit for CBR if no rarfile.py and no unrar
|
# - Add gracefully exit for CBR if no rarfile.py and no unrar
|
||||||
@@ -36,85 +37,84 @@
|
|||||||
# - Improve error reporting
|
# - Improve error reporting
|
||||||
# - recurse into dirtree for multiple comics
|
# - recurse into dirtree for multiple comics
|
||||||
|
|
||||||
__version__ = '1.30'
|
__version__ = '1.50'
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
import image, cbxarchive, pdfjpgextract
|
import image, cbxarchive, pdfjpgextract
|
||||||
|
|
||||||
class HTMLbuilder:
|
def buildHTML(path,file):
|
||||||
|
filename = getImageFileName(file)
|
||||||
def getResult(self):
|
if filename is not None:
|
||||||
return getImageFileName(self.file)
|
htmlfile = os.path.join(path,filename[0] + '.html')
|
||||||
|
f = open(htmlfile, "w")
|
||||||
def __init__(self, dstdir, file):
|
f.writelines(["<!DOCTYPE html SYSTEM \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n",
|
||||||
self.file = file
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n",
|
||||||
filename = getImageFileName(file)
|
"<head>\n",
|
||||||
if filename is not None:
|
"<title>",filename[0],"</title>\n",
|
||||||
htmlfile = dstdir + '/' + filename[0] + '.html'
|
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n",
|
||||||
f = open(htmlfile, "w")
|
"</head>\n",
|
||||||
f.writelines(["<!DOCTYPE html SYSTEM \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n",
|
"<body>\n",
|
||||||
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n",
|
"<div><img src=\"",file,"\" /></div>\n",
|
||||||
"<head>\n",
|
"</body>\n",
|
||||||
"<title>",filename[0],"</title>\n",
|
"</html>"
|
||||||
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n",
|
])
|
||||||
"</head>\n",
|
|
||||||
"<body>\n",
|
|
||||||
"<div><img src=\"",file,"\" /></div>\n",
|
|
||||||
"</body>\n",
|
|
||||||
"</html>"
|
|
||||||
])
|
|
||||||
f.close()
|
|
||||||
return
|
|
||||||
|
|
||||||
class NCXbuilder:
|
|
||||||
def __init__(self, dstdir, title):
|
|
||||||
ncxfile = dstdir + '/content.ncx'
|
|
||||||
f = open(ncxfile, "w")
|
|
||||||
f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
|
|
||||||
"<!DOCTYPE ncx PUBLIC \"-//NISO//DTD ncx 2005-1//EN\" \"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd\">\n",
|
|
||||||
"<ncx version=\"2005-1\" xml:lang=\"en-US\" xmlns=\"http://www.daisy.org/z3986/2005/ncx/\">\n",
|
|
||||||
"<head>\n</head>\n",
|
|
||||||
"<docTitle><text>",title,"</text></docTitle>\n",
|
|
||||||
"<navMap></navMap>\n</ncx>"
|
|
||||||
])
|
|
||||||
f.close()
|
f.close()
|
||||||
return
|
return path,file
|
||||||
|
|
||||||
class OPFBuilder:
|
def buildNCX(dstdir, title):
|
||||||
def __init__(self, profile, dstdir, title, filelist):
|
ncxfile = dstdir + '/toc.ncx'
|
||||||
opffile = dstdir + '/content.opf'
|
f = open(ncxfile, "w")
|
||||||
# read the first file resolution
|
f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
|
||||||
profilelabel, deviceres, palette = image.ProfileData.Profiles[profile]
|
"<!DOCTYPE ncx PUBLIC \"-//NISO//DTD ncx 2005-1//EN\" \"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd\">\n",
|
||||||
imgres = str(deviceres[0]) + "x" + str(deviceres[1])
|
"<ncx version=\"2005-1\" xml:lang=\"en-US\" xmlns=\"http://www.daisy.org/z3986/2005/ncx/\">\n",
|
||||||
f = open(opffile, "w")
|
"<head>\n</head>\n",
|
||||||
f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
|
"<docTitle><text>",title,"</text></docTitle>\n",
|
||||||
"<package version=\"2.0\" unique-identifier=\"PrimaryID\" xmlns=\"http://www.idpf.org/2007/opf\">\n",
|
"<navMap></navMap>\n</ncx>"
|
||||||
"<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:opf=\"http://www.idpf.org/2007/opf\">\n",
|
])
|
||||||
"<dc:title>",title,"</dc:title>\n",
|
f.close()
|
||||||
"<dc:language>en-US</dc:language>\n",
|
return
|
||||||
"<meta name=\"book-type\" content=\"comic\"/>\n",
|
|
||||||
"<meta name=\"zero-gutter\" content=\"true\"/>\n",
|
def buildOPF(profile, dstdir, title, filelist):
|
||||||
"<meta name=\"zero-margin\" content=\"true\"/>\n",
|
opffile = dstdir + '/content.opf'
|
||||||
"<meta name=\"fixed-layout\" content=\"true\"/>\n",
|
# read the first file resolution
|
||||||
"<meta name=\"orientation-lock\" content=\"portrait\"/>\n",
|
profilelabel, deviceres, palette = image.ProfileData.Profiles[profile]
|
||||||
"<meta name=\"original-resolution\" content=\"" + imgres + "\"/>\n",
|
imgres = str(deviceres[0]) + "x" + str(deviceres[1])
|
||||||
"</metadata><manifest><item id=\"ncx\" href=\"content.ncx\" media-type=\"application/x-dtbncx+xml\"/>\n"])
|
f = open(opffile, "w")
|
||||||
for filename in filelist:
|
f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
|
||||||
f.write("<item id=\"page_" + filename[0] + "\" href=\"" + filename[0] + ".html\" media-type=\"application/xhtml+xml\"/>\n")
|
"<package version=\"2.0\" unique-identifier=\"PrimaryID\" xmlns=\"http://www.idpf.org/2007/opf\">\n",
|
||||||
for filename in filelist:
|
"<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:opf=\"http://www.idpf.org/2007/opf\">\n",
|
||||||
if '.png' == filename[1]:
|
"<dc:title>",title,"</dc:title>\n",
|
||||||
mt = 'image/png'
|
"<dc:language>en-US</dc:language>\n",
|
||||||
else:
|
"<meta name=\"book-type\" content=\"comic\"/>\n",
|
||||||
mt = 'image/jpeg'
|
"<meta name=\"zero-gutter\" content=\"true\"/>\n",
|
||||||
f.write("<item id=\"img_" + filename[0] + "\" href=\"" + filename[0] + filename[1] + "\" media-type=\"" + mt + "\"/>\n")
|
"<meta name=\"zero-margin\" content=\"true\"/>\n",
|
||||||
f.write("</manifest>\n<spine toc=\"ncx\">\n")
|
"<meta name=\"fixed-layout\" content=\"true\"/>\n",
|
||||||
for filename in filelist:
|
"<meta name=\"orientation-lock\" content=\"portrait\"/>\n",
|
||||||
f.write("<itemref idref=\"page_" + filename[0] + "\" />\n")
|
"<meta name=\"original-resolution\" content=\"" + imgres + "\"/>\n",
|
||||||
f.write("</spine>\n<guide>\n</guide>\n</package>\n")
|
"</metadata><manifest><item id=\"ncx\" href=\"toc.ncx\" media-type=\"application/x-dtbncx+xml\"/>\n"])
|
||||||
f.close()
|
for path in filelist:
|
||||||
return
|
folder = path[0].replace(dstdir,'').lstrip('/')
|
||||||
|
filename = getImageFileName(path[1])
|
||||||
|
uniqueid = os.path.join(folder,filename[0]).replace('/','_')
|
||||||
|
f.write("<item id=\"page_" + uniqueid + "\" href=\"" + os.path.join(folder,filename[0])
|
||||||
|
+ ".html\" media-type=\"application/xhtml+xml\"/>\n")
|
||||||
|
#for filename in filelist:
|
||||||
|
if '.png' == filename[1]:
|
||||||
|
mt = 'image/png'
|
||||||
|
else:
|
||||||
|
mt = 'image/jpeg'
|
||||||
|
f.write("<item id=\"img_" + uniqueid + "\" href=\"" + os.path.join(folder,path[1]) + "\" media-type=\"" + mt + "\"/>\n")
|
||||||
|
f.write("</manifest>\n<spine toc=\"ncx\">\n")
|
||||||
|
for path in filelist:
|
||||||
|
folder = path[0].replace(dstdir,'').lstrip('/')
|
||||||
|
filename = getImageFileName(path[1])
|
||||||
|
uniqueid = os.path.join(folder,filename[0]).replace('/','_')
|
||||||
|
f.write("<itemref idref=\"page_" + uniqueid + "\" />\n")
|
||||||
|
f.write("</spine>\n<guide>\n</guide>\n</package>\n")
|
||||||
|
f.close()
|
||||||
|
return
|
||||||
|
|
||||||
def getImageFileName(file):
|
def getImageFileName(file):
|
||||||
filename = os.path.splitext(file)
|
filename = os.path.splitext(file)
|
||||||
@@ -130,6 +130,78 @@ def isInFilelist(file,list):
|
|||||||
seen = True
|
seen = True
|
||||||
return seen
|
return seen
|
||||||
|
|
||||||
|
def applyImgOptimization(img):
|
||||||
|
img.optimizeImage()
|
||||||
|
img.cropWhiteSpace(10.0)
|
||||||
|
if options.cutpagenumbers:
|
||||||
|
img.cutPageNumber()
|
||||||
|
img.resizeImage(options.upscale,options.stretch)
|
||||||
|
img.quantizeImage()
|
||||||
|
|
||||||
|
|
||||||
|
def dirImgProcess(path):
|
||||||
|
global options
|
||||||
|
|
||||||
|
for (dirpath, dirnames, filenames) in os.walk(path):
|
||||||
|
for file in filenames:
|
||||||
|
if getImageFileName(file) is not None:
|
||||||
|
if options.verbose:
|
||||||
|
print "Optimizing " + file + " for " + options.profile
|
||||||
|
else:
|
||||||
|
print ".",
|
||||||
|
img = image.ComicPage(os.path.join(dirpath,file), options.profile)
|
||||||
|
split = img.splitPage(dirpath, options.righttoleft)
|
||||||
|
if split is not None:
|
||||||
|
print "Splitted " + file
|
||||||
|
img0 = image.ComicPage(split[0],options.profile)
|
||||||
|
img1 = image.ComicPage(split[1],options.profile)
|
||||||
|
applyImgOptimization(img0)
|
||||||
|
img0.saveToDir(dirpath)
|
||||||
|
applyImgOptimization(img1)
|
||||||
|
img1.saveToDir(dirpath)
|
||||||
|
else:
|
||||||
|
applyImgOptimization(img)
|
||||||
|
img.saveToDir(dirpath)
|
||||||
|
|
||||||
|
def genEpubStruct(path):
|
||||||
|
global options
|
||||||
|
filelist = []
|
||||||
|
for (dirpath, dirnames, filenames) in os.walk(path):
|
||||||
|
for file in filenames:
|
||||||
|
if getImageFileName(file) is not None:
|
||||||
|
# put credits at the end
|
||||||
|
if "credits" in file.lower():
|
||||||
|
os.rename(os.path.join(dirpath,file), os.path.join(dirpath,'ZZZ999_'+file))
|
||||||
|
file = 'ZZZ999_'+file
|
||||||
|
filelist.append(buildHTML(dirpath,file))
|
||||||
|
#filelist.extend(filenames)
|
||||||
|
if options.title == 'defaulttitle':
|
||||||
|
options.title = os.path.basename(path)
|
||||||
|
buildNCX(path,options.title)
|
||||||
|
# ensure we're sorting files alphabetically
|
||||||
|
filelist = sorted(filelist, key=lambda name: (name[0].lower(), name[1].lower()))
|
||||||
|
buildOPF(options.profile,path,options.title,filelist)
|
||||||
|
|
||||||
|
def getWorkFolder(file):
|
||||||
|
fname = os.path.splitext(file)
|
||||||
|
if fname[1].lower() == '.pdf':
|
||||||
|
pdf = pdfjpgextract.PdfJpgExtract(file)
|
||||||
|
pdf.extract()
|
||||||
|
return pdf.getPath()
|
||||||
|
else:
|
||||||
|
cbx = cbxarchive.CBxArchive(file)
|
||||||
|
if cbx.isCbxFile():
|
||||||
|
cbx.extract()
|
||||||
|
return cbx.getPath()
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
import shutil
|
||||||
|
if not os.path.isdir(file + "_orig"):
|
||||||
|
shutil.copytree(file, file + "_orig")
|
||||||
|
return file
|
||||||
|
except OSError:
|
||||||
|
raise
|
||||||
|
|
||||||
def Copyright():
|
def Copyright():
|
||||||
print ('comic2ebook v%(__version__)s. '
|
print ('comic2ebook v%(__version__)s. '
|
||||||
'Written 2012 by Ciro Mattia Gonano.' % globals())
|
'Written 2012 by Ciro Mattia Gonano.' % globals())
|
||||||
@@ -140,7 +212,7 @@ def Usage():
|
|||||||
parser.print_help()
|
parser.print_help()
|
||||||
|
|
||||||
def main(argv=None):
|
def main(argv=None):
|
||||||
global parser
|
global parser, options
|
||||||
usage = "Usage: %prog [options] comic_file|comic_folder"
|
usage = "Usage: %prog [options] comic_file|comic_folder"
|
||||||
parser = OptionParser(usage=usage, version=__version__)
|
parser = OptionParser(usage=usage, version=__version__)
|
||||||
parser.add_option("-p", "--profile", action="store", dest="profile", default="KHD",
|
parser.add_option("-p", "--profile", action="store", dest="profile", default="KHD",
|
||||||
@@ -163,68 +235,14 @@ def main(argv=None):
|
|||||||
if len(args) != 1:
|
if len(args) != 1:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
return
|
return
|
||||||
dir = args[0]
|
path = args[0]
|
||||||
fname = os.path.splitext(dir)
|
path = getWorkFolder(path)
|
||||||
if fname[1].lower() == '.pdf':
|
|
||||||
pdf = pdfjpgextract.PdfJpgExtract(dir)
|
|
||||||
pdf.extract()
|
|
||||||
dir = pdf.getPath()
|
|
||||||
else:
|
|
||||||
cbx = cbxarchive.CBxArchive(dir)
|
|
||||||
if cbx.isCbxFile():
|
|
||||||
cbx.extract()
|
|
||||||
dir = cbx.getPath()
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
import shutil
|
|
||||||
shutil.copytree(dir, dir + "_orig")
|
|
||||||
#dir = dir + "_orig"
|
|
||||||
except OSError as exc:
|
|
||||||
raise
|
|
||||||
filelist = []
|
|
||||||
if options.imgproc:
|
if options.imgproc:
|
||||||
print "Processing images..."
|
print "Processing images..."
|
||||||
try:
|
dirImgProcess(path)
|
||||||
if options.verbose:
|
|
||||||
print "Splitting double pages..."
|
|
||||||
for file in os.listdir(dir):
|
|
||||||
if getImageFileName(file) is not None:
|
|
||||||
print ".",
|
|
||||||
img = image.ComicPage(dir+'/'+file, options.profile)
|
|
||||||
img.splitPage(dir, options.righttoleft)
|
|
||||||
for file in os.listdir(dir):
|
|
||||||
if getImageFileName(file) is not None:
|
|
||||||
if options.verbose:
|
|
||||||
print "Optimizing " + file + " for " + options.profile
|
|
||||||
else:
|
|
||||||
print ".",
|
|
||||||
img = image.ComicPage(dir+'/'+file, options.profile)
|
|
||||||
img.optimizeImage()
|
|
||||||
img.cropWhiteSpace(10.0)
|
|
||||||
if options.cutpagenumbers:
|
|
||||||
img.cutPageNumber()
|
|
||||||
img.resizeImage(options.upscale,options.stretch)
|
|
||||||
img.quantizeImage()
|
|
||||||
img.saveToDir(dir)
|
|
||||||
except ImportError:
|
|
||||||
print "Could not load PIL, not optimizing image"
|
|
||||||
|
|
||||||
print "Creating ePub structure..."
|
print "Creating ePub structure..."
|
||||||
for file in os.listdir(dir):
|
genEpubStruct(path)
|
||||||
if getImageFileName(file) is not None and isInFilelist(file,filelist) == False:
|
|
||||||
# put credits at the end
|
|
||||||
if "credits" in file.lower():
|
|
||||||
os.rename(dir+'/'+file, dir+'/ZZZ999_'+file)
|
|
||||||
file = 'ZZZ999_'+file
|
|
||||||
filename = HTMLbuilder(dir,file).getResult()
|
|
||||||
if filename is not None:
|
|
||||||
filelist.append(filename)
|
|
||||||
if options.title == 'defaulttitle':
|
|
||||||
options.title = os.path.basename(dir)
|
|
||||||
NCXbuilder(dir,options.title)
|
|
||||||
# ensure we're sorting files alphabetically
|
|
||||||
filelist = sorted(filelist, key=lambda name: name[0].lower())
|
|
||||||
OPFBuilder(options.profile,dir,options.title,filelist)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -118,6 +118,12 @@ class ComicPage:
|
|||||||
method = Image.ANTIALIAS
|
method = Image.ANTIALIAS
|
||||||
if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]:
|
if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]:
|
||||||
if not upscale:
|
if not upscale:
|
||||||
|
# do not upscale but center image in a device-sized image
|
||||||
|
newImage = Image.new('RGB', (self.size[0], self.size[1]), (255,255,255))
|
||||||
|
newImage.paste(self.image, (
|
||||||
|
(self.size[0] - self.image.size[0]) / 2,
|
||||||
|
(self.size[1] - self.image.size[1]) / 2))
|
||||||
|
self.image = newImage
|
||||||
return self.image
|
return self.image
|
||||||
else:
|
else:
|
||||||
method = Image.NEAREST
|
method = Image.NEAREST
|
||||||
@@ -170,7 +176,8 @@ class ComicPage:
|
|||||||
except IOError as e:
|
except IOError as e:
|
||||||
raise RuntimeError('Cannot write image in directory %s: %s' %(targetdir,e))
|
raise RuntimeError('Cannot write image in directory %s: %s' %(targetdir,e))
|
||||||
return fileone,filetwo
|
return fileone,filetwo
|
||||||
return None
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
def frameImage(self):
|
def frameImage(self):
|
||||||
foreground = tuple(self.palette[:3])
|
foreground = tuple(self.palette[:3])
|
||||||
|
|||||||
Reference in New Issue
Block a user