1
0
mirror of https://github.com/gryf/ebook-converter.git synced 2026-03-22 18:33:34 +01:00

Move fit_image to img utils module.

This commit is contained in:
2020-10-13 19:39:05 +02:00
parent 250d0eeea7
commit b44926a6eb
6 changed files with 41 additions and 43 deletions

View File

@@ -1,5 +1,4 @@
import html import html
import math
import os import os
import re import re
@@ -9,30 +8,6 @@ from ebook_converter import constants_old
from ebook_converter.ebooks.html_entities import html5_entities 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): class CurrentDir(object):
def __init__(self, path): def __init__(self, path):

View File

@@ -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.constants_old import __appname__, __version__
from ebook_converter.gui2 import ensure_app, config, load_builtin_fonts, pixmap_to_data 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 from ebook_converter.utils.cleantext import clean_ascii_chars, clean_xml_chars
@@ -28,7 +28,7 @@ from ebook_converter.utils.config import JSONConfig
# bottom = footer_block.position.y - 50 # bottom = footer_block.position.y - 50
# logo = QImage(logo_path or I('library.png')) # logo = QImage(logo_path or I('library.png'))
# pwidth, pheight = rect.width(), bottom - top # 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 # x, y = (pwidth - width) // 2, (pheight - height) // 2
# rect = QRect(x, top + y, width, height) # rect = QRect(x, top + y, width, height)
# painter.setRenderHint(QPainter.SmoothPixmapTransform) # painter.setRenderHint(QPainter.SmoothPixmapTransform)

View File

@@ -6,9 +6,9 @@ import urllib.parse
from lxml import etree from lxml import etree
from ebook_converter import fit_image
from ebook_converter.ebooks.docx.images import pt_to_emu from ebook_converter.ebooks.docx.images import pt_to_emu
from ebook_converter.utils.filenames import ascii_filename from ebook_converter.utils.filenames import ascii_filename
from ebook_converter.utils import img as uimg
from ebook_converter.utils.imghdr import identify from ebook_converter.utils.imghdr import identify
@@ -101,7 +101,8 @@ class ImagesManager(object):
img = self.images[href] img = self.images[href]
name = urllib.parse.unquote(posixpath.basename(href)) name = urllib.parse.unquote(posixpath.basename(href))
width, height = style.img_size(img.width, img.height) 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)) width, height = map(pt_to_emu, (width, height))
makeelement, namespaces = self.document_relationships.namespace.makeelement, self.document_relationships.namespace.namespaces makeelement, namespaces = self.document_relationships.namespace.makeelement, self.document_relationships.namespace.namespaces

View File

@@ -19,9 +19,9 @@ import itertools
import math import math
import bs4 import bs4
from PIL import Image as PILImage
from ebook_converter import entity_to_unicode, fit_image, \ from ebook_converter import entity_to_unicode, force_unicode
force_unicode
from ebook_converter.constants_old import __appname__, filesystem_encoding, \ from ebook_converter.constants_old import __appname__, filesystem_encoding, \
preferred_encoding preferred_encoding
from ebook_converter.devices.interface import DevicePlugin as Device from ebook_converter.devices.interface import DevicePlugin as Device
@@ -37,8 +37,7 @@ from ebook_converter.ebooks.lrf.pylrs.pylrs import (
RuledLine, Span, Sub, Sup, TextBlock RuledLine, Span, Sub, Sup, TextBlock
) )
from ebook_converter.ptempfile import PersistentTemporaryFile from ebook_converter.ptempfile import PersistentTemporaryFile
from ebook_converter.utils import img as uimg
from PIL import Image as PILImage
def strip_style_comments(match): def strip_style_comments(match):
@@ -1075,7 +1074,7 @@ class HTMLConverter(object):
finally: finally:
pt.close() pt.close()
scaled, width, height = fit_image(width, height, pwidth, pheight) scaled, width, height = uimg.fit_image(width, height, pwidth, pheight)
if scaled: if scaled:
path = scale_image(width, height) path = scale_image(width, height)
@@ -1956,7 +1955,8 @@ def process_file(path, options, logger):
corrf = pwidth/width corrf = pwidth/width
width, height = pwidth, int(corrf*height) 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: try:
cim = im.resize((width, height), cim = im.resize((width, height),
PILImage PILImage

View File

@@ -1,9 +1,4 @@
from ebook_converter import fit_image from ebook_converter.utils import img as uimg
__license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
class RescaleImages(object): class RescaleImages(object):
@@ -57,7 +52,9 @@ class RescaleImages(object):
except Exception: except Exception:
self.log.exception('Failed to convert image %s from CMYK to RGB' % item.href) 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: if scaled:
new_width = max(1, new_width) new_width = max(1, new_width)
new_height = max(1, new_height) new_height = max(1, new_height)

View File

@@ -1,4 +1,5 @@
import errno import errno
import math
import os import os
import shutil import shutil
import subprocess import subprocess
@@ -11,7 +12,7 @@ from threading import Thread
#from PyQt5.QtCore import QBuffer, QByteArray, Qt #from PyQt5.QtCore import QBuffer, QByteArray, Qt
#from PyQt5.QtGui import QColor, QImage, QImageReader, QImageWriter, QPixmap, QTransform #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.constants_old import plugins
from ebook_converter.ptempfile import TemporaryDirectory from ebook_converter.ptempfile import TemporaryDirectory
from ebook_converter.utils.config_base import tweaks from ebook_converter.utils.config_base import tweaks
@@ -24,6 +25,30 @@ from ebook_converter.utils.imghdr import what
# raise RuntimeError(imageops_err) # 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): class NotImage(ValueError):
pass pass