View Single Post
Old 08-30-2021, 05:00 AM   #11
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,118
Karma: 1954138
Join Date: Aug 2015
Device: Kindle
Trim cover action

Note: This action is now available as part of the builtin Single Field Edit Action.

Code:
from functools import partial

from qt.core 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 TrimCover(ChainAction):

    name = 'Trim Cover'
    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()

Last edited by capink; 01-07-2022 at 06:11 AM.
capink is offline   Reply With Quote