mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Use a generated image cover for coverless EPUB files as well
This commit is contained in:
parent
d9d20f0310
commit
6bed097e07
@ -440,7 +440,7 @@ def remove_cover_image_in_page(container, page, cover_images):
|
|||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
def set_epub_cover(container, cover_path, report, options=None):
|
def set_epub_cover(container, cover_path, report, options=None, image_callback=None):
|
||||||
existing_image = options is not None and options.get('existing_image', False)
|
existing_image = options is not None and options.get('existing_image', False)
|
||||||
if existing_image:
|
if existing_image:
|
||||||
existing_image = cover_path
|
existing_image = cover_path
|
||||||
@ -455,6 +455,7 @@ def set_epub_cover(container, cover_path, report, options=None):
|
|||||||
# TODO: Handle possible_removals and also iterate over links in the removed
|
# TODO: Handle possible_removals and also iterate over links in the removed
|
||||||
# pages and handle possibly removing stylesheets referred to by them.
|
# pages and handle possibly removing stylesheets referred to by them.
|
||||||
|
|
||||||
|
image_callback_called = False
|
||||||
spine_items = tuple(container.spine_items)
|
spine_items = tuple(container.spine_items)
|
||||||
if cover_page is None and spine_items:
|
if cover_page is None and spine_items:
|
||||||
# Check if the first item in the spine is a simple cover wrapper
|
# Check if the first item in the spine is a simple cover wrapper
|
||||||
@ -490,9 +491,15 @@ def set_epub_cover(container, cover_path, report, options=None):
|
|||||||
log('Existing cover page {} is a simple wrapper, removing it'.format(cover_page))
|
log('Existing cover page {} is a simple wrapper, removing it'.format(cover_page))
|
||||||
container.remove_item(cover_page)
|
container.remove_item(cover_page)
|
||||||
if wrapped_image != existing_image:
|
if wrapped_image != existing_image:
|
||||||
|
if image_callback is not None and not image_callback_called:
|
||||||
|
image_callback(cover_image, wrapped_image)
|
||||||
|
image_callback_called = True
|
||||||
container.remove_item(wrapped_image)
|
container.remove_item(wrapped_image)
|
||||||
updated = True
|
updated = True
|
||||||
|
|
||||||
|
if image_callback is not None and not image_callback_called:
|
||||||
|
image_callback_called = True
|
||||||
|
image_callback(cover_image, wrapped_image)
|
||||||
if cover_image and cover_image != wrapped_image:
|
if cover_image and cover_image != wrapped_image:
|
||||||
# Remove the old cover image
|
# Remove the old cover image
|
||||||
if cover_image != existing_image:
|
if cover_image != existing_image:
|
||||||
|
@ -277,10 +277,21 @@ class Container(ContainerBase):
|
|||||||
return BLANK_JPEG
|
return BLANK_JPEG
|
||||||
|
|
||||||
if input_fmt == 'epub':
|
if input_fmt == 'epub':
|
||||||
|
|
||||||
|
def image_callback(cover_image, wrapped_image):
|
||||||
|
if cover_image:
|
||||||
|
image_callback.cover_data = self.raw_data(cover_image, decode=False)
|
||||||
|
if wrapped_image and not getattr(image_callback, 'cover_data', None):
|
||||||
|
image_callback.cover_data = self.raw_data(wrapped_image, decode=False)
|
||||||
|
|
||||||
def cover_path(action, data):
|
def cover_path(action, data):
|
||||||
if action == 'write_image':
|
if action == 'write_image':
|
||||||
data.write(generic_cover())
|
cdata = getattr(image_callback, 'cover_data', None) or generic_cover()
|
||||||
raster_cover_name, titlepage_name = set_epub_cover(self, cover_path, (lambda *a: None), options={'template':templ})
|
data.write(cdata)
|
||||||
|
|
||||||
|
raster_cover_name, titlepage_name = set_epub_cover(
|
||||||
|
self, cover_path, (lambda *a: None), options={'template':templ},
|
||||||
|
image_callback=image_callback)
|
||||||
else:
|
else:
|
||||||
raster_cover_name = find_cover_image(self, strict=True)
|
raster_cover_name = find_cover_image(self, strict=True)
|
||||||
if raster_cover_name is None:
|
if raster_cover_name is None:
|
||||||
@ -611,18 +622,16 @@ def render(pathtoebook, output_dir, book_hash=None, serialize_metadata=False, ex
|
|||||||
mi = None
|
mi = None
|
||||||
if serialize_metadata:
|
if serialize_metadata:
|
||||||
from calibre.ebooks.metadata.meta import get_metadata
|
from calibre.ebooks.metadata.meta import get_metadata
|
||||||
with lopen(pathtoebook, 'rb') as f:
|
from calibre.customize.ui import quick_metadata
|
||||||
|
with lopen(pathtoebook, 'rb') as f, quick_metadata:
|
||||||
mi = get_metadata(f, os.path.splitext(pathtoebook)[1][1:].lower())
|
mi = get_metadata(f, os.path.splitext(pathtoebook)[1][1:].lower())
|
||||||
container = Container(pathtoebook, output_dir, book_hash=book_hash, save_bookmark_data=extract_annotations, book_metadata=mi)
|
container = Container(pathtoebook, output_dir, book_hash=book_hash, save_bookmark_data=extract_annotations, book_metadata=mi)
|
||||||
if serialize_metadata:
|
if serialize_metadata:
|
||||||
from calibre.utils.serialize import json_dumps
|
from calibre.utils.serialize import json_dumps
|
||||||
from calibre.ebooks.metadata.book.serialize import metadata_as_dict
|
from calibre.ebooks.metadata.book.serialize import metadata_as_dict
|
||||||
d = metadata_as_dict(mi)
|
d = metadata_as_dict(mi)
|
||||||
|
d.pop('cover_data', None)
|
||||||
serialize_datetimes(d), serialize_datetimes(d.get('user_metadata', {}))
|
serialize_datetimes(d), serialize_datetimes(d.get('user_metadata', {}))
|
||||||
cdata = d.pop('cover_data', None)
|
|
||||||
if cdata and cdata[1] and container.book_render_data['raster_cover_name']:
|
|
||||||
with lopen(os.path.join(output_dir, container.book_render_data['raster_cover_name']), 'wb') as f:
|
|
||||||
f.write(cdata[1])
|
|
||||||
with lopen(os.path.join(output_dir, 'calibre-book-metadata.json'), 'wb') as f:
|
with lopen(os.path.join(output_dir, 'calibre-book-metadata.json'), 'wb') as f:
|
||||||
f.write(json_dumps(d))
|
f.write(json_dumps(d))
|
||||||
if extract_annotations:
|
if extract_annotations:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user