MOBI Output: Convert WebP images to PNG so they work with Amazon's software. Fixes #1922341 [epub image (WebP format) couldn't get into the mobi format](https://bugs.launchpad.net/calibre/+bug/1922341)

This commit is contained in:
Kovid Goyal 2021-04-02 16:29:06 +05:30
parent d1c22611b3
commit 2d68fbabb0
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 13 additions and 2 deletions

View File

@ -88,6 +88,9 @@ class Resources(object):
for item in self.oeb.manifest.values(): for item in self.oeb.manifest.values():
if item.media_type not in OEB_RASTER_IMAGES: if item.media_type not in OEB_RASTER_IMAGES:
continue continue
if item.media_type.lower() == 'image/webp':
self.log.info(f'Converting WebP image {item.href} to PNG')
item.convert_webp()
try: try:
data = self.process_image(item.data) data = self.process_image(item.data)
except: except:

View File

@ -321,6 +321,7 @@ GIF_MIME = types_map['.gif']
JPEG_MIME = types_map['.jpeg'] JPEG_MIME = types_map['.jpeg']
PNG_MIME = types_map['.png'] PNG_MIME = types_map['.png']
SVG_MIME = types_map['.svg'] SVG_MIME = types_map['.svg']
WEBP_MIME = types_map['.webp']
BINARY_MIME = 'application/octet-stream' BINARY_MIME = 'application/octet-stream'
XHTML_CSS_NAMESPACE = '@namespace "%s";\n' % XHTML_NS XHTML_CSS_NAMESPACE = '@namespace "%s";\n' % XHTML_NS
@ -328,7 +329,7 @@ XHTML_CSS_NAMESPACE = '@namespace "%s";\n' % XHTML_NS
OEB_STYLES = {CSS_MIME, OEB_CSS_MIME, 'text/x-oeb-css', 'xhtml/css'} OEB_STYLES = {CSS_MIME, OEB_CSS_MIME, 'text/x-oeb-css', 'xhtml/css'}
OEB_DOCS = {XHTML_MIME, 'text/html', OEB_DOC_MIME, OEB_DOCS = {XHTML_MIME, 'text/html', OEB_DOC_MIME,
'text/x-oeb-document'} 'text/x-oeb-document'}
OEB_RASTER_IMAGES = {GIF_MIME, JPEG_MIME, PNG_MIME} OEB_RASTER_IMAGES = {GIF_MIME, JPEG_MIME, PNG_MIME, WEBP_MIME}
OEB_IMAGES = {GIF_MIME, JPEG_MIME, PNG_MIME, SVG_MIME} OEB_IMAGES = {GIF_MIME, JPEG_MIME, PNG_MIME, SVG_MIME}
MS_COVER_TYPE = 'other.ms-coverimage-standard' MS_COVER_TYPE = 'other.ms-coverimage-standard'
@ -1144,6 +1145,13 @@ class Manifest(object):
href = os.path.normpath(href).replace('\\', '/') href = os.path.normpath(href).replace('\\', '/')
return href return href
def convert_webp(self):
from calibre.utils.img import image_and_format_from_data, image_to_data
img, fmt = image_and_format_from_data(self.data)
if fmt == 'webp' and not img.isNull():
self.data = image_to_data(img, fmt='PNG')
self.media_type = 'image/png'
def __init__(self, oeb): def __init__(self, oeb):
self.oeb = oeb self.oeb = oeb
self.items = set() self.items = set()

View File

@ -303,7 +303,7 @@ class BuildTest(unittest.TestCase):
# hard-coded paths of the Qt installation should work. If they do not, # hard-coded paths of the Qt installation should work. If they do not,
# then it is a distro problem. # then it is a distro problem.
fmts = set(map(lambda x: x.data().decode('utf-8'), QImageReader.supportedImageFormats())) # no2to3 fmts = set(map(lambda x: x.data().decode('utf-8'), QImageReader.supportedImageFormats())) # no2to3
testf = {'jpg', 'png', 'svg', 'ico', 'gif'} testf = {'jpg', 'png', 'svg', 'ico', 'gif', 'webp'}
self.assertEqual(testf.intersection(fmts), testf, "Qt doesn't seem to be able to load some of its image plugins. Available plugins: %s" % fmts) self.assertEqual(testf.intersection(fmts), testf, "Qt doesn't seem to be able to load some of its image plugins. Available plugins: %s" % fmts)
data = P('images/blank.png', allow_user_override=False, data=True) data = P('images/blank.png', allow_user_override=False, data=True)
img = image_from_data(data) img = image_from_data(data)