Remove IM from DOCX input

This commit is contained in:
Kovid Goyal 2016-05-04 21:18:26 +05:30
parent 0a3b2a28c6
commit af51493118
3 changed files with 13 additions and 9 deletions

View File

@ -181,9 +181,10 @@ def cleanup_markup(log, root, styles, dest_dir, detect_cover, XPath):
img = img[0] img = img[0]
path = os.path.join(dest_dir, img.get('src')) path = os.path.join(dest_dir, img.get('src'))
if os.path.exists(path) and before_count(root, img, limit=10) < 5: if os.path.exists(path) and before_count(root, img, limit=10) < 5:
from calibre.utils.magick.draw import identify from calibre.utils.imghdr import identify
try: try:
width, height, fmt = identify(path) with lopen(path, 'rb') as imf:
fmt, width, height = identify(imf)
except: except:
width, height, fmt = 0, 0, None # noqa width, height, fmt = 0, 0, None # noqa
del fmt del fmt

View File

@ -10,12 +10,11 @@ import os
from lxml.html.builder import IMG, HR from lxml.html.builder import IMG, HR
from calibre import fit_image
from calibre.constants import iswindows from calibre.constants import iswindows
from calibre.ebooks.docx.names import barename from calibre.ebooks.docx.names import barename
from calibre.utils.filenames import ascii_filename from calibre.utils.filenames import ascii_filename
from calibre.utils.img import resize_to_fit, image_to_data
from calibre.utils.imghdr import what from calibre.utils.imghdr import what
from calibre.utils.magick import Image
class LinkedImageNotFound(ValueError): class LinkedImageNotFound(ValueError):
@ -156,14 +155,11 @@ class Images(object):
return name return name
def resize_image(self, raw, base, max_width, max_height): def resize_image(self, raw, base, max_width, max_height):
img = Image() resized, img = resize_to_fit(raw, max_width, max_height)
img.load(raw)
resized, nwidth, nheight = fit_image(img.size[0], img.size[1], max_width, max_height)
if resized: if resized:
img.size = (nwidth, nheight)
base, ext = os.path.splitext(base) base, ext = os.path.splitext(base)
base = base + '-%dx%d%s' % (max_width, max_height, ext) base = base + '-%dx%d%s' % (max_width, max_height, ext)
raw = img.export(ext[1:]) raw = image_to_data(img, fmt=ext[1:])
return raw, base, resized return raw, base, resized
def generate_filename(self, rid, base=None, rid_map=None, max_width=None, max_height=None): def generate_filename(self, rid, base=None, rid_map=None, max_width=None, max_height=None):

View File

@ -82,6 +82,13 @@ def image_to_data(img, compression_quality=95, fmt='JPEG'):
def resize_image(img, width, height): def resize_image(img, width, height):
return img.scaled(int(width), int(height), Qt.IgnoreAspectRatio, Qt.SmoothTransformation) return img.scaled(int(width), int(height), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
def resize_to_fit(img, width, height):
img = image_from_data(img)
resize_needed, nw, nh = fit_image(img.width(), img.height(), width, height)
if resize_needed:
resize_image(img, nw, nh)
return resize_needed, img
def scale_image(data, width=60, height=80, compression_quality=70, as_png=False, preserve_aspect_ratio=True): def scale_image(data, width=60, height=80, compression_quality=70, as_png=False, preserve_aspect_ratio=True):
''' Scale an image, returning it as either JPEG or PNG data (bytestring). ''' Scale an image, returning it as either JPEG or PNG data (bytestring).
Transparency is alpha blended with white when converting to JPEG. Is thread Transparency is alpha blended with white when converting to JPEG. Is thread