From af51493118d044ce5b3876314c2d1a076b0a7ca5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 4 May 2016 21:18:26 +0530 Subject: [PATCH] Remove IM from DOCX input --- src/calibre/ebooks/docx/cleanup.py | 5 +++-- src/calibre/ebooks/docx/images.py | 10 +++------- src/calibre/utils/img.py | 7 +++++++ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/calibre/ebooks/docx/cleanup.py b/src/calibre/ebooks/docx/cleanup.py index 1107e1a971..044c853944 100644 --- a/src/calibre/ebooks/docx/cleanup.py +++ b/src/calibre/ebooks/docx/cleanup.py @@ -181,9 +181,10 @@ def cleanup_markup(log, root, styles, dest_dir, detect_cover, XPath): img = img[0] path = os.path.join(dest_dir, img.get('src')) 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: - width, height, fmt = identify(path) + with lopen(path, 'rb') as imf: + fmt, width, height = identify(imf) except: width, height, fmt = 0, 0, None # noqa del fmt diff --git a/src/calibre/ebooks/docx/images.py b/src/calibre/ebooks/docx/images.py index 375739e727..471b50c4b1 100644 --- a/src/calibre/ebooks/docx/images.py +++ b/src/calibre/ebooks/docx/images.py @@ -10,12 +10,11 @@ import os from lxml.html.builder import IMG, HR -from calibre import fit_image from calibre.constants import iswindows from calibre.ebooks.docx.names import barename 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.magick import Image class LinkedImageNotFound(ValueError): @@ -156,14 +155,11 @@ class Images(object): return name def resize_image(self, raw, base, max_width, max_height): - img = Image() - img.load(raw) - resized, nwidth, nheight = fit_image(img.size[0], img.size[1], max_width, max_height) + resized, img = resize_to_fit(raw, max_width, max_height) if resized: - img.size = (nwidth, nheight) base, ext = os.path.splitext(base) 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 def generate_filename(self, rid, base=None, rid_map=None, max_width=None, max_height=None): diff --git a/src/calibre/utils/img.py b/src/calibre/utils/img.py index 96dc9c6a4b..cb71a6284b 100644 --- a/src/calibre/utils/img.py +++ b/src/calibre/utils/img.py @@ -82,6 +82,13 @@ def image_to_data(img, compression_quality=95, fmt='JPEG'): def resize_image(img, width, height): 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): ''' Scale an image, returning it as either JPEG or PNG data (bytestring). Transparency is alpha blended with white when converting to JPEG. Is thread