From 31b92d6ab8a8d3a2ceb5b7fe9ff6401ad7fdf944 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 5 Apr 2023 17:57:31 +0530 Subject: [PATCH] Comic Input: When converting grayscaled PNG images to PNG ensure output images are stored as indexed PNG. Fixes #1846 (Better implied comic conversion of grayscale images) --- src/calibre/ebooks/comic/input.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/comic/input.py b/src/calibre/ebooks/comic/input.py index 2c3b70709f..3c28346a5e 100644 --- a/src/calibre/ebooks/comic/input.py +++ b/src/calibre/ebooks/comic/input.py @@ -104,9 +104,11 @@ class PageProcessor(list): # {{{ self.num = num self.dest = dest self.rotate = False + self.src_img_was_grayscaled = False self.render() def render(self): + from qt.core import QImage from calibre.utils.img import image_from_data, scale_image, crop_image with open(self.path_to_page, 'rb') as f: img = image_from_data(f.read()) @@ -114,6 +116,8 @@ class PageProcessor(list): # {{{ if self.num == 0: # First image so create a thumbnail from it with open(os.path.join(self.dest, 'thumbnail.png'), 'wb') as f: f.write(scale_image(img, as_png=True)[-1]) + self.src_img_was_grayscaled = img.format() in (QImage.Format.Format_Grayscale8, QImage.Format.Format_Grayscale16) or ( + img.format() == QImage.Format.Format_Indexed8 and img.allGray()) self.pages = [img] if width > height: if self.opts.landscape: @@ -126,6 +130,7 @@ class PageProcessor(list): # {{{ self.process_pages() def process_pages(self): + from qt.core import QImage from calibre.utils.img import ( image_to_data, rotate_image, remove_borders_from_image, normalize_image, add_borders_to_image, resize_image, gaussian_sharpen_image, grayscale_image, @@ -206,8 +211,11 @@ class PageProcessor(list): # {{{ if self.opts.despeckle: img = despeckle_image(img) - if self.opts.output_format.lower() == 'png' and self.opts.colors: - img = quantize_image(img, max_colors=min(256, self.opts.colors)) + if self.opts.output_format.lower() == 'png': + if self.opts.colors: + img = quantize_image(img, max_colors=min(256, self.opts.colors)) + elif self.src_img_was_grayscaled: + img = img.convertToFormat(QImage.Format.Format_Grayscale8) dest = '%d_%d.%s'%(self.num, i, self.opts.output_format) dest = os.path.join(self.dest, dest) with open(dest, 'wb') as f: