mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #2252 (--profile should resize pictures too)
This commit is contained in:
parent
596b52afac
commit
3e12e32721
@ -36,7 +36,7 @@ import os, sys, cStringIO, logging, re, functools, shutil
|
|||||||
|
|
||||||
from lxml.etree import XPath
|
from lxml.etree import XPath
|
||||||
from lxml import html, etree
|
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,\
|
from calibre.ebooks.html import Processor, merge_metadata, get_filelist,\
|
||||||
opf_traverse, create_metadata, rebase_toc, Link, parser
|
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.ebooks.epub.fonts import Rationalizer
|
||||||
from calibre.constants import preferred_encoding
|
from calibre.constants import preferred_encoding
|
||||||
from calibre.customize.ui import run_plugins_on_postprocess
|
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')
|
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 f, os.path.splitext(f)[1].lower()[1:]
|
||||||
return html_files[-1], os.path.splitext(html_files[-1])[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):
|
class HTMLProcessor(Processor, Rationalizer):
|
||||||
|
|
||||||
def __init__(self, htmlfile, opts, tdir, resource_map, htmlfiles, stylesheets):
|
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:
|
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)
|
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:
|
if create_epub:
|
||||||
epub = initialize_container(opts.output)
|
epub = initialize_container(opts.output)
|
||||||
epub.add_dir(tdir)
|
epub.add_dir(tdir)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user