diff --git a/src/calibre/ebooks/metadata/sources/cli.py b/src/calibre/ebooks/metadata/sources/cli.py index aa47508890..31775d3631 100644 --- a/src/calibre/ebooks/metadata/sources/cli.py +++ b/src/calibre/ebooks/metadata/sources/cli.py @@ -13,7 +13,7 @@ from threading import Event from calibre import prints from calibre.utils.config import OptionParser -from calibre.utils.magick.draw import save_cover_data_to +from calibre.utils.img import save_cover_data_to from calibre.ebooks.metadata import string_to_authors from calibre.ebooks.metadata.opf2 import metadata_to_opf from calibre.ebooks.metadata.sources.base import create_log diff --git a/src/calibre/ebooks/mobi/mobiml.py b/src/calibre/ebooks/mobi/mobiml.py index 1afdd10ad1..11df25f18d 100644 --- a/src/calibre/ebooks/mobi/mobiml.py +++ b/src/calibre/ebooks/mobi/mobiml.py @@ -14,7 +14,7 @@ from calibre.ebooks.oeb.base import XHTML, XHTML_NS, urlnormalize from calibre.ebooks.oeb.stylizer import Stylizer from calibre.ebooks.oeb.transforms.flatcss import KeyMapper from calibre.ebooks.mobi.utils import convert_color_for_font_tag -from calibre.utils.magick.draw import identify_data +from calibre.utils.imghdr import identify MBP_NS = 'http://mobipocket.com/ns/mbp' def MBP(name): @@ -445,8 +445,8 @@ class MobiMLizer(object): href) else: try: - width, height = identify_data(item.data)[:2] - except: + width, height = identify(item.data)[1:] + except Exception: self.oeb.logger.warn('Invalid image:', href) else: if 'width' not in istate.attrib and 'height' not in \ diff --git a/src/calibre/ebooks/mobi/reader/containers.py b/src/calibre/ebooks/mobi/reader/containers.py index 93d3b893fc..72ebaf29ee 100644 --- a/src/calibre/ebooks/mobi/reader/containers.py +++ b/src/calibre/ebooks/mobi/reader/containers.py @@ -8,17 +8,10 @@ __copyright__ = '2014, Kovid Goyal ' from struct import unpack_from, error -from calibre.utils.magick.draw import identify_data from calibre.utils.imghdr import what def find_imgtype(data): - imgtype = what(None, data) - if imgtype is None: - try: - imgtype = identify_data(data)[2] - except Exception: - imgtype = 'unknown' - return imgtype + return what(None, data) or 'unknown' class Container(object): diff --git a/src/calibre/ebooks/mobi/reader/mobi6.py b/src/calibre/ebooks/mobi/reader/mobi6.py index baa93352ea..f09f3bbd1c 100644 --- a/src/calibre/ebooks/mobi/reader/mobi6.py +++ b/src/calibre/ebooks/mobi/reader/mobi6.py @@ -21,7 +21,7 @@ from calibre.ebooks.metadata import MetaInformation from calibre.ebooks.metadata.opf2 import OPFCreator, OPF from calibre.ebooks.metadata.toc import TOC from calibre.ebooks.mobi.reader.headers import BookHeader -from calibre.utils.magick.draw import save_cover_data_to +from calibre.utils.img import save_cover_data_to from calibre.utils.imghdr import what class TopazError(ValueError): @@ -863,7 +863,7 @@ class MobiReader(object): path = os.path.join(output_dir, '%05d.jpg' % image_index) try: - if what(None, data) not in {'jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'}: + if what(None, data) not in {'jpg', 'jpeg', 'gif', 'png', 'bmp'}: continue save_cover_data_to(data, path, minify_to=(10000, 10000)) except Exception: diff --git a/src/calibre/ebooks/mobi/utils.py b/src/calibre/ebooks/mobi/utils.py index 40f039a4be..2a5c1c0a96 100644 --- a/src/calibre/ebooks/mobi/utils.py +++ b/src/calibre/ebooks/mobi/utils.py @@ -11,7 +11,7 @@ import struct, string, zlib, os from collections import OrderedDict from io import BytesIO -from calibre.utils.magick.draw import Image, save_cover_data_to, thumbnail +from calibre.utils.img import save_cover_data_to, scale_image, image_to_data, image_from_data, resize_image from calibre.utils.imghdr import what from calibre.ebooks import normalize from tinycss.color3 import parse_color_string @@ -148,34 +148,27 @@ def rescale_image(data, maxsizeb=IMAGE_MAX_SIZE, dimen=None): width, height = dimen else: width = height = dimen - data = thumbnail(data, width=width, height=height, - compression_quality=90)[-1] + data = scale_image(data, width=width, height=height, compression_quality=90)[-1] else: # Replace transparent pixels with white pixels and convert to JPEG - data = save_cover_data_to(data, 'img.jpg', return_data=True) + data = save_cover_data_to(data) if len(data) <= maxsizeb: return data - orig_data = data - img = Image() - quality = 95 - - img.load(data) - while len(data) >= maxsizeb and quality >= 10: + orig_data = data # save it in case compression fails + quality = 90 + while len(data) > maxsizeb and quality >= 5: + data = image_to_data(image_from_data(orig_data), compression_quality=quality) quality -= 5 - img.set_compression_quality(quality) - data = img.export('jpg') if len(data) <= maxsizeb: return data orig_data = data scale = 0.9 - while len(data) >= maxsizeb and scale >= 0.05: - img = Image() - img.load(orig_data) - w, h = img.size - img.size = (int(scale*w), int(scale*h)) - img.set_compression_quality(quality) - data = img.export('jpg') + while len(data) > maxsizeb and scale >= 0.05: + img = image_from_data(data) + w, h = img.width(), img.height() + img = resize_image(img, int(scale*w), int(scale*h)) + data = image_to_data(img, compression_quality=quality) scale -= 0.05 return data @@ -391,9 +384,11 @@ def mobify_image(data): fmt = what(None, data) if fmt == 'png': - im = Image() - im.load(data) - data = im.export('gif') + from PIL import Image + im = Image.open(BytesIO(data)) + buf = BytesIO() + im.save(buf, 'gif') + data = buf.getvalue() return data # Font records {{{ diff --git a/src/calibre/ebooks/mobi/writer2/resources.py b/src/calibre/ebooks/mobi/writer2/resources.py index 3bffeea778..2866ac41b5 100644 --- a/src/calibre/ebooks/mobi/writer2/resources.py +++ b/src/calibre/ebooks/mobi/writer2/resources.py @@ -45,15 +45,14 @@ class Resources(object): try: return func(data) except Exception: - from calibre.utils.magick.draw import identify_data - if 'png' != identify_data(data)[-1].lower(): + if 'png' != what(None, data): raise with PersistentTemporaryFile(suffix='.png') as pt: pt.write(data) try: from calibre.utils.img import optimize_png optimize_png(pt.name) - data = open(pt.name, 'rb').read() + data = lopen(pt.name, 'rb').read() finally: os.remove(pt.name) return func(data)