From 751e6eb4e700b0fdaf346feb1478cc5a0cb08162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Devernay?= Date: Tue, 5 Mar 2013 17:56:43 +0100 Subject: [PATCH 1/2] save dithered images as PNG, and linearize (inverse gamma) before dithering --- kcc/comic2ebook.py | 4 +++- kcc/gui.py | 14 ++++++++++++-- kcc/image.py | 9 ++++++--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index a6c0b51..0249a84 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -252,7 +252,7 @@ def isInFilelist(filename, filelist): def applyImgOptimization(img, isSplit=False, toRight=False): - img.optimizeImage() + img.optimizeImage(options.gamma) img.cropWhiteSpace(10.0) if options.cutpagenumbers: img.cutPageNumber() @@ -396,6 +396,8 @@ def main(argv=None): help="Verbose output [default=False]") 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]") + parser.add_option("--gamma", type="float", dest="gamma", default=2.2, + help="Apply gamma correction to linearize the image [default=2.2]") 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, diff --git a/kcc/gui.py b/kcc/gui.py index 9f98590..c1156b4 100644 --- a/kcc/gui.py +++ b/kcc/gui.py @@ -92,6 +92,7 @@ class MainWindow: 'rotate': IntVar(None, 0), 'cut_page_numbers': IntVar(None, 1), 'mangastyle': IntVar(None, 0), + 'image_gamma': DoubleVar(None, 2.2), 'image_upscale': IntVar(None, 0), 'image_stretch': IntVar(None, 0), 'black_borders': IntVar(None, 0) @@ -102,13 +103,20 @@ class MainWindow: 'rotate': "Rotate landscape images instead of splitting them", 'cut_page_numbers': "Cut page numbers", 'mangastyle': "Manga-style (right-to-left reading, applies to reading and splitting)", + 'image_gamma': "Gamma value", 'image_upscale': "Allow image upscaling", 'image_stretch': "Stretch images", 'black_borders': "Use black borders" } for key in self.options: - aCheckButton = Checkbutton(self.master, text=self.optionlabels[key], variable=self.options[key]) - aCheckButton.grid(column=3, sticky='w') + if isinstance( self.options[key], IntVar ) or isinstance( self.options[key], BooleanVar ): + aCheckButton = Checkbutton(self.master, text=self.optionlabels[key], variable=self.options[key]) + aCheckButton.grid(column=3, sticky='w') + elif isinstance( self.options[key], DoubleVar ): + aLabel = Label(self.master, text=self.optionlabels[key]) + aLabel.grid(column=2, sticky='w') + aEntry = Entry(self.master, textvariable=self.options[key]) + aEntry.grid(column=3, row=(self.master.grid_size()[1]-1), sticky='w') self.progressbar = ttk.Progressbar(orient=HORIZONTAL, length=200, mode='determinate') self.submit = Button(self.master, text="Execute!", command=self.start_conversion, fg="red") @@ -138,6 +146,8 @@ class MainWindow: argv.append("--no-cut-page-numbers") if self.options['mangastyle'].get() == 1: argv.append("-m") + argv.append("--gamma") + argv.append(self.options['image_gamma'].get()) if self.options['image_upscale'].get() == 1: argv.append("--upscale-images") if self.options['image_stretch'].get() == 1: diff --git a/kcc/image.py b/kcc/image.py index f3080ed..00c1067 100755 --- a/kcc/image.py +++ b/kcc/image.py @@ -115,12 +115,15 @@ class ComicPage: filename = os.path.basename(self.origFileName) try: self.image = self.image.convert('L') # convert to grayscale - self.image.save(os.path.join(targetdir, filename), "JPEG") + os.remove(os.path.join(targetdir,filename)) # remove original file, copied by copytree() in comic2ebook.py +# self.image.save(os.path.join(targetdir, filename), "JPEG") + self.image.save(os.path.join(targetdir, os.path.splitext(filename)[0] + ".png"), "PNG") # quantized images don't like JPEG 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 optimizeImage(self, gamma): + self.image = ImageOps.autocontrast(Image.eval(self.image, lambda a: 255*(a/255.)**gamma)) +# self.image = ImageOps.autocontrast(self.image) def quantizeImage(self): colors = len(self.palette) / 3 From 5d3b7e83f58997898a11990254cd64821c8883a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Devernay?= Date: Tue, 5 Mar 2013 17:58:24 +0100 Subject: [PATCH 2/2] add --nosplitrotate option (is the name right?) --- kcc/comic2ebook.py | 7 ++++++- kcc/gui.py | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/kcc/comic2ebook.py b/kcc/comic2ebook.py index 0249a84..1cfdd9a 100755 --- a/kcc/comic2ebook.py +++ b/kcc/comic2ebook.py @@ -277,7 +277,10 @@ def dirImgProcess(path): else: print ".", img = image.ComicPage(os.path.join(dirpath, afile), options.profile) - split = img.splitPage(dirpath, options.righttoleft, options.rotate) + if options.nosplitrotate: + split = None + else: + split = img.splitPage(dirpath, options.righttoleft, options.rotate) if split is not None: if options.verbose: print "Splitted " + afile @@ -407,6 +410,8 @@ def main(argv=None): + "is not like the device's one [default=False]") 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]") + parser.add_option("--nosplitrotate", action="store_true", dest="nosplitrotate", default=False, + help="Disable splitting and rotation [default=False]") parser.add_option("--rotate", action="store_true", dest="rotate", default=False, help="Rotate landscape pages instead of splitting them [default=False]") parser.add_option("-o", "--output", action="store", dest="output", default=None, diff --git a/kcc/gui.py b/kcc/gui.py index c1156b4..beb44e0 100644 --- a/kcc/gui.py +++ b/kcc/gui.py @@ -89,6 +89,7 @@ class MainWindow: self.options = { 'epub_only': IntVar(None, 0), 'image_preprocess': IntVar(None, 1), + 'nosplitrotate': IntVar(None, 0), 'rotate': IntVar(None, 0), 'cut_page_numbers': IntVar(None, 1), 'mangastyle': IntVar(None, 0), @@ -100,6 +101,7 @@ class MainWindow: self.optionlabels = { 'epub_only': "Generate ePub only (does not call 'kindlegen')", 'image_preprocess': "Apply image optimizations", + 'nosplitrotate': "Disable splitting and rotation", 'rotate': "Rotate landscape images instead of splitting them", 'cut_page_numbers': "Cut page numbers", 'mangastyle': "Manga-style (right-to-left reading, applies to reading and splitting)", @@ -140,6 +142,8 @@ class MainWindow: argv = ["-p", profilekey] if self.options['image_preprocess'].get() == 0: argv.append("--no-image-processing") + if self.options['nosplitrotate'].get() == 1: + argv.append("--nosplitrotate") if self.options['rotate'].get() == 1: argv.append("--rotate") if self.options['cut_page_numbers'].get() == 0: