1
0
mirror of https://github.com/ciromattia/kcc synced 2026-05-30 19:23:06 +00:00

Added a bunch of optimizations (autocontrast, page centering)

This commit is contained in:
Ciro Mattia Gonano
2013-01-17 16:20:32 +01:00
parent df752eb110
commit f231e9112a
4 changed files with 102 additions and 62 deletions
+41 -20
View File
@@ -141,13 +141,21 @@ def main(argv=None):
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, KDX or KHD) [default=KHD]")
help="Device profile (choose one among K1, K2, K3, K4, KDX, KDXG or KHD) [default=KHD]")
parser.add_option("-t", "--title", action="store", dest="title", default="defaulttitle",
help="Comic title [default=filename]")
parser.add_option("-m", "--manga-style", action="store_true", dest="righttoleft", default=False,
help="Split pages 'manga style' (right-to-left reading) [default=False]")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
help="Verbose output [default=False]")
parser.add_option("-i", "--image-processing", action="store_false", dest="imgproc", default=True,
help="Apply image preprocessing (page splitting and optimizations) [default=True]")
parser.add_option("--upscale-images", action="store_true", dest="upscale", default=False,
help="Resize images smaller than device's resolution [default=False]")
parser.add_option("--stretch-images", action="store_true", dest="stretch", default=False,
help="Stretch images to device's resolution [default=False]")
parser.add_option("--cut-page-numbers", action="store_false", dest="cutpagenumbers", default=True,
help="Try to cut page numbering on images [default=True]")
options, args = parser.parse_args(argv)
if len(args) != 1:
parser.print_help()
@@ -163,26 +171,36 @@ def main(argv=None):
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 = []
try:
print "Splitting double pages..."
for file in os.listdir(dir):
if getImageFileName(file) is not None:
img = image.ComicPage(dir+'/'+file, options.profile)
img.splitPage(dir, options.righttoleft)
for file in os.listdir(dir):
if getImageFileName(file) is not None:
print "Optimizing " + file + " for " + options.profile
img = image.ComicPage(dir+'/'+file, options.profile)
img.cutPageNumber()
img.cropWhiteSpace(5.0)
img.resizeImage()
#img.frameImage()
#img.addProgressbar()
img.quantizeImage()
img.saveToDir(dir)
except ImportError:
print "Could not load PIL, not optimizing image"
if options.imgproc:
try:
if options.verbose:
print "Splitting double pages..."
for file in os.listdir(dir):
if getImageFileName(file) is not None:
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
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"
for file in os.listdir(dir):
if getImageFileName(file) is not None and isInFilelist(file,filelist) == False:
@@ -193,11 +211,14 @@ def main(argv=None):
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])
OPFBuilder(options.profile,dir,options.title,filelist)
if __name__ == "__main__":
Copyright()
main(sys.argv[1:])
+1
View File
@@ -95,6 +95,7 @@ class MainWindow:
subargv = list(argv)
subargv.append(entry)
comic2ebook.main(subargv)
print "Done!"
def __init__(self, master, title):
+27 -26
View File
@@ -16,7 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
from PIL import Image, ImageDraw, ImageStat
from PIL import Image, ImageOps, ImageDraw, ImageStat
class ImageFlags:
Orient = 1 << 0
@@ -103,6 +103,9 @@ class ComicPage:
except IOError as e:
raise RuntimeError('Cannot write image in directory %s: %s' %(targetdir,e))
def optimizeImage(self):
self.image = ImageOps.autocontrast(self.image)
def quantizeImage(self):
colors = len(self.palette) / 3
if colors < 256:
@@ -111,33 +114,31 @@ class ComicPage:
palImg.putpalette(self.palette)
self.image = self.image.quantize(palette=palImg)
def stretchImage(self):
widthDev, heightDev = self.size
self.image = self.image.resize((widthDev, heightDev), Image.ANTIALIAS)
def resizeImage(self,upscale=False, stretch=False):
method = Image.ANTIALIAS
if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]:
if not upscale:
return self.image
else:
method = Image.NEAREST
# TODO:
# - add option to stretch page
# - add option to upscale page
# - if ratio is not equal to dev size and stretch is not enabled, add white
# background and center it (otherwise K3 does not display page
# center-aligned but left-aligned)
def resizeImage(self):
widthDev, heightDev = self.size
widthImg, heightImg = self.image.size
if widthImg <= widthDev and heightImg <= heightDev:
if stretch: # if stretching call directly resize() without other considerations.
self.image = self.image.resize(self.size,method)
return self.image
ratioImg = float(widthImg) / float(heightImg)
ratioWidth = float(widthImg) / float(widthDev)
ratioHeight = float(heightImg) / float(heightDev)
if ratioWidth > ratioHeight:
widthImg = widthDev
heightImg = int(widthDev / ratioImg)
elif ratioWidth < ratioHeight:
heightImg = heightDev
widthImg = int(heightDev * ratioImg)
else:
widthImg, heightImg = self.size
self.image = self.image.resize((widthImg, heightImg), Image.ANTIALIAS)
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]
newImage = Image.new('RGB', (self.image.size[0] + diff, self.image.size[1]), (255,255,255))
newImage.paste(self.image, (diff / 2, 0, diff / 2 + self.image.size[0], self.image.size[1]))
self.image = newImage
elif (float(self.image.size[0]) / float(self.image.size[1])) > ratioDev:
diff = int(self.image.size[0] * ratioDev) - self.image.size[1]
newImage = Image.new('RGB', (self.image.size[0], self.image.size[1] + diff), (255,255,255))
newImage.paste(self.image, (0, diff / 2, self.image.size[0], diff / 2 + self.image.size[1]))
self.image = newImage
self.image = ImageOps.fit(self.image, self.size, method = method, centering = (0.5,0.5))
return self.image
def splitPage(self, targetdir, righttoleft=False):
width, height = self.image.size