From 5593a1af48dac1a68e6e61f3a06320be3852db50 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 5 May 2016 07:30:18 +0530 Subject: [PATCH] Remove IM from some more places --- src/calibre/ebooks/metadata/book/json_codec.py | 11 +++++------ src/calibre/ebooks/metadata/kfx.py | 6 +++--- src/calibre/ebooks/oeb/transforms/cover.py | 12 ++++++------ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/calibre/ebooks/metadata/book/json_codec.py b/src/calibre/ebooks/metadata/book/json_codec.py index 8cbc7b70e2..0af304f6a1 100644 --- a/src/calibre/ebooks/metadata/book/json_codec.py +++ b/src/calibre/ebooks/metadata/book/json_codec.py @@ -40,17 +40,16 @@ def encode_thumbnail(thumbnail): ''' Encode the image part of a thumbnail, then return the 3 part tuple ''' - from calibre.utils.magick import Image - + from calibre.utils.imghdr import identify if thumbnail is None: return None if not isinstance(thumbnail, (tuple, list)): try: - img = Image() - img.load(thumbnail) - width, height = img.size + width, height = identify(bytes(thumbnail))[1:] + if width < 0 or height < 0: + return None thumbnail = (width, height, thumbnail) - except: + except Exception: return None return (thumbnail[0], thumbnail[1], b64encode(str(thumbnail[2]))) diff --git a/src/calibre/ebooks/metadata/kfx.py b/src/calibre/ebooks/metadata/kfx.py index 1e54d53bff..7acc32716e 100644 --- a/src/calibre/ebooks/metadata/kfx.py +++ b/src/calibre/ebooks/metadata/kfx.py @@ -17,7 +17,7 @@ from calibre.utils.cleantext import clean_xml_chars from calibre.utils.config_base import tweaks from calibre.utils.date import parse_only_date from calibre.utils.localization import canonicalize_lang -from calibre.utils.magick.draw import identify_data +from calibre.utils.imghdr import identify class InvalidKFX(ValueError): pass @@ -313,10 +313,10 @@ def read_metadata_kfx(stream, read_cover=True): if read_cover and m[COVER_KEY]: try: data = base64.standard_b64decode(m[COVER_KEY]) - w, h, fmt = identify_data(data) + fmt, w, h = identify(bytes(data)) except Exception: w, h, fmt = 0, 0, None - if fmt and w and h: + if fmt and w > -1 and h > -1: mi.cover_data = (fmt, data) return mi diff --git a/src/calibre/ebooks/oeb/transforms/cover.py b/src/calibre/ebooks/oeb/transforms/cover.py index 964b9b40a9..03663a51aa 100644 --- a/src/calibre/ebooks/oeb/transforms/cover.py +++ b/src/calibre/ebooks/oeb/transforms/cover.py @@ -10,7 +10,7 @@ from urllib import unquote from lxml import etree from calibre import guess_type -from calibre.utils.magick.draw import identify_data +from calibre.utils.imghdr import identify class CoverManager(object): @@ -115,10 +115,10 @@ class CoverManager(object): if x.href == urlnormalize(href): try: raw = x.data - return identify_data(raw)[:2] - except: - self.log.exception('Failed to read image dimensions') - return None, None + return identify(raw)[1:] + except Exception: + self.log.exception('Failed to read cover image dimensions') + return -1, -1 def insert_cover(self): from calibre.ebooks.oeb.base import urldefrag @@ -132,7 +132,7 @@ class CoverManager(object): if href is None: return width, height = self.inspect_cover(href) - if width is None or height is None: + if width == -1 or height == -1: self.log.warning('Failed to read cover dimensions') width, height = 600, 800 # if self.preserve_aspect_ratio: