mirror of
https://github.com/ciromattia/kcc
synced 2026-07-01 18:45:26 +00:00
Mainly adhere to PEP 8 code style (http://www.python.org/dev/peps/pep-0008/)
Add some commented code for working on Panel view enhancement and natural sorting.
This commit is contained in:
+153
-103
@@ -17,17 +17,33 @@
|
|||||||
# PERFORMANCE OF THIS SOFTWARE.
|
# PERFORMANCE OF THIS SOFTWARE.
|
||||||
#
|
#
|
||||||
__version__ = '2.5'
|
__version__ = '2.5'
|
||||||
__license__ = 'ISC'
|
__license__ = 'ISC'
|
||||||
__copyright__ = '2012-2013, Ciro Mattia Gonano <ciromattia@gmail.com>'
|
__copyright__ = '2012-2013, Ciro Mattia Gonano <ciromattia@gmail.com>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import os, sys, tempfile
|
import os
|
||||||
from shutil import move,copyfile,copytree,rmtree,make_archive
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import re
|
||||||
|
from shutil import move
|
||||||
|
from shutil import copyfile
|
||||||
|
from shutil import copytree
|
||||||
|
from shutil import rmtree
|
||||||
|
from shutil import make_archive
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
import image, cbxarchive, pdfjpgextract
|
import image
|
||||||
|
import cbxarchive
|
||||||
|
import pdfjpgextract
|
||||||
|
|
||||||
def buildHTML(path,file):
|
|
||||||
filename = getImageFileName(file)
|
def sort_nicely(l):
|
||||||
|
convert = lambda text: int(text) if text.isdigit() else text
|
||||||
|
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
|
||||||
|
l.sort(key=alphanum_key)
|
||||||
|
|
||||||
|
|
||||||
|
def buildHTML(path, imgfile):
|
||||||
|
filename = getImageFileName(imgfile)
|
||||||
if filename is not None:
|
if filename is not None:
|
||||||
htmlpath = ''
|
htmlpath = ''
|
||||||
postfix = ''
|
postfix = ''
|
||||||
@@ -36,55 +52,70 @@ def buildHTML(path,file):
|
|||||||
while True:
|
while True:
|
||||||
head, tail = os.path.split(head)
|
head, tail = os.path.split(head)
|
||||||
if tail == 'Images':
|
if tail == 'Images':
|
||||||
htmlpath = os.path.join(head,'Text',postfix)
|
htmlpath = os.path.join(head, 'Text', postfix)
|
||||||
break
|
break
|
||||||
postfix = tail + "/" + postfix
|
postfix = tail + "/" + postfix
|
||||||
backref += 1
|
backref += 1
|
||||||
if not os.path.exists(htmlpath):
|
if not os.path.exists(htmlpath):
|
||||||
os.makedirs(htmlpath)
|
os.makedirs(htmlpath)
|
||||||
htmlfile = os.path.join(htmlpath,filename[0] + '.html')
|
htmlfile = os.path.join(htmlpath, filename[0] + '.html')
|
||||||
f = open(htmlfile, "w")
|
f = open(htmlfile, "w")
|
||||||
f.writelines(["<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n",
|
f.writelines(["<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" ",
|
||||||
|
"\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n",
|
||||||
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n",
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n",
|
||||||
"<head>\n",
|
"<head>\n",
|
||||||
"<title>",filename[0],"</title>\n",
|
"<title>", filename[0], "</title>\n",
|
||||||
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n",
|
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n",
|
||||||
"</head>\n",
|
"</head>\n",
|
||||||
"<body>\n",
|
"<body>\n",
|
||||||
"<div><img src=\"","../" * backref,"Images/",postfix,file,"\" alt=\"",file,"\" /></div>\n",
|
"<div><img src=\"../" * backref, "Images/", postfix, imgfile, "\" alt=\"",
|
||||||
|
imgfile, "\" class=\"singlePage\"/></div>\n",
|
||||||
|
#"<div id=\"", filename[0], "-1\">\n",
|
||||||
|
#"<a class=\"app-amzn-magnify\" data-app-amzn-magnify='{\"targetId\":\"", filename[0],
|
||||||
|
#"-1-magTargetParent\", \"ordinal\":1}'></a>\n",
|
||||||
|
#"</div>\n",
|
||||||
|
#"<div id=\"", filename[0], "-1-magTargetParent\" class=\"target-mag-parent\">\n",
|
||||||
|
#"<div class=\"target-mag-lb\">\n",
|
||||||
|
#"</div>\n",
|
||||||
|
#"<div id=\"", filename[0], "-1-magTarget\" class=\"target-mag\">\n",
|
||||||
|
#"<img src=\"../" * backref, "Images/", postfix, imgfile, "\" alt=\"", imgfile, "\"/>\n",
|
||||||
|
#"</div></div>\n",
|
||||||
"</body>\n",
|
"</body>\n",
|
||||||
"</html>"
|
"</html>"
|
||||||
])
|
])
|
||||||
f.close()
|
f.close()
|
||||||
return path,file
|
return path, imgfile
|
||||||
|
|
||||||
|
|
||||||
def buildNCX(dstdir, title, chapters):
|
def buildNCX(dstdir, title, chapters):
|
||||||
ncxfile = os.path.join(dstdir,'OEBPS','toc.ncx')
|
ncxfile = os.path.join(dstdir, 'OEBPS', 'toc.ncx')
|
||||||
f = open(ncxfile, "w")
|
f = open(ncxfile, "w")
|
||||||
f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
|
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",
|
"<!DOCTYPE ncx PUBLIC \"-//NISO//DTD ncx 2005-1//EN\" ",
|
||||||
"<ncx version=\"2005-1\" xml:lang=\"en-US\" xmlns=\"http://www.daisy.org/z3986/2005/ncx/\">\n",
|
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd\">\n",
|
||||||
"<head>\n",
|
"<ncx version=\"2005-1\" xml:lang=\"en-US\" xmlns=\"http://www.daisy.org/z3986/2005/ncx/\">\n",
|
||||||
"<meta name=\"dtb:uid\" content=\"015ffaec-9340-42f8-b163-a0c5ab7d0611\"/>\n",
|
"<head>\n",
|
||||||
"<meta name=\"dtb:depth\" content=\"2\"/>\n",
|
"<meta name=\"dtb:uid\" content=\"015ffaec-9340-42f8-b163-a0c5ab7d0611\"/>\n",
|
||||||
"<meta name=\"dtb:totalPageCount\" content=\"0\"/>\n",
|
"<meta name=\"dtb:depth\" content=\"2\"/>\n",
|
||||||
"<meta name=\"dtb:maxPageNumber\" content=\"0\"/>\n",
|
"<meta name=\"dtb:totalPageCount\" content=\"0\"/>\n",
|
||||||
"</head>\n",
|
"<meta name=\"dtb:maxPageNumber\" content=\"0\"/>\n",
|
||||||
"<docTitle><text>",title,"</text></docTitle>\n",
|
"</head>\n",
|
||||||
"<navMap>"
|
"<docTitle><text>", title, "</text></docTitle>\n",
|
||||||
])
|
"<navMap>"
|
||||||
|
])
|
||||||
for chapter in chapters:
|
for chapter in chapters:
|
||||||
folder = chapter[0].replace(os.path.join(dstdir,'OEBPS'),'').lstrip('/').lstrip('\\\\')
|
folder = chapter[0].replace(os.path.join(dstdir, 'OEBPS'), '').lstrip('/').lstrip('\\\\')
|
||||||
title = os.path.basename(folder)
|
title = os.path.basename(folder)
|
||||||
filename = getImageFileName(os.path.join(folder,chapter[1]))
|
filename = getImageFileName(os.path.join(folder, chapter[1]))
|
||||||
f.write("<navPoint id=\"" + folder.replace('/','_') + "\"><navLabel><text>" + title
|
f.write("<navPoint id=\"" + folder.replace('/', '_') + "\"><navLabel><text>" + title
|
||||||
+ "</text></navLabel><content src=\"" + filename[0] + ".html\"/></navPoint>\n")
|
+ "</text></navLabel><content src=\"" + filename[0] + ".html\"/></navPoint>\n")
|
||||||
f.write("</navMap>\n</ncx>")
|
f.write("</navMap>\n</ncx>")
|
||||||
f.close()
|
f.close()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False):
|
def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False):
|
||||||
opffile = os.path.join(dstdir,'OEBPS','content.opf')
|
opffile = os.path.join(dstdir, 'OEBPS', 'content.opf')
|
||||||
# read the first file resolution
|
# read the first file resolution
|
||||||
profilelabel, deviceres, palette = image.ProfileData.Profiles[profile]
|
profilelabel, deviceres, palette = image.ProfileData.Profiles[profile]
|
||||||
imgres = str(deviceres[0]) + "x" + str(deviceres[1])
|
imgres = str(deviceres[0]) + "x" + str(deviceres[1])
|
||||||
@@ -94,24 +125,28 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False):
|
|||||||
writingmode = "horizontal-lr"
|
writingmode = "horizontal-lr"
|
||||||
f = open(opffile, "w")
|
f = open(opffile, "w")
|
||||||
f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
|
f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
|
||||||
"<package version=\"2.0\" unique-identifier=\"BookID\" xmlns=\"http://www.idpf.org/2007/opf\">\n",
|
"<package version=\"2.0\" unique-identifier=\"BookID\" xmlns=\"http://www.idpf.org/2007/opf\">\n",
|
||||||
"<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:opf=\"http://www.idpf.org/2007/opf\">\n",
|
"<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" ",
|
||||||
"<dc:title>",title,"</dc:title>\n",
|
"xmlns:opf=\"http://www.idpf.org/2007/opf\">\n",
|
||||||
"<dc:language>en-US</dc:language>\n",
|
"<dc:title>", title, "</dc:title>\n",
|
||||||
"<dc:identifier id=\"BookID\" opf:scheme=\"UUID\">015ffaec-9340-42f8-b163-a0c5ab7d0611</dc:identifier>\n",
|
"<dc:language>en-US</dc:language>\n",
|
||||||
"<meta name=\"cover\" content=\"cover\"/>\n",
|
"<dc:identifier id=\"BookID\" opf:scheme=\"UUID\">",
|
||||||
"<meta name=\"book-type\" content=\"comic\"/>\n",
|
"015ffaec-9340-42f8-b163-a0c5ab7d0611</dc:identifier>\n",
|
||||||
"<meta name=\"zero-gutter\" content=\"true\"/>\n",
|
"<meta name=\"RegionMagnification\" content=\"true\"/>\n",
|
||||||
"<meta name=\"zero-margin\" content=\"true\"/>\n",
|
"<meta name=\"cover\" content=\"cover\"/>\n",
|
||||||
"<meta name=\"fixed-layout\" content=\"true\"/>\n",
|
"<meta name=\"book-type\" content=\"comic\"/>\n",
|
||||||
"<meta name=\"orientation-lock\" content=\"portrait\"/>\n",
|
"<meta name=\"zero-gutter\" content=\"true\"/>\n",
|
||||||
"<meta name=\"original-resolution\" content=\"" + imgres + "\"/>\n",
|
"<meta name=\"zero-margin\" content=\"true\"/>\n",
|
||||||
"<meta name=\"primary-writing-mode\" content=\"" + writingmode + "\"/>\n",
|
"<meta name=\"fixed-layout\" content=\"true\"/>\n",
|
||||||
"</metadata>\n<manifest>\n<item id=\"ncx\" href=\"toc.ncx\" media-type=\"application/x-dtbncx+xml\"/>\n"
|
"<meta name=\"orientation-lock\" content=\"portrait\"/>\n",
|
||||||
])
|
"<meta name=\"original-resolution\" content=\"" + imgres + "\"/>\n",
|
||||||
|
"<meta name=\"primary-writing-mode\" content=\"" + writingmode + "\"/>\n",
|
||||||
|
"</metadata>\n<manifest>\n<item id=\"ncx\" href=\"toc.ncx\" ",
|
||||||
|
"media-type=\"application/x-dtbncx+xml\"/>\n"
|
||||||
|
])
|
||||||
# set cover
|
# set cover
|
||||||
if cover is not None:
|
if cover is not None:
|
||||||
filename = getImageFileName(cover.replace(os.path.join(dstdir,'OEBPS'),'').lstrip('/').lstrip('\\\\'))
|
filename = getImageFileName(cover.replace(os.path.join(dstdir, 'OEBPS'), '').lstrip('/').lstrip('\\\\'))
|
||||||
if '.png' == filename[1]:
|
if '.png' == filename[1]:
|
||||||
mt = 'image/png'
|
mt = 'image/png'
|
||||||
else:
|
else:
|
||||||
@@ -119,57 +154,65 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False):
|
|||||||
f.write("<item id=\"cover\" href=\"" + filename[0] + filename[1] + "\" media-type=\"" + mt + "\"/>\n")
|
f.write("<item id=\"cover\" href=\"" + filename[0] + filename[1] + "\" media-type=\"" + mt + "\"/>\n")
|
||||||
reflist = []
|
reflist = []
|
||||||
for path in filelist:
|
for path in filelist:
|
||||||
folder = path[0].replace(os.path.join(dstdir,'OEBPS'),'').lstrip('/').lstrip('\\\\')
|
folder = path[0].replace(os.path.join(dstdir, 'OEBPS'), '').lstrip('/').lstrip('\\\\')
|
||||||
filename = getImageFileName(path[1])
|
filename = getImageFileName(path[1])
|
||||||
uniqueid = os.path.join(folder,filename[0]).replace('/','_')
|
uniqueid = os.path.join(folder, filename[0]).replace('/', '_')
|
||||||
reflist.append(uniqueid)
|
reflist.append(uniqueid)
|
||||||
f.write("<item id=\"page_" + uniqueid + "\" href=\"" + os.path.join(folder.replace('Images','Text'),filename[0])
|
f.write("<item id=\"page_" + uniqueid + "\" href=\""
|
||||||
|
+ os.path.join(folder.replace('Images', 'Text'), filename[0])
|
||||||
+ ".html\" media-type=\"application/xhtml+xml\"/>\n")
|
+ ".html\" media-type=\"application/xhtml+xml\"/>\n")
|
||||||
if '.png' == filename[1]:
|
if '.png' == filename[1]:
|
||||||
mt = 'image/png'
|
mt = 'image/png'
|
||||||
else:
|
else:
|
||||||
mt = 'image/jpeg'
|
mt = 'image/jpeg'
|
||||||
f.write("<item id=\"img_" + uniqueid + "\" href=\"" + os.path.join(folder,path[1]) + "\" media-type=\"" + mt + "\"/>\n")
|
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")
|
f.write("</manifest>\n<spine toc=\"ncx\">\n")
|
||||||
for entry in reflist:
|
for entry in reflist:
|
||||||
f.write("<itemref idref=\"page_" + entry + "\" />\n")
|
f.write("<itemref idref=\"page_" + entry + "\" />\n")
|
||||||
f.write("</spine>\n<guide>\n</guide>\n</package>\n")
|
f.write("</spine>\n<guide>\n</guide>\n</package>\n")
|
||||||
f.close()
|
f.close()
|
||||||
# finish with standard ePub folders
|
# finish with standard ePub folders
|
||||||
os.mkdir(os.path.join(dstdir,'META-INF'))
|
os.mkdir(os.path.join(dstdir, 'META-INF'))
|
||||||
f = open(os.path.join(dstdir,'mimetype'), 'w')
|
f = open(os.path.join(dstdir, 'mimetype'), 'w')
|
||||||
f.write('application/epub+zip')
|
f.write('application/epub+zip')
|
||||||
f.close()
|
f.close()
|
||||||
f = open(os.path.join(dstdir,'META-INF','container.xml'), 'w')
|
f = open(os.path.join(dstdir, 'META-INF', 'container.xml'), 'w')
|
||||||
f.writelines(["<?xml version=\"1.0\"?>\n",
|
f.writelines(["<?xml version=\"1.0\"?>\n",
|
||||||
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n",
|
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n",
|
||||||
"<rootfiles>\n",
|
"<rootfiles>\n",
|
||||||
"<rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/>\n",
|
"<rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/>\n",
|
||||||
"</rootfiles>\n",
|
"</rootfiles>\n",
|
||||||
"</container>"])
|
"</container>"])
|
||||||
f.close()
|
f.close()
|
||||||
return
|
return
|
||||||
|
|
||||||
def getImageFileName(file):
|
|
||||||
filename = os.path.splitext(file)
|
def getImageFileName(imgfile):
|
||||||
if filename[0].startswith('.') or (filename[1].lower() != '.png' and filename[1].lower() != '.jpg' and filename[1].lower() != '.jpeg'):
|
filename = os.path.splitext(imgfile)
|
||||||
|
if filename[0].startswith('.') or\
|
||||||
|
(filename[1].lower() != '.png' and
|
||||||
|
filename[1].lower() != '.jpg' and
|
||||||
|
filename[1].lower() != '.jpeg'):
|
||||||
return None
|
return None
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
def isInFilelist(file,list):
|
|
||||||
filename = os.path.splitext(file)
|
def isInFilelist(filename, filelist):
|
||||||
|
filename = os.path.splitext(filename)
|
||||||
seen = False
|
seen = False
|
||||||
for item in list:
|
for item in filelist:
|
||||||
if filename[0] == item[0]:
|
if filename[0] == item[0]:
|
||||||
seen = True
|
seen = True
|
||||||
return seen
|
return seen
|
||||||
|
|
||||||
|
|
||||||
def applyImgOptimization(img):
|
def applyImgOptimization(img):
|
||||||
img.optimizeImage()
|
img.optimizeImage()
|
||||||
img.cropWhiteSpace(10.0)
|
img.cropWhiteSpace(10.0)
|
||||||
if options.cutpagenumbers:
|
if options.cutpagenumbers:
|
||||||
img.cutPageNumber()
|
img.cutPageNumber()
|
||||||
img.resizeImage(options.upscale,options.stretch,options.black_borders)
|
img.resizeImage(options.upscale, options.stretch, options.black_borders)
|
||||||
img.quantizeImage()
|
img.quantizeImage()
|
||||||
|
|
||||||
|
|
||||||
@@ -177,91 +220,96 @@ def dirImgProcess(path):
|
|||||||
global options
|
global options
|
||||||
|
|
||||||
for (dirpath, dirnames, filenames) in os.walk(path):
|
for (dirpath, dirnames, filenames) in os.walk(path):
|
||||||
for file in filenames:
|
for afile in filenames:
|
||||||
if getImageFileName(file) is not None:
|
if getImageFileName(afile) is not None:
|
||||||
if options.verbose:
|
if options.verbose:
|
||||||
print "Optimizing " + file + " for " + options.profile
|
print "Optimizing " + afile + " for " + options.profile
|
||||||
else:
|
else:
|
||||||
print ".",
|
print ".",
|
||||||
img = image.ComicPage(os.path.join(dirpath,file), options.profile)
|
img = image.ComicPage(os.path.join(dirpath, afile), options.profile)
|
||||||
split = img.splitPage(dirpath, options.righttoleft)
|
split = img.splitPage(dirpath, options.righttoleft)
|
||||||
if split is not None:
|
if split is not None:
|
||||||
if options.verbose:
|
if options.verbose:
|
||||||
print "Splitted " + file
|
print "Splitted " + afile
|
||||||
img0 = image.ComicPage(split[0],options.profile)
|
img0 = image.ComicPage(split[0], options.profile)
|
||||||
applyImgOptimization(img0)
|
applyImgOptimization(img0)
|
||||||
img0.saveToDir(dirpath)
|
img0.saveToDir(dirpath)
|
||||||
img1 = image.ComicPage(split[1],options.profile)
|
img1 = image.ComicPage(split[1], options.profile)
|
||||||
applyImgOptimization(img1)
|
applyImgOptimization(img1)
|
||||||
img1.saveToDir(dirpath)
|
img1.saveToDir(dirpath)
|
||||||
else:
|
else:
|
||||||
applyImgOptimization(img)
|
applyImgOptimization(img)
|
||||||
img.saveToDir(dirpath)
|
img.saveToDir(dirpath)
|
||||||
|
|
||||||
|
|
||||||
def genEpubStruct(path):
|
def genEpubStruct(path):
|
||||||
global options
|
global options
|
||||||
filelist = []
|
filelist = []
|
||||||
chapterlist = []
|
chapterlist = []
|
||||||
cover = None
|
cover = None
|
||||||
os.mkdir(os.path.join(path,'OEBPS','Text'))
|
os.mkdir(os.path.join(path, 'OEBPS', 'Text'))
|
||||||
for (dirpath, dirnames, filenames) in os.walk(os.path.join(path,'OEBPS','Images')):
|
for (dirpath, dirnames, filenames) in os.walk(os.path.join(path, 'OEBPS', 'Images')):
|
||||||
chapter = False
|
chapter = False
|
||||||
for file in filenames:
|
for afile in filenames:
|
||||||
filename = getImageFileName(file)
|
filename = getImageFileName(afile)
|
||||||
if filename is not None:
|
if filename is not None:
|
||||||
# put credits at the end
|
# put credits at the end
|
||||||
if "credit" in file.lower():
|
if "credit" in afile.lower():
|
||||||
os.rename(os.path.join(dirpath,file), os.path.join(dirpath,'ZZZ999_'+file))
|
os.rename(os.path.join(dirpath, afile), os.path.join(dirpath, 'ZZZ999_' + afile))
|
||||||
file = 'ZZZ999_'+file
|
afile = 'ZZZ999_' + afile
|
||||||
if "+" in file.lower() or "#" in file.lower():
|
if "+" in afile.lower() or "#" in afile.lower():
|
||||||
newfilename = file.replace('+','_').replace('#','_')
|
newfilename = afile.replace('+', '_').replace('#', '_')
|
||||||
os.rename(os.path.join(dirpath,file), os.path.join(dirpath,newfilename))
|
os.rename(os.path.join(dirpath, afile), os.path.join(dirpath, newfilename))
|
||||||
file = newfilename
|
afile = newfilename
|
||||||
filelist.append(buildHTML(dirpath,file))
|
filelist.append(buildHTML(dirpath, afile))
|
||||||
if not chapter:
|
if not chapter:
|
||||||
chapterlist.append((dirpath.replace('Images','Text'),filelist[-1][1]))
|
chapterlist.append((dirpath.replace('Images', 'Text'), filelist[-1][1]))
|
||||||
chapter = True
|
chapter = True
|
||||||
if cover is None:
|
if cover is None:
|
||||||
cover = os.path.join(filelist[-1][0],'cover' + getImageFileName(filelist[-1][1])[1])
|
cover = os.path.join(filelist[-1][0], 'cover' + getImageFileName(filelist[-1][1])[1])
|
||||||
copyfile(os.path.join(filelist[-1][0],filelist[-1][1]), cover)
|
copyfile(os.path.join(filelist[-1][0], filelist[-1][1]), cover)
|
||||||
buildNCX(path,options.title,chapterlist)
|
buildNCX(path, options.title, chapterlist)
|
||||||
# ensure we're sorting files alphabetically
|
# ensure we're sorting files alphabetically
|
||||||
filelist = sorted(filelist, key=lambda name: (name[0].lower(), name[1].lower()))
|
filelist = sorted(filelist, key=lambda name: (name[0].lower(), name[1].lower()))
|
||||||
buildOPF(options.profile,path,options.title,filelist,cover,options.righttoleft)
|
buildOPF(options.profile, path, options.title, filelist, cover, options.righttoleft)
|
||||||
|
|
||||||
def getWorkFolder(file):
|
|
||||||
|
def getWorkFolder(afile):
|
||||||
workdir = tempfile.mkdtemp()
|
workdir = tempfile.mkdtemp()
|
||||||
fname = os.path.splitext(file)
|
fname = os.path.splitext(afile)
|
||||||
if os.path.isdir(file):
|
if os.path.isdir(afile):
|
||||||
try:
|
try:
|
||||||
import shutil
|
import shutil
|
||||||
os.rmdir(workdir) # needed for copytree() fails if dst already exists
|
os.rmdir(workdir) # needed for copytree() fails if dst already exists
|
||||||
copytree(file, workdir)
|
copytree(afile, workdir)
|
||||||
path = workdir
|
path = workdir
|
||||||
except OSError:
|
except OSError:
|
||||||
raise
|
raise
|
||||||
elif fname[1].lower() == '.pdf':
|
elif fname[1].lower() == '.pdf':
|
||||||
pdf = pdfjpgextract.PdfJpgExtract(file)
|
pdf = pdfjpgextract.PdfJpgExtract(afile)
|
||||||
path = pdf.extract(workdir)
|
path = pdf.extract()
|
||||||
else:
|
else:
|
||||||
cbx = cbxarchive.CBxArchive(file)
|
cbx = cbxarchive.CBxArchive(afile)
|
||||||
if cbx.isCbxFile():
|
if cbx.isCbxFile():
|
||||||
path = cbx.extract(workdir)
|
path = cbx.extract(workdir)
|
||||||
else:
|
else:
|
||||||
raise TypeError
|
raise TypeError
|
||||||
move(path,path + "_temp")
|
move(path, path + "_temp")
|
||||||
move(path + "_temp",os.path.join(path,'OEBPS','Images'))
|
move(path + "_temp", os.path.join(path, 'OEBPS', 'Images'))
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
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())
|
||||||
|
|
||||||
|
|
||||||
def Usage():
|
def Usage():
|
||||||
print "Generates HTML, NCX and OPF for a Comic ebook from a bunch of images"
|
print "Generates HTML, NCX and OPF for a Comic ebook from a bunch of images"
|
||||||
print "Optimized for creating Mobipockets to be read into Kindle Paperwhite"
|
print "Optimized for creating Mobipockets to be read into Kindle Paperwhite"
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
|
|
||||||
|
|
||||||
def main(argv=None):
|
def main(argv=None):
|
||||||
global parser, options, epub_path
|
global parser, options, epub_path
|
||||||
usage = "Usage: %prog [options] comic_file|comic_folder"
|
usage = "Usage: %prog [options] comic_file|comic_folder"
|
||||||
@@ -275,15 +323,16 @@ def main(argv=None):
|
|||||||
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
|
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
|
||||||
help="Verbose output [default=False]")
|
help="Verbose output [default=False]")
|
||||||
parser.add_option("--no-image-processing", action="store_false", dest="imgproc", default=True,
|
parser.add_option("--no-image-processing", action="store_false", dest="imgproc", default=True,
|
||||||
help="Do not apply image preprocessing (page splitting and optimizations) [default=True]")
|
help="Do not apply image preprocessing (page splitting and optimizations) [default=True]")
|
||||||
parser.add_option("--upscale-images", action="store_true", dest="upscale", default=False,
|
parser.add_option("--upscale-images", action="store_true", dest="upscale", default=False,
|
||||||
help="Resize images smaller than device's resolution [default=False]")
|
help="Resize images smaller than device's resolution [default=False]")
|
||||||
parser.add_option("--stretch-images", action="store_true", dest="stretch", default=False,
|
parser.add_option("--stretch-images", action="store_true", dest="stretch", default=False,
|
||||||
help="Stretch images to device's resolution [default=False]")
|
help="Stretch images to device's resolution [default=False]")
|
||||||
parser.add_option("--black-borders", action="store_true", dest="black_borders", default=False,
|
parser.add_option("--black-borders", action="store_true", dest="black_borders", default=False,
|
||||||
help="Use black borders (instead of white ones) when not stretching and ratio is not like the device's one [default=False]")
|
help="Use black borders (instead of white ones) when not stretching and ratio "
|
||||||
|
+ "is not like the device's one [default=False]")
|
||||||
parser.add_option("--no-cut-page-numbers", action="store_false", dest="cutpagenumbers", default=True,
|
parser.add_option("--no-cut-page-numbers", action="store_false", dest="cutpagenumbers", default=True,
|
||||||
help="Do not try to cut page numbering on images [default=True]")
|
help="Do not try to cut page numbering on images [default=True]")
|
||||||
options, args = parser.parse_args(argv)
|
options, args = parser.parse_args(argv)
|
||||||
if len(args) != 1:
|
if len(args) != 1:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
@@ -301,11 +350,12 @@ def main(argv=None):
|
|||||||
epubpath = args[0] + '.epub'
|
epubpath = args[0] + '.epub'
|
||||||
else:
|
else:
|
||||||
epubpath = os.path.splitext(args[0])[0] + '.epub'
|
epubpath = os.path.splitext(args[0])[0] + '.epub'
|
||||||
make_archive(path + '_comic','zip',path)
|
make_archive(path + '_comic', 'zip', path)
|
||||||
move(path + '_comic.zip', epubpath)
|
move(path + '_comic.zip', epubpath)
|
||||||
rmtree(path)
|
rmtree(path)
|
||||||
return epubpath
|
return epubpath
|
||||||
|
|
||||||
|
|
||||||
def getEpubPath():
|
def getEpubPath():
|
||||||
global epub_path
|
global epub_path
|
||||||
return epub_path
|
return epub_path
|
||||||
|
|||||||
Reference in New Issue
Block a user