From 9b5a4fd9fba9b6d03ee76f6bae5e5b17b02e165c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 23 Oct 2014 09:54:54 +0530 Subject: [PATCH] MOBI/AZW3 Output: When converting an EPUB document that specifies an SVG image as its cover image, convert the cover to JPEG as the Kindle cannot handle SVG cover images. See #1384375 (svg cover lost when converting ePub->AZW3) --- src/calibre/ebooks/__init__.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/calibre/ebooks/__init__.py b/src/calibre/ebooks/__init__.py index 76c988582c..dbbf94f27d 100644 --- a/src/calibre/ebooks/__init__.py +++ b/src/calibre/ebooks/__init__.py @@ -66,6 +66,14 @@ class HTMLRenderer(object): self.loop.exit(0) +def return_raster_image(path): + from calibre.utils.imghdr import what + if os.access(path, os.R_OK): + with open(path, 'rb') as f: + raw = f.read() + if what(None, raw) not in (None, 'svg'): + return raw + def extract_cover_from_embedded_svg(html, base, log): from lxml import etree from calibre.ebooks.oeb.base import XPath, SVG, XLINK @@ -75,9 +83,9 @@ def extract_cover_from_embedded_svg(html, base, log): if len(svg) == 1 and len(svg[0]) == 1 and svg[0][0].tag == SVG('image'): image = svg[0][0] href = image.get(XLINK('href'), None) - path = os.path.join(base, *href.split('/')) - if href and os.access(path, os.R_OK): - return open(path, 'rb').read() + if href: + path = os.path.join(base, *href.split('/')) + return return_raster_image(path) def extract_calibre_cover(raw, base, log): from calibre.ebooks.BeautifulSoup import BeautifulSoup @@ -89,8 +97,7 @@ def extract_calibre_cover(raw, base, log): images[0].get('alt', '')=='cover': img = images[0] img = os.path.join(base, *img['src'].split('/')) - if os.path.exists(img): - return open(img, 'rb').read() + return_raster_image(img) # Look for a simple cover, i.e. a body with no text and only one tag if matches is None: @@ -103,8 +110,7 @@ def extract_calibre_cover(raw, base, log): images = body.findAll('img', src=True) if 0 < len(images) < 2: img = os.path.join(base, *images[0]['src'].split('/')) - if os.path.exists(img): - return open(img, 'rb').read() + return_raster_image(img) def render_html_svg_workaround(path_to_html, log, width=590, height=750): from calibre.ebooks.oeb.base import SVG_NS