1
0
mirror of https://github.com/ciromattia/kcc synced 2026-07-05 04:25:31 +00:00

Merge pull request #24 from ciromattia/kcc-landscape

KCC Landscape
This commit is contained in:
Ciro Mattia Gonano
2013-03-05 02:55:03 -08:00
3 changed files with 78 additions and 14 deletions
+58 -7
View File
@@ -87,6 +87,22 @@ def buildHTML(path, imgfile):
return path, imgfile return path, imgfile
def buildBlankHTML(path):
f = open(os.path.join(path, 'blank.html'), "w")
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",
"<head>\n",
"<title></title>\n",
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n",
"</head>\n",
"<body>\n",
"</body>\n",
"</html>"])
f.close()
return path
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")
@@ -121,8 +137,14 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False):
imgres = str(deviceres[0]) + "x" + str(deviceres[1]) imgres = str(deviceres[0]) + "x" + str(deviceres[1])
if righttoleft: if righttoleft:
writingmode = "horizontal-rl" writingmode = "horizontal-rl"
facing = "right"
facing1 = "right"
facing2 = "left"
else: else:
writingmode = "horizontal-lr" writingmode = "horizontal-lr"
facing = "left"
facing1 = "left"
facing2 = "right"
from uuid import uuid4 from uuid import uuid4
uuid = str(uuid4()) uuid = str(uuid4())
uuid = uuid.encode('utf-8') uuid = uuid.encode('utf-8')
@@ -140,11 +162,11 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False):
"<meta name=\"zero-gutter\" content=\"true\"/>\n", "<meta name=\"zero-gutter\" content=\"true\"/>\n",
"<meta name=\"zero-margin\" content=\"true\"/>\n", "<meta name=\"zero-margin\" content=\"true\"/>\n",
"<meta name=\"fixed-layout\" content=\"true\"/>\n", "<meta name=\"fixed-layout\" content=\"true\"/>\n",
"<meta name=\"orientation-lock\" content=\"portrait\"/>\n", "<meta name=\"orientation-lock\" content=\"none\"/>\n",
"<meta name=\"original-resolution\" content=\"", imgres, "\"/>\n", "<meta name=\"original-resolution\" content=\"", imgres, "\"/>\n",
"<meta name=\"primary-writing-mode\" content=\"", writingmode, "\"/>\n", "<meta name=\"primary-writing-mode\" content=\"", writingmode, "\"/>\n",
"<meta name=\"rendition:layout\" content=\"pre-paginated\"/>\n", "<meta name=\"rendition:layout\" content=\"pre-paginated\"/>\n",
"<meta name=\"rendition:orientation\" content=\"portrait\"/>\n", "<meta name=\"rendition:orientation\" content=\"auto\"/>\n",
"</metadata>\n<manifest>\n<item id=\"ncx\" href=\"toc.ncx\" ", "</metadata>\n<manifest>\n<item id=\"ncx\" href=\"toc.ncx\" ",
"media-type=\"application/x-dtbncx+xml\"/>\n" "media-type=\"application/x-dtbncx+xml\"/>\n"
]) ])
@@ -171,9 +193,27 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False):
mt = 'image/jpeg' mt = 'image/jpeg'
f.write("<item id=\"img_" + uniqueid + "\" href=\"" + os.path.join(folder, path[1]) + "\" media-type=\"" f.write("<item id=\"img_" + uniqueid + "\" href=\"" + os.path.join(folder, path[1]) + "\" media-type=\""
+ mt + "\"/>\n") + mt + "\"/>\n")
if (options.profile == 'K4' or options.profile == 'KHD') and splittedSomething:
f.write("<item id=\"blank-page\" href=\"Text\\blank.html\" media-type=\"application/xhtml+xml\"/>\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") if entry.endswith("-1"):
if (righttoleft and facing == 'left') or (not righttoleft and facing == 'right') and \
(options.profile == 'K4' or options.profile == 'KHD'):
f.write("<itemref idref=\"blank-page\" properties=\"layout-blank\"/>\n")
f.write("<itemref idref=\"page_" + entry + "\" properties=\"page-spread-" + facing1 + "\"/>\n")
elif entry.endswith("-2"):
f.write("<itemref idref=\"page_" + entry + "\" properties=\"page-spread-" + facing2 + "\"/>\n")
if righttoleft:
facing = "right"
else:
facing = "left"
else:
f.write("<itemref idref=\"page_" + entry + "\" properties=\"page-spread-" + facing + "\"/>\n")
if facing == 'right':
facing = 'left'
else:
facing = 'right'
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
@@ -211,17 +251,19 @@ def isInFilelist(filename, filelist):
return seen return seen
def applyImgOptimization(img): def applyImgOptimization(img, isSplit=False, toRight=False):
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, isSplit, toRight)
img.quantizeImage() img.quantizeImage()
def dirImgProcess(path): def dirImgProcess(path):
global options global options
global splittedSomething
splittedSomething = False
for (dirpath, dirnames, filenames) in os.walk(path): for (dirpath, dirnames, filenames) in os.walk(path):
for afile in filenames: for afile in filenames:
@@ -233,13 +275,20 @@ def dirImgProcess(path):
img = image.ComicPage(os.path.join(dirpath, afile), options.profile) img = image.ComicPage(os.path.join(dirpath, afile), options.profile)
split = img.splitPage(dirpath, options.righttoleft, options.rotate) split = img.splitPage(dirpath, options.righttoleft, options.rotate)
if split is not None: if split is not None:
splittedSomething = True
if options.verbose: if options.verbose:
print "Splitted " + afile print "Splitted " + afile
if options.righttoleft:
toRight1 = False
toRight2 = True
else:
toRight1 = True
toRight2 = False
img0 = image.ComicPage(split[0], options.profile) img0 = image.ComicPage(split[0], options.profile)
applyImgOptimization(img0) applyImgOptimization(img0, True, toRight1)
img0.saveToDir(dirpath) img0.saveToDir(dirpath)
img1 = image.ComicPage(split[1], options.profile) img1 = image.ComicPage(split[1], options.profile)
applyImgOptimization(img1) applyImgOptimization(img1, True, toRight2)
img1.saveToDir(dirpath) img1.saveToDir(dirpath)
else: else:
applyImgOptimization(img) applyImgOptimization(img)
@@ -278,6 +327,8 @@ def genEpubStruct(path):
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)] alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
filelist.sort(key=lambda name: (alphanum_key(name[0].lower()), alphanum_key(name[1].lower()))) filelist.sort(key=lambda name: (alphanum_key(name[0].lower()), alphanum_key(name[1].lower())))
buildOPF(options.profile, path, options.title, filelist, cover, options.righttoleft) buildOPF(options.profile, path, options.title, filelist, cover, options.righttoleft)
if (options.profile == 'K4' or options.profile == 'KHD') and splittedSomething:
filelist.append(buildBlankHTML(os.path.join(path, 'OEBPS', 'Text')))
def getWorkFolder(afile): def getWorkFolder(afile):
+18 -5
View File
@@ -100,6 +100,7 @@ class ProfileData:
class ComicPage: class ComicPage:
def __init__(self, source, device): def __init__(self, source, device):
try: try:
self.profile = device
self.profile_label, self.size, self.palette = ProfileData.Profiles[device] self.profile_label, self.size, self.palette = ProfileData.Profiles[device]
except KeyError: except KeyError:
raise RuntimeError('Unexpected output device %s' % device) raise RuntimeError('Unexpected output device %s' % device)
@@ -129,7 +130,7 @@ class ComicPage:
palImg.putpalette(self.palette) palImg.putpalette(self.palette)
self.image = self.image.quantize(palette=palImg) self.image = self.image.quantize(palette=palImg)
def resizeImage(self, upscale=False, stretch=False, black_borders=False): def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, toRight=False):
method = Image.ANTIALIAS method = Image.ANTIALIAS
if black_borders: if black_borders:
fill = 'black' fill = 'black'
@@ -137,10 +138,20 @@ class ComicPage:
fill = 'white' fill = 'white'
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 if isSplit and (self.profile == 'K4' or self.profile == 'KHD'):
borderw = (self.size[0] - self.image.size[0]) / 2 borderw = (self.size[0] - self.image.size[0])
borderh = (self.size[1] - self.image.size[1]) / 2 borderh = (self.size[1] - self.image.size[1]) / 2
self.image = ImageOps.expand(self.image, border=(borderw, borderh), fill=fill) self.image = ImageOps.expand(self.image, border=(0, borderh), fill=fill)
tempImg = Image.new(self.image.mode, (self.image.size[0] + borderw, self.image.size[1]), fill)
if toRight:
tempImg.paste(self.image, (borderw, 0))
else:
tempImg.paste(self.image, (0, 0))
self.image = tempImg
else:
borderw = (self.size[0] - self.image.size[0]) / 2
borderh = (self.size[1] - self.image.size[1]) / 2
self.image = ImageOps.expand(self.image, border=(borderw, borderh), fill=fill)
return self.image return self.image
else: else:
method = Image.NEAREST method = Image.NEAREST
@@ -152,6 +163,8 @@ class ComicPage:
ratioDev = float(self.size[0]) / float(self.size[1]) ratioDev = float(self.size[0]) / float(self.size[1])
if (float(self.image.size[0]) / float(self.image.size[1])) < ratioDev: if (float(self.image.size[0]) / float(self.image.size[1])) < ratioDev:
diff = int(self.image.size[1] * ratioDev) - self.image.size[0] diff = int(self.image.size[1] * ratioDev) - self.image.size[0]
if isSplit and (self.profile == 'K4' or self.profile == 'KHD'):
diff = 2
self.image = ImageOps.expand(self.image, border=(diff / 2, 0), fill=fill) self.image = ImageOps.expand(self.image, border=(diff / 2, 0), fill=fill)
elif (float(self.image.size[0]) / float(self.image.size[1])) > ratioDev: elif (float(self.image.size[0]) / float(self.image.size[1])) > ratioDev:
diff = int(self.image.size[0] / ratioDev) - self.image.size[1] diff = int(self.image.size[0] / ratioDev) - self.image.size[1]
+1 -1
View File
@@ -57,7 +57,7 @@ setup(
version=VERSION, version=VERSION,
author="Ciro Mattia Gonano", author="Ciro Mattia Gonano",
author_email="ciromattia@gmail.com", author_email="ciromattia@gmail.com",
description=("A tool to convert comics (CBR/CBZ/PDFs/image folders) to Mobipocket."), description="A tool to convert comics (CBR/CBZ/PDFs/image folders) to Mobipocket.",
license = "ISC License (ISCL)", license = "ISC License (ISCL)",
keywords = "kindle comic mobipocket mobi cbz cbr manga", keywords = "kindle comic mobipocket mobi cbz cbr manga",
url = "http://github.com/ciromattia/kcc", url = "http://github.com/ciromattia/kcc",