diff --git a/ebook_converter/__init__.py b/ebook_converter/__init__.py index df643fa..d3970f7 100644 --- a/ebook_converter/__init__.py +++ b/ebook_converter/__init__.py @@ -1,5 +1,4 @@ import html -import math import os import re @@ -9,30 +8,6 @@ from ebook_converter.ebooks.html_entities import html5_entities -def fit_image(width, height, pwidth, pheight): - """ - Fit image in box of width pwidth and height pheight. - @param width: Width of image - @param height: Height of image - @param pwidth: Width of box - @param pheight: Height of box - @return: scaled, new_width, new_height. scaled is True iff new_width - and/or new_height is different from width or height. - """ - scaled = height > pheight or width > pwidth - if height > pheight: - corrf = pheight / float(height) - width, height = math.floor(corrf*width), pheight - if width > pwidth: - corrf = pwidth / float(width) - width, height = pwidth, math.floor(corrf*height) - if height > pheight: - corrf = pheight / float(height) - width, height = math.floor(corrf*width), pheight - - return scaled, int(width), int(height) - - class CurrentDir(object): def __init__(self, path): diff --git a/ebook_converter/ebooks/covers.py b/ebook_converter/ebooks/covers.py index 7fd7aaf..46a28b3 100644 --- a/ebook_converter/ebooks/covers.py +++ b/ebook_converter/ebooks/covers.py @@ -1,4 +1,4 @@ -from ebook_converter import fit_image +# from ebook_converter.util import img from ebook_converter.constants_old import __appname__, __version__ from ebook_converter.gui2 import ensure_app, config, load_builtin_fonts, pixmap_to_data from ebook_converter.utils.cleantext import clean_ascii_chars, clean_xml_chars @@ -28,7 +28,7 @@ # bottom = footer_block.position.y - 50 # logo = QImage(logo_path or I('library.png')) # pwidth, pheight = rect.width(), bottom - top -# scaled, width, height = fit_image(logo.width(), logo.height(), pwidth, pheight) +# scaled, width, height = img.fit_image(logo.width(), logo.height(), pwidth, pheight) # x, y = (pwidth - width) // 2, (pheight - height) // 2 # rect = QRect(x, top + y, width, height) # painter.setRenderHint(QPainter.SmoothPixmapTransform) diff --git a/ebook_converter/ebooks/docx/writer/images.py b/ebook_converter/ebooks/docx/writer/images.py index daaff85..c29ebaf 100644 --- a/ebook_converter/ebooks/docx/writer/images.py +++ b/ebook_converter/ebooks/docx/writer/images.py @@ -6,9 +6,9 @@ from lxml import etree -from ebook_converter import fit_image from ebook_converter.ebooks.docx.images import pt_to_emu from ebook_converter.utils.filenames import ascii_filename +from ebook_converter.utils import img as uimg from ebook_converter.utils.imghdr import identify @@ -101,7 +101,8 @@ def create_image_markup(self, html_img, stylizer, href, as_block=False): img = self.images[href] name = urllib.parse.unquote(posixpath.basename(href)) width, height = style.img_size(img.width, img.height) - scaled, width, height = fit_image(width, height, self.page_width, self.page_height) + scaled, width, height = uimg.fit_image(width, height, self.page_width, + self.page_height) width, height = map(pt_to_emu, (width, height)) makeelement, namespaces = self.document_relationships.namespace.makeelement, self.document_relationships.namespace.namespaces diff --git a/ebook_converter/ebooks/lrf/html/convert_from.py b/ebook_converter/ebooks/lrf/html/convert_from.py index 71e3a79..92462c1 100644 --- a/ebook_converter/ebooks/lrf/html/convert_from.py +++ b/ebook_converter/ebooks/lrf/html/convert_from.py @@ -19,9 +19,9 @@ import math import bs4 +from PIL import Image as PILImage -from ebook_converter import entity_to_unicode, fit_image, \ - force_unicode +from ebook_converter import entity_to_unicode, force_unicode from ebook_converter.constants_old import __appname__, filesystem_encoding, \ preferred_encoding from ebook_converter.devices.interface import DevicePlugin as Device @@ -37,8 +37,7 @@ RuledLine, Span, Sub, Sup, TextBlock ) from ebook_converter.ptempfile import PersistentTemporaryFile - -from PIL import Image as PILImage +from ebook_converter.utils import img as uimg def strip_style_comments(match): @@ -1075,7 +1074,7 @@ def scale_image(width, height): finally: pt.close() - scaled, width, height = fit_image(width, height, pwidth, pheight) + scaled, width, height = uimg.fit_image(width, height, pwidth, pheight) if scaled: path = scale_image(width, height) @@ -1956,7 +1955,8 @@ def process_file(path, options, logger): corrf = pwidth/width width, height = pwidth, int(corrf*height) - scaled, width, height = fit_image(width, height, pwidth, pheight) + scaled, width, height = uimg.fit_image(width, height, pwidth, + pheight) try: cim = im.resize((width, height), PILImage diff --git a/ebook_converter/ebooks/oeb/transforms/rescale.py b/ebook_converter/ebooks/oeb/transforms/rescale.py index 9d4e409..d1677be 100644 --- a/ebook_converter/ebooks/oeb/transforms/rescale.py +++ b/ebook_converter/ebooks/oeb/transforms/rescale.py @@ -1,9 +1,4 @@ -from ebook_converter import fit_image - - -__license__ = 'GPL v3' -__copyright__ = '2009, Kovid Goyal ' -__docformat__ = 'restructuredtext en' +from ebook_converter.utils import img as uimg class RescaleImages(object): @@ -57,7 +52,9 @@ def rescale(self): except Exception: self.log.exception('Failed to convert image %s from CMYK to RGB' % item.href) - scaled, new_width, new_height = fit_image(width, height, page_width, page_height) + scaled, new_width, new_height = uimg.fit_image(width, height, + page_width, + page_height) if scaled: new_width = max(1, new_width) new_height = max(1, new_height) diff --git a/ebook_converter/utils/img.py b/ebook_converter/utils/img.py index 3550c08..53a6dd1 100644 --- a/ebook_converter/utils/img.py +++ b/ebook_converter/utils/img.py @@ -1,4 +1,5 @@ import errno +import math import os import shutil import subprocess @@ -11,7 +12,7 @@ #from PyQt5.QtCore import QBuffer, QByteArray, Qt #from PyQt5.QtGui import QColor, QImage, QImageReader, QImageWriter, QPixmap, QTransform -from ebook_converter import fit_image, force_unicode +from ebook_converter import force_unicode from ebook_converter.constants_old import plugins from ebook_converter.ptempfile import TemporaryDirectory from ebook_converter.utils.config_base import tweaks @@ -24,6 +25,30 @@ # raise RuntimeError(imageops_err) +def fit_image(width, height, pwidth, pheight): + """ + Fit image in box of width pwidth and height pheight. + @param width: Width of image + @param height: Height of image + @param pwidth: Width of box + @param pheight: Height of box + @return: scaled, new_width, new_height. scaled is True iff new_width + and/or new_height is different from width or height. + """ + scaled = height > pheight or width > pwidth + if height > pheight: + corrf = pheight / float(height) + width, height = math.floor(corrf*width), pheight + if width > pwidth: + corrf = pwidth / float(width) + width, height = pwidth, math.floor(corrf*height) + if height > pheight: + corrf = pheight / float(height) + width, height = math.floor(corrf*width), pheight + + return scaled, int(width), int(height) + + class NotImage(ValueError): pass