From dec2f6aa14d90855eced7566c153acf9069e14b8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 5 Mar 2011 07:44:38 -0700 Subject: [PATCH] Fix #9286 (Paste cover fails in 0.7.48) --- src/calibre/library/database2.py | 3 +-- src/calibre/utils/magick/draw.py | 12 +++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index e7d55da9df..2554df93e6 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -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.search_query_parser import saved_searches, set_saved_searches from calibre.ebooks import BOOK_EXTENSIONS, check_ebook_format -from calibre.utils.magick.draw import minify_image, save_cover_data_to +from calibre.utils.magick.draw import save_cover_data_to from calibre.utils.recycle_bin import delete_file, delete_tree from calibre.utils.formatter_functions import load_user_template_functions @@ -951,7 +951,6 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): if callable(getattr(data, 'read', None)): data = data.read() try: - data = minify_image(data, tweaks['maximum_cover_size']) save_cover_data_to(data, path) except (IOError, OSError): time.sleep(0.2) diff --git a/src/calibre/utils/magick/draw.py b/src/calibre/utils/magick/draw.py index 615aab9bd0..42659d70cc 100644 --- a/src/calibre/utils/magick/draw.py +++ b/src/calibre/utils/magick/draw.py @@ -47,7 +47,7 @@ def normalize_format_name(fmt): return fmt def save_cover_data_to(data, path, bgcolor='#ffffff', resize_to=None, - return_data=False, compression_quality=90): + return_data=False, compression_quality=90, minify_to=None): ''' Saves image in data to path, in the format specified by the path extension. Removes any transparency. If there is no transparency and no @@ -60,6 +60,9 @@ def save_cover_data_to(data, path, bgcolor='#ffffff', resize_to=None, compression (lossless). :param bgcolor: The color for transparent pixels. Must be specified in hex. :param resize_to: A tuple (width, height) or None for no resizing + :param minify_to: A tuple (width, height) to specify target size. The image + will be resized to fit into this target size. If None the value from the + tweak is used. ''' changed = False @@ -71,11 +74,18 @@ def save_cover_data_to(data, path, bgcolor='#ffffff', resize_to=None, if resize_to is not None: img.size = (resize_to[0], resize_to[1]) changed = True + owidth, oheight = img.size + nwidth, nheight = tweaks['maximum_cover_size'] if minify_to is None else minify_to + scaled, nwidth, nheight = fit_image(owidth, oheight, nwidth, nheight) + if scaled: + img.size = (nwidth, nheight) + changed = True if img.has_transparent_pixels(): canvas = create_canvas(img.size[0], img.size[1], bgcolor) canvas.compose(img) img = canvas changed = True + if not changed: changed = fmt != orig_fmt