1
0
mirror of https://github.com/ciromattia/kcc synced 2026-05-25 08:52:12 +00:00

Fixed an issue in OPF generation for device resolution (fixes #4)

Reworked options system (call with -h option to get the inline help) (fixes #3)
This commit is contained in:
Ciro Mattia Gonano
2013-01-10 13:03:13 +01:00
parent d24c88d3c2
commit 988a357555
11 changed files with 139 additions and 89 deletions

View File

@@ -24,6 +24,8 @@
# with portrait target or portrait with landscape target), add palette
# and other image optimizations from Mangle.
# WARNING: PIL is required for all image mangling!
# 1.30 - Fixed an issue in OPF generation for device resolution
# Reworked options system (call with -h option to get the inline help)
#
# Todo:
# - Add gracefully exit for CBR if no rarfile.py and no unrar
@@ -31,10 +33,11 @@
# - Improve error reporting
# - recurse into dirtree for multiple comics
__version__ = '1.20'
__version__ = '1.30'
import os
import sys
from optparse import OptionParser
import image, cbxarchive
class HTMLbuilder:
@@ -77,17 +80,11 @@ class NCXbuilder:
return
class OPFBuilder:
def __init__(self, dstdir, title, filelist):
def __init__(self, profile, dstdir, title, filelist):
opffile = dstdir + '/content.opf'
# read the first file resolution
try:
from PIL import Image
im = Image.open(dstdir + "/" + filelist[0][0] + filelist[0][1])
width, height = im.size
imgres = str(width) + "x" + str(height)
except ImportError:
print "Could not load PIL, falling back on default HD"
imgres = "758x1024"
deviceres, palette = image.ProfileData.Profiles[profile]
imgres = str(deviceres[0]) + "x" + str(deviceres[1])
f = open(opffile, "w");
f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
"<package version=\"2.0\" unique-identifier=\"PrimaryID\" xmlns=\"http://www.idpf.org/2007/opf\">\n",
@@ -137,34 +134,41 @@ def Copyright():
def Usage():
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 "Usage:"
print " %s <profile> <dir> <title>" % sys.argv[0]
print " <title> is optional"
#print "Usage:"
#print " %s <profile> <dir> <title>" % sys.argv[0]
#print " <title> is optional"
parser.print_help()
def main(argv=None):
if argv is None:
argv = sys.argv
profile = argv[1]
dir = argv[2]
global parser
usage = "Usage: %prog [options] comic_file|comic_folder"
parser = OptionParser(usage=usage, version=__version__)
parser.add_option("-p", "--profile", action="store", dest="profile", default="KHD",
help="Device profile (choose one among K1, K2, K3, K4, KHD [default])")
parser.add_option("-t", "--title", action="store", dest="title", default="comic",
help="Comic title")
parser.add_option("-m", "--manga-style", action="store_true", dest="righttoleft", default=False,
help="Split pages 'manga style' (right-to-left reading)")
options, args = parser.parse_args()
if len(args) != 1:
parser.print_help()
sys.exit(1)
dir = args[0]
cbx = cbxarchive.CBxArchive(dir)
if cbx.isCbxFile():
cbx.extract()
dir = cbx.getPath()
if len(argv)==4:
title = argv[3]
else:
title = "comic"
filelist = []
try:
print "Splitting double pages..."
for file in os.listdir(dir):
if (getImageFileName(file) != None):
img = image.ComicPage(dir+'/'+file, profile)
img.splitPage(dir)
img = image.ComicPage(dir+'/'+file, options.profile)
img.splitPage(dir, options.righttoleft)
for file in os.listdir(dir):
if (getImageFileName(file) != None):
print "Optimizing " + file + " for " + profile
img = image.ComicPage(dir+'/'+file, profile)
print "Optimizing " + file + " for " + options.profile
img = image.ComicPage(dir+'/'+file, options.profile)
img.resizeImage()
#img.frameImage()
img.quantizeImage()
@@ -181,16 +185,12 @@ def main(argv=None):
filename = HTMLbuilder(dir,file).getResult()
if (filename != None):
filelist.append(filename)
NCXbuilder(dir,title)
NCXbuilder(dir,options.title)
# ensure we're sorting files alphabetically
filelist = sorted(filelist, key=lambda name: name[0])
OPFBuilder(dir,title,filelist)
OPFBuilder(options.profile,dir,options.title,filelist)
if __name__ == "__main__":
Copyright()
if len(sys.argv)<3 or len(sys.argv)>4:
Usage()
sys.exit(1)
else:
main()
main()
sys.exit(0)

43
kcc/gui.py Normal file
View File

@@ -0,0 +1,43 @@
#!/usr/bin/env python
#
# Copyright (c) 2013 Ciro Mattia Gonano <ciromattia@gmail.com>
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all
# copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
# OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
from Tkinter import *
import tkFileDialog
class MainWindow(Frame):
def open_files(self):
self.files = tkFileDialog.askopenfilename()
def createWidgets(self):
self.QUIT = Button(self)
self.QUIT["text"] = "Quit"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit
self.QUIT.pack({"side": "right"})
self.OPENFILES = Button(self)
self.OPENFILES["text"] = "Open files",
self.OPENFILES["command"] = self.open_files
self.OPENFILES.pack({"side": "left"})
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()

View File

@@ -24,7 +24,7 @@ class ImageFlags:
Stretch = 1 << 4
class KindleData:
class ProfileData:
Palette4 = [
0x00, 0x00, 0x00,
0x55, 0x55, 0x55,
@@ -80,7 +80,7 @@ class KindleData:
class ComicPage:
def __init__(self,source,device):
try:
self.size, self.palette = KindleData.Profiles[device]
self.size, self.palette = ProfileData.Profiles[device]
except KeyError:
raise RuntimeError('Unexpected output device %s' % device)
try: