From 3e12e32721433b8effb9a35243dd4378d5398c65 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 9 Apr 2009 15:01:40 -0700 Subject: [PATCH] Fix #2252 (--profile should resize pictures too) --- src/calibre/ebooks/epub/from_html.py | 33 ++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/epub/from_html.py b/src/calibre/ebooks/epub/from_html.py index 318cf5cc02..3fd7b082f9 100644 --- a/src/calibre/ebooks/epub/from_html.py +++ b/src/calibre/ebooks/epub/from_html.py @@ -36,7 +36,7 @@ import os, sys, cStringIO, logging, re, functools, shutil from lxml.etree import XPath from lxml import html, etree -from PyQt4.Qt import QApplication, QPixmap +from PyQt4.Qt import QApplication, QPixmap, Qt from calibre.ebooks.html import Processor, merge_metadata, get_filelist,\ opf_traverse, create_metadata, rebase_toc, Link, parser @@ -50,7 +50,7 @@ from calibre.ebooks.epub.pages import add_page_map from calibre.ebooks.epub.fonts import Rationalizer from calibre.constants import preferred_encoding from calibre.customize.ui import run_plugins_on_postprocess -from calibre import walk, CurrentDir, to_unicode +from calibre import walk, CurrentDir, to_unicode, fit_image content = functools.partial(os.path.join, u'content') @@ -112,6 +112,31 @@ def find_html_index(files): return f, os.path.splitext(f)[1].lower()[1:] return html_files[-1], os.path.splitext(html_files[-1])[1].lower()[1:] +def rescale_images(imgdir, screen_size, log): + pwidth, pheight = screen_size + if QApplication.instance() is None: + QApplication([]) + for f in os.listdir(imgdir): + path = os.path.join(imgdir, f) + if os.path.splitext(f)[1] in ('.css', '.js'): + continue + + p = QPixmap() + p.load(path) + if p.isNull(): + continue + width, height = p.width(), p.height() + scaled, new_width, new_height = fit_image(width, height, pwidth, + pheight) + if scaled: + log.info('Rescaling image: '+f) + p.scaled(new_width, new_height, Qt.IgnoreAspectRatio, + Qt.SmoothTransformation).save(path, 'JPEG') + + + + + class HTMLProcessor(Processor, Rationalizer): def __init__(self, htmlfile, opts, tdir, resource_map, htmlfiles, stylesheets): @@ -482,6 +507,10 @@ def convert(htmlfile, opts, notification=None, create_epub=True, if os.stat(ncx_path).st_size > opts.profile.flow_size: logger.warn('NCX still larger than allowed size at %d bytes. Menu based Table of Contents may not work on device.'%os.stat(ncx_path).st_size) + if opts.profile.screen_size is not None: + rescale_images(os.path.join(tdir, 'content', 'resources'), + opts.profile.screen_size, logger) + if create_epub: epub = initialize_container(opts.output) epub.add_dir(tdir)