Skip to content

Commit

Permalink
Move fit_image to img utils module.
Browse files Browse the repository at this point in the history
  • Loading branch information
gryf committed Oct 13, 2020
1 parent 250d0ee commit b44926a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 43 deletions.
25 changes: 0 additions & 25 deletions ebook_converter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import html
import math
import os
import re

Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions ebook_converter/ebooks/covers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions ebook_converter/ebooks/docx/writer/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions ebook_converter/ebooks/lrf/html/convert_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions ebook_converter/ebooks/oeb/transforms/rescale.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
from ebook_converter import fit_image


__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
from ebook_converter.utils import img as uimg


class RescaleImages(object):
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 26 additions & 1 deletion ebook_converter/utils/img.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import errno
import math
import os
import shutil
import subprocess
Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit b44926a

Please sign in to comment.