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)

This commit is contained in:
Kovid Goyal 2014-10-23 09:54:54 +05:30
parent 41ce07ea7a
commit 9b5a4fd9fb

View File

@ -66,6 +66,14 @@ class HTMLRenderer(object):
self.loop.exit(0) 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): def extract_cover_from_embedded_svg(html, base, log):
from lxml import etree from lxml import etree
from calibre.ebooks.oeb.base import XPath, SVG, XLINK 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'): if len(svg) == 1 and len(svg[0]) == 1 and svg[0][0].tag == SVG('image'):
image = svg[0][0] image = svg[0][0]
href = image.get(XLINK('href'), None) href = image.get(XLINK('href'), None)
if href:
path = os.path.join(base, *href.split('/')) path = os.path.join(base, *href.split('/'))
if href and os.access(path, os.R_OK): return return_raster_image(path)
return open(path, 'rb').read()
def extract_calibre_cover(raw, base, log): def extract_calibre_cover(raw, base, log):
from calibre.ebooks.BeautifulSoup import BeautifulSoup from calibre.ebooks.BeautifulSoup import BeautifulSoup
@ -89,8 +97,7 @@ def extract_calibre_cover(raw, base, log):
images[0].get('alt', '')=='cover': images[0].get('alt', '')=='cover':
img = images[0] img = images[0]
img = os.path.join(base, *img['src'].split('/')) img = os.path.join(base, *img['src'].split('/'))
if os.path.exists(img): return_raster_image(img)
return open(img, 'rb').read()
# Look for a simple cover, i.e. a body with no text and only one <img> tag # Look for a simple cover, i.e. a body with no text and only one <img> tag
if matches is None: if matches is None:
@ -103,8 +110,7 @@ def extract_calibre_cover(raw, base, log):
images = body.findAll('img', src=True) images = body.findAll('img', src=True)
if 0 < len(images) < 2: if 0 < len(images) < 2:
img = os.path.join(base, *images[0]['src'].split('/')) img = os.path.join(base, *images[0]['src'].split('/'))
if os.path.exists(img): return_raster_image(img)
return open(img, 'rb').read()
def render_html_svg_workaround(path_to_html, log, width=590, height=750): def render_html_svg_workaround(path_to_html, log, width=590, height=750):
from calibre.ebooks.oeb.base import SVG_NS from calibre.ebooks.oeb.base import SVG_NS