diff --git a/src/calibre/utils/img.py b/src/calibre/utils/img.py index ef2e224aec..e18fc0529b 100644 --- a/src/calibre/utils/img.py +++ b/src/calibre/utils/img.py @@ -140,6 +140,18 @@ def save_cover_data_to(data, path=None, bgcolor='#ffffff', resize_to=None, compr with lopen(path, 'wb') as f: f.write(image_to_data(img, compression_quality, fmt)) +def blend_on_canvas(img, width, height, bgcolor='#ffffff'): + w, h = img.width(), img.height() + scaled, nw, nh = fit_image(w, h, width, height) + if scaled: + img = img.scaled(nw, nh, Qt.IgnoreAspectRatio, Qt.SmoothTransformation) + w, h = nw, nh + nimg = QImage(width, height, QImage.Format_RGB32) + nimg.fill(QColor(bgcolor)) + p = QPainter(nimg) + p.drawImage((width - w)//2, (height - h)//2, img) + p.end() + return nimg def run_optimizer(file_path, cmd, as_filter=False, input_data=None): file_path = os.path.abspath(file_path) diff --git a/src/calibre/web/feeds/news.py b/src/calibre/web/feeds/news.py index 7141ed3b92..37819a4634 100644 --- a/src/calibre/web/feeds/news.py +++ b/src/calibre/web/feeds/news.py @@ -23,6 +23,7 @@ from calibre.ebooks.metadata.toc import TOC from calibre.ebooks.metadata import MetaInformation from calibre.web.feeds import feed_from_xml, templates, feeds_from_index, Feed from calibre.web.fetch.simple import option_parser as web2disk_option_parser, RecursiveFetcher, AbortArticle +from calibre.web.fetch.utils import prepare_masthead_image from calibre.utils.threadpool import WorkRequest, ThreadPool, NoResultsPending from calibre.ptempfile import PersistentTemporaryFile from calibre.utils.date import now as nowf @@ -1374,22 +1375,7 @@ class BasicNewsRecipe(Recipe): width=self.MI_WIDTH, height=self.MI_HEIGHT) def prepare_masthead_image(self, path_to_image, out_path): - from calibre import fit_image - from calibre.utils.magick import Image, create_canvas - - img = Image() - img.open(path_to_image) - width, height = img.size - scaled, nwidth, nheight = fit_image(width, height, self.MI_WIDTH, self.MI_HEIGHT) - img2 = create_canvas(width, height) - frame = create_canvas(self.MI_WIDTH, self.MI_HEIGHT) - img2.compose(img) - if scaled: - img2.size = (nwidth, nheight, 'LanczosFilter', 0.5) - left = int((self.MI_WIDTH - nwidth)/2.0) - top = int((self.MI_HEIGHT - nheight)/2.0) - frame.compose(img2, left, top) - frame.save(out_path) + prepare_masthead_image(path_to_image, out_path, self.MI_WIDTH, self.MI_HEIGHT) def create_opf(self, feeds, dir=None): if dir is None: diff --git a/src/calibre/web/fetch/utils.py b/src/calibre/web/fetch/utils.py index 1f5de5e90c..5563049568 100644 --- a/src/calibre/web/fetch/utils.py +++ b/src/calibre/web/fetch/utils.py @@ -4,7 +4,7 @@ from __future__ import (unicode_literals, division, absolute_import, print_function) -from calibre.utils.img import image_from_data, scale_image, image_to_data +from calibre.utils.img import image_from_data, scale_image, image_to_data, blend_on_canvas def rescale_image(data, scale_news_images, compress_news_images_max_size, compress_news_images_auto_size): orig_data = data # save it in case compression fails @@ -38,6 +38,13 @@ def rescale_image(data, scale_news_images, compress_news_images_max_size, compre return data +def prepare_masthead_image(path_to_image, out_path, mi_width, mi_height): + with lopen(path_to_image, 'rb') as f: + img = image_from_data(f.read()) + img = blend_on_canvas(img, mi_width, mi_height) + with lopen(out_path, 'wb') as f: + f.write(image_to_data(img)) + if __name__ == '__main__': import sys data = sys.stdin.read()