From 31e66b8bc55d69016362ab965a7599963698d2e0 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Tue, 1 Feb 2011 14:15:04 +0000 Subject: [PATCH] 1) fix problem where new covers are not sent as thumbnails even if the options ask for it. 2) permit not respecting aspect ratio when generating cover thumbnails --- src/calibre/devices/interface.py | 10 ++++++++++ src/calibre/devices/prs505/driver.py | 20 +++++++++++++++++--- src/calibre/gui2/device.py | 26 ++++++++++++++++++++++++-- src/calibre/utils/magick/draw.py | 11 +++++++++-- 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/calibre/devices/interface.py b/src/calibre/devices/interface.py index 2a92f46e8d..bc442f5853 100644 --- a/src/calibre/devices/interface.py +++ b/src/calibre/devices/interface.py @@ -35,6 +35,16 @@ class DevicePlugin(Plugin): #: Height for thumbnails on the device THUMBNAIL_HEIGHT = 68 + #: Width for thumbnails on the device. Setting this will force thumbnails + #: to this size, not preserving aspect ratio. If it is not set, then + #: the aspect ratio will be preserved and the thumbnail will be no higher + #: than THUMBNAIL_HEIGHT + # THUMBNAIL_WIDTH = 68 + + #: Set this to True if the device supports updating cover thumbnails during + #: sync_booklists. Setting it to true will ask device.py to refresh the + #: cover thumbnails during book matching + WANTS_UPDATED_THUMBNAILS = False #: Whether the metadata on books can be set via the GUI. CAN_SET_METADATA = ['title', 'authors', 'collections'] diff --git a/src/calibre/devices/prs505/driver.py b/src/calibre/devices/prs505/driver.py index 0f6668891a..4d3ac31540 100644 --- a/src/calibre/devices/prs505/driver.py +++ b/src/calibre/devices/prs505/driver.py @@ -81,12 +81,19 @@ class PRS505(USBMS): _('Set this option to have separate book covers uploaded ' 'every time you connect your device. Unset this option if ' 'you have so many books on the reader that performance is ' - 'unacceptable.') + 'unacceptable.'), + _('Preserve cover aspect ratio when building thumbnails') + + ':::' + + _('Set this option if you want the cover thumbnails to have ' + 'the same aspect ratio (width to height) as the cover. ' + 'Unset it if you want the thumbnail to be the maximum size, ' + 'ignoring aspect ratio.') ] EXTRA_CUSTOMIZATION_DEFAULT = [ ', '.join(['series', 'tags']), False, - False + False, + True ] OPT_COLLECTIONS = 0 @@ -96,7 +103,7 @@ class PRS505(USBMS): plugboard = None plugboard_func = None - THUMBNAIL_HEIGHT = 200 + THUMBNAIL_HEIGHT = 217 MAX_PATH_LEN = 201 # 250 - (max(len(CACHE_THUMBNAIL), len(MEDIA_THUMBNAIL)) + # len('main_thumbnail.jpg') + 1) @@ -138,6 +145,13 @@ class PRS505(USBMS): if not write_cache(self._card_b_prefix): self._card_b_prefix = None self.booklist_class.rebuild_collections = self.rebuild_collections + # Set the thumbnail width to the theoretical max if the user has asked + # that we do not preserve aspect ratio + if not self.settings().extra_customization[3]: + self.THUMBNAIL_WIDTH = 168 + # Set CAN_UPDATE_THUMBNAILS if the user has asked that thumbnails be + # updated on every connect + self.WANTS_UPDATED_THUMBNAILS = self.settings().extra_customization[2] def get_device_information(self, end_session=True): return (self.gui_name, '', '', '') diff --git a/src/calibre/gui2/device.py b/src/calibre/gui2/device.py index a5066a99ef..bf8c734089 100644 --- a/src/calibre/gui2/device.py +++ b/src/calibre/gui2/device.py @@ -871,6 +871,16 @@ class DeviceMixin(object): # {{{ self.send_by_mail(to, fmts, delete) def cover_to_thumbnail(self, data): + if self.device_manager.device and \ + hasattr(self.device_manager.device, 'THUMBNAIL_WIDTH'): + try: + return thumbnail(data, + self.device_manager.device.THUMBNAIL_WIDTH, + self.device_manager.device.THUMBNAIL_HEIGHT, + preserve_aspect_ratio=False) + except: + pass + return ht = self.device_manager.device.THUMBNAIL_HEIGHT \ if self.device_manager else DevicePlugin.THUMBNAIL_HEIGHT try: @@ -1272,6 +1282,8 @@ class DeviceMixin(object): # {{{ x = x.lower() if x else '' return string_pat.sub('', x) + update_metadata = prefs['manage_device_metadata'] == 'on_connect' + # Force a reset if the caches are not initialized if reset or not hasattr(self, 'db_book_title_cache'): # Build a cache (map) of the library, so the search isn't On**2 @@ -1284,8 +1296,13 @@ class DeviceMixin(object): # {{{ except: return False + get_covers = False + if update_metadata and self.device_manager.is_device_connected: + if self.device_manager.device.WANTS_UPDATED_THUMBNAILS: + get_covers = True + for id in db.data.iterallids(): - mi = db.get_metadata(id, index_is_id=True) + mi = db.get_metadata(id, index_is_id=True, get_cover=get_covers) title = clean_string(mi.title) if title not in db_book_title_cache: db_book_title_cache[title] = \ @@ -1311,7 +1328,6 @@ class DeviceMixin(object): # {{{ # the application_id to the db_id of the matching book. This value # will be used by books_on_device to indicate matches. - update_metadata = prefs['manage_device_metadata'] == 'on_connect' for booklist in booklists: for book in booklist: book.in_library = None @@ -1382,6 +1398,12 @@ class DeviceMixin(object): # {{{ if update_metadata: if self.device_manager.is_device_connected: + if self.device_manager.device.CAN_UPDATE_THUMBNAILS: + for blist in booklists: + for book in blist: + if book.cover and os.access(book.cover, os.R_OK): + book.thumbnail = \ + self.cover_to_thumbnail(open(book.cover, 'rb').read()) plugboards = self.library_view.model().db.prefs.get('plugboards', {}) self.device_manager.sync_booklists( Dispatcher(self.metadata_synced), booklists, diff --git a/src/calibre/utils/magick/draw.py b/src/calibre/utils/magick/draw.py index ad4b681b43..111f22cb5b 100644 --- a/src/calibre/utils/magick/draw.py +++ b/src/calibre/utils/magick/draw.py @@ -72,11 +72,18 @@ def save_cover_data_to(data, path, bgcolor='#ffffff', resize_to=None, f.write(data) return ret -def thumbnail(data, width=120, height=120, bgcolor='#ffffff', fmt='jpg'): +def thumbnail(data, width=120, height=120, bgcolor='#ffffff', fmt='jpg', + preserve_aspect_ratio=True): img = Image() img.load(data) owidth, oheight = img.size - scaled, nwidth, nheight = fit_image(owidth, oheight, width, height) + if not preserve_aspect_ratio: + scaled = owidth > width or oheight > height + nwidth = width + nheight = height + else: + scaled, nwidth, nheight = fit_image(owidth, oheight, width, height) + print 'in thumbnail', scaled, nwidth, nheight if scaled: img.size = (nwidth, nheight) canvas = create_canvas(img.size[0], img.size[1], bgcolor)