mirror of
https://github.com/ciromattia/kcc
synced 2025-12-27 00:22:54 +00:00
Merge from lanscape branch
This commit is contained in:
@@ -81,6 +81,22 @@ def buildHTML(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):
|
||||
ncxfile = os.path.join(dstdir, 'OEBPS', 'toc.ncx')
|
||||
f = open(ncxfile, "w")
|
||||
@@ -115,8 +131,14 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False):
|
||||
imgres = str(deviceres[0]) + "x" + str(deviceres[1])
|
||||
if righttoleft:
|
||||
writingmode = "horizontal-rl"
|
||||
facing = "right"
|
||||
facing1 = "right"
|
||||
facing2 = "left"
|
||||
else:
|
||||
writingmode = "horizontal-lr"
|
||||
facing = "left"
|
||||
facing1 = "left"
|
||||
facing2 = "right"
|
||||
from uuid import uuid4
|
||||
uuid = str(uuid4())
|
||||
uuid = uuid.encode('utf-8')
|
||||
@@ -134,11 +156,11 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False):
|
||||
"<meta name=\"zero-gutter\" content=\"true\"/>\n",
|
||||
"<meta name=\"zero-margin\" 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=\"primary-writing-mode\" content=\"", writingmode, "\"/>\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\" ",
|
||||
"media-type=\"application/x-dtbncx+xml\"/>\n"
|
||||
])
|
||||
@@ -165,9 +187,27 @@ def buildOPF(profile, dstdir, title, filelist, cover=None, righttoleft=False):
|
||||
mt = 'image/jpeg'
|
||||
f.write("<item id=\"img_" + uniqueid + "\" href=\"" + os.path.join(folder, path[1]) + "\" media-type=\""
|
||||
+ 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")
|
||||
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.close()
|
||||
# finish with standard ePub folders
|
||||
@@ -205,17 +245,19 @@ def isInFilelist(filename, filelist):
|
||||
return seen
|
||||
|
||||
|
||||
def applyImgOptimization(img):
|
||||
def applyImgOptimization(img, isSplit=False, toRight=False):
|
||||
img.optimizeImage()
|
||||
img.cropWhiteSpace(10.0)
|
||||
if options.cutpagenumbers:
|
||||
img.cutPageNumber()
|
||||
img.resizeImage(options.upscale, options.stretch, options.black_borders)
|
||||
img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight)
|
||||
img.quantizeImage()
|
||||
|
||||
|
||||
def dirImgProcess(path):
|
||||
global options
|
||||
global splittedSomething
|
||||
splittedSomething = False
|
||||
|
||||
for (dirpath, dirnames, filenames) in os.walk(path):
|
||||
for afile in filenames:
|
||||
@@ -227,13 +269,20 @@ def dirImgProcess(path):
|
||||
img = image.ComicPage(os.path.join(dirpath, afile), options.profile)
|
||||
split = img.splitPage(dirpath, options.righttoleft, options.rotate)
|
||||
if split is not None:
|
||||
splittedSomething = True
|
||||
if options.verbose:
|
||||
print "Splitted " + afile
|
||||
if options.righttoleft:
|
||||
toRight1 = False
|
||||
toRight2 = True
|
||||
else:
|
||||
toRight1 = True
|
||||
toRight2 = False
|
||||
img0 = image.ComicPage(split[0], options.profile)
|
||||
applyImgOptimization(img0)
|
||||
applyImgOptimization(img0, True, toRight1)
|
||||
img0.saveToDir(dirpath)
|
||||
img1 = image.ComicPage(split[1], options.profile)
|
||||
applyImgOptimization(img1)
|
||||
applyImgOptimization(img1, True, toRight2)
|
||||
img1.saveToDir(dirpath)
|
||||
else:
|
||||
applyImgOptimization(img)
|
||||
@@ -272,6 +321,8 @@ def genEpubStruct(path):
|
||||
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())))
|
||||
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):
|
||||
|
||||
25
kcc/image.py
25
kcc/image.py
@@ -85,7 +85,7 @@ class ProfileData:
|
||||
'KDX': ("Kindle DX", (824, 1200), Palette15),
|
||||
'KDXG': ("Kindle DXG", (824, 1200), Palette16)
|
||||
}
|
||||
|
||||
|
||||
ProfileLabels = {
|
||||
"Kindle": 'K1',
|
||||
"Kindle 2": 'K2',
|
||||
@@ -100,6 +100,7 @@ class ProfileData:
|
||||
class ComicPage:
|
||||
def __init__(self, source, device):
|
||||
try:
|
||||
self.profile = device
|
||||
self.profile_label, self.size, self.palette = ProfileData.Profiles[device]
|
||||
except KeyError:
|
||||
raise RuntimeError('Unexpected output device %s' % device)
|
||||
@@ -129,7 +130,7 @@ class ComicPage:
|
||||
palImg.putpalette(self.palette)
|
||||
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
|
||||
if black_borders:
|
||||
fill = 'black'
|
||||
@@ -137,10 +138,20 @@ class ComicPage:
|
||||
fill = 'white'
|
||||
if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]:
|
||||
if not upscale:
|
||||
# do not upscale but center image in a device-sized image
|
||||
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)
|
||||
if isSplit and (self.profile == 'K4' or self.profile == 'KHD'):
|
||||
borderw = (self.size[0] - self.image.size[0])
|
||||
borderh = (self.size[1] - self.image.size[1]) / 2
|
||||
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
|
||||
else:
|
||||
method = Image.NEAREST
|
||||
@@ -152,6 +163,8 @@ class ComicPage:
|
||||
ratioDev = float(self.size[0]) / float(self.size[1])
|
||||
if (float(self.image.size[0]) / float(self.image.size[1])) < ratioDev:
|
||||
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)
|
||||
elif (float(self.image.size[0]) / float(self.image.size[1])) > ratioDev:
|
||||
diff = int(self.image.size[0] / ratioDev) - self.image.size[1]
|
||||
|
||||
Reference in New Issue
Block a user