View Single Post
Old 07-26-2021, 11:03 AM   #3
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,203
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
If you are familiar with the Action Chains plugin, you can achieve what you want:
  • Copy past this custom action into the plugin's module editor
    Code:
    from functools import partial
    
    from PyQt5.Qt import (QApplication, Qt)
    
    from calibre.gui2 import error_dialog
    
    from calibre_plugins.action_chains.common_utils import DoubleProgressDialog
    from calibre_plugins.action_chains.actions.base import ChainAction
    
    class Test(ChainAction):
    
        name = 'Trim Covers'
        support_scopes = True
    
        def trim_covers(self, db, book_ids, pbar):
    
            from calibre.utils.img import (
                image_from_data, image_to_data, remove_borders_from_image
            )
            
            pbar.update_overall(len(book_ids))
            
            for book_id in book_ids:
                cdata = db.new_api.cover(book_id)
                if cdata:
                    img = image_from_data(cdata)
                    nimg = remove_borders_from_image(img)
                    if nimg is not img:
                        cdata = image_to_data(nimg)
                        db.new_api.set_cover({book_id:cdata})
                msg = _('Trimming cover for book_id: {}'.format(book_id))
                pbar.update_progress(1, msg)
    
        def run(self, gui, settings, chain):
            db = gui.current_db
            book_ids = chain.scope().get_book_ids()
    
            callback = partial(self.trim_covers, db, book_ids)
            pd = DoubleProgressDialog(1, callback, gui, window_title=_('Trimming ...'))
    
            gui.tags_view.blockSignals(True)
            QApplication.setOverrideCursor(Qt.ArrowCursor)
            try:
                pd.exec_()
    
                pd.thread = None
    
                if pd.error is not None:
                    return error_dialog(gui, _('Failed'),
                            pd.error[0], det_msg=pd.error[1],
                            show=True)
            finally:
                QApplication.restoreOverrideCursor()
                gui.tags_view.recount()
  • Create a chain containing the action you just added: Trim Covers.
  • Assign a keyboard shortcut your want to the chain (Add/Modify Chains > Keyboard shortcuts)
capink is offline   Reply With Quote