mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
When setting covers in calibre, resize to fit within a maximum size of (1200, 1600), to prevent slowdowns due to extra large covers. This size can be controlled via Preferences->Tweaks. Fixes #9277 (Restrict max cover size to 1000x1000)
This commit is contained in:
parent
9abee8e6e6
commit
1bef93de39
@ -349,3 +349,9 @@ public_smtp_relay_delay = 301
|
|||||||
# after a restart of calibre.
|
# after a restart of calibre.
|
||||||
draw_hidden_section_indicators = True
|
draw_hidden_section_indicators = True
|
||||||
|
|
||||||
|
#: The maximum width and height for covers saved in the calibre library
|
||||||
|
# All covers in the calibre library will be resized, preserving aspect ratio,
|
||||||
|
# to fit within this size. This is to prevent slowdowns caused by extremely
|
||||||
|
# large covers
|
||||||
|
maximum_cover_size = (1200, 1600)
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ from calibre.utils.config import prefs, tweaks, from_json, to_json
|
|||||||
from calibre.utils.icu import sort_key
|
from calibre.utils.icu import sort_key
|
||||||
from calibre.utils.search_query_parser import saved_searches, set_saved_searches
|
from calibre.utils.search_query_parser import saved_searches, set_saved_searches
|
||||||
from calibre.ebooks import BOOK_EXTENSIONS, check_ebook_format
|
from calibre.ebooks import BOOK_EXTENSIONS, check_ebook_format
|
||||||
from calibre.utils.magick.draw import save_cover_data_to
|
from calibre.utils.magick.draw import minify_image, save_cover_data_to
|
||||||
from calibre.utils.recycle_bin import delete_file, delete_tree
|
from calibre.utils.recycle_bin import delete_file, delete_tree
|
||||||
from calibre.utils.formatter_functions import load_user_template_functions
|
from calibre.utils.formatter_functions import load_user_template_functions
|
||||||
|
|
||||||
@ -951,6 +951,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
|||||||
if callable(getattr(data, 'read', None)):
|
if callable(getattr(data, 'read', None)):
|
||||||
data = data.read()
|
data = data.read()
|
||||||
try:
|
try:
|
||||||
|
data = minify_image(data, tweaks['maximum_cover_size'])
|
||||||
save_cover_data_to(data, path)
|
save_cover_data_to(data, path)
|
||||||
except (IOError, OSError):
|
except (IOError, OSError):
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
|
@ -12,6 +12,34 @@ from calibre.constants import __appname__, __version__
|
|||||||
from calibre.utils.config import tweaks
|
from calibre.utils.config import tweaks
|
||||||
from calibre import fit_image
|
from calibre import fit_image
|
||||||
|
|
||||||
|
def _data_to_image(data):
|
||||||
|
if isinstance(data, Image):
|
||||||
|
img = data
|
||||||
|
else:
|
||||||
|
img = Image()
|
||||||
|
img.load(data)
|
||||||
|
return img
|
||||||
|
|
||||||
|
def minify_image(data, minify_to=(1200, 1600), preserve_aspect_ratio=True):
|
||||||
|
'''
|
||||||
|
Minify image to specified size if image is bigger than specified
|
||||||
|
size and return minified image, otherwise, original image is
|
||||||
|
returned.
|
||||||
|
|
||||||
|
:param data: Image data as bytestring or Image object
|
||||||
|
:param minify_to: A tuple (width, height) to specify target size
|
||||||
|
:param preserve_aspect_ratio: whether preserve original aspect ratio
|
||||||
|
'''
|
||||||
|
img = _data_to_image(data)
|
||||||
|
owidth, oheight = img.size
|
||||||
|
nwidth, nheight = minify_to
|
||||||
|
if owidth <= nwidth and oheight <= nheight:
|
||||||
|
return img
|
||||||
|
if preserve_aspect_ratio:
|
||||||
|
scaled, nwidth, nheight = fit_image(owidth, oheight, nwidth, nheight)
|
||||||
|
img.size = (nwidth, nheight)
|
||||||
|
return img
|
||||||
|
|
||||||
def normalize_format_name(fmt):
|
def normalize_format_name(fmt):
|
||||||
fmt = fmt.lower()
|
fmt = fmt.lower()
|
||||||
if fmt == 'jpeg':
|
if fmt == 'jpeg':
|
||||||
@ -35,11 +63,7 @@ def save_cover_data_to(data, path, bgcolor='#ffffff', resize_to=None,
|
|||||||
|
|
||||||
'''
|
'''
|
||||||
changed = False
|
changed = False
|
||||||
if isinstance(data, Image):
|
img = _data_to_image(data)
|
||||||
img = data
|
|
||||||
else:
|
|
||||||
img = Image()
|
|
||||||
img.load(data)
|
|
||||||
orig_fmt = normalize_format_name(img.format)
|
orig_fmt = normalize_format_name(img.format)
|
||||||
fmt = os.path.splitext(path)[1]
|
fmt = os.path.splitext(path)[1]
|
||||||
fmt = normalize_format_name(fmt[1:])
|
fmt = normalize_format_name(fmt[1:])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user