From 187eb4e0659239a816bac9050a982f9f127bc071 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 12 Sep 2013 20:30:53 +0530 Subject: [PATCH] ODT Input: Only use the first image as cover ODT Input: Only treat the first image as a cover if it is of suitable size, instead of any image in the document. See #1224157 (ODT to MOBI - Images - Cover Image will not removed and other images are missed) --- src/calibre/ebooks/metadata/odt.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/calibre/ebooks/metadata/odt.py b/src/calibre/ebooks/metadata/odt.py index b919885bfd..41b870b312 100644 --- a/src/calibre/ebooks/metadata/odt.py +++ b/src/calibre/ebooks/metadata/odt.py @@ -224,26 +224,28 @@ def read_cover(stream, zin, mi, opfmeta, extract_cover): cover_href = None cover_data = None cover_frame = None + imgnum = 0 for frm in otext.topnode.getElementsByType(odFrame): img = frm.getElementsByType(odImage) - if len(img) > 0: # there should be only one - i_href = img[0].getAttribute('href') - try: - raw = zin.read(i_href) - except KeyError: - continue - try: - width, height, fmt = identify_data(raw) - except: - continue - else: + if len(img) == 0: continue + i_href = img[0].getAttribute('href') + try: + raw = zin.read(i_href) + except KeyError: + continue + try: + width, height, fmt = identify_data(raw) + except: + continue + imgnum += 1 if opfmeta and frm.getAttribute('name').lower() == u'opf.cover': cover_href = i_href cover_data = (fmt, raw) cover_frame = frm.getAttribute('name') # could have upper case break - if cover_href is None and 0.8 <= height/width <= 1.8 and height*width >= 12000: + if cover_href is None and imgnum == 1 and 0.8 <= height/width <= 1.8 and height*width >= 12000: + # Pick the first image as the cover if it is of a suitable size cover_href = i_href cover_data = (fmt, raw) if not opfmeta: