View Single Post
Old 11-25-2019, 04:58 AM   #2
Doitsu
Grand Sorcerer
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 5,736
Karma: 24031401
Join Date: Dec 2010
Device: Kindle PW2
Quote:
Originally Posted by Vroni View Post
Has someone another idea to speed this process up? Did i miss something?
Since you know some Python, you could use BeautifulSoup to get all image tags and PIL to get the image size. The following Edit plugin code should work for you:

Spoiler:
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os
from io import BytesIO
from PIL import Image
from sigil_bs4 import BeautifulSoup

def run(bk):

    # preferences
    max_width = 50
    fleuron_name = 'fleuron.jpg'

    # process file list
    for (html_id, file_href) in bk.text_iter():
        file_name = os.path.basename(file_href)
        print('Processing {}...\n'.format(file_name))
        html = bk.readfile(html_id)

        # load html code into BeautifulSoup
        soup = BeautifulSoup(html, 'html.parser')
        orig_soup = str(soup)
 
        # look for images
        img_tags = soup.find_all('img')
        for img in img_tags:
            if 'src' in img.attrs:
                href = img['src']
                base_name = os.path.basename(href)
                id = bk.basename_to_id(base_name)
                if id:
                    # get image file size
                    imgdata = bk.readfile(id)
                    img_data = Image.open(BytesIO(imgdata)).convert('L')
                    width, height = img_data.size
                    if width <= max_width:
                        img['src'] = href.replace(base_name, fleuron_name)
                        print('{} renamed to {}'.format(base_name, fleuron_name))
                        
            else:
                print(img['src'] + ' skipped! (empty img tag)\n')

        if str(soup) != orig_soup:
            bk.writefile(html_id, str(soup.prettyprint_xhtml()))
            print('\n{} updated\n'.format(file_name))
    
    print('\nPlease click OK to close the Plugin Runner window.')
    
    return 0

def main():
    print('I reached main when I should not have\n')
    return -1

if __name__ == "__main__":
    sys.exit(main())


It looks for images with a width of up to 50 pixels and changes the file name in the img src attribute to fleuron.jpg.

If that code catches too many false positives, you might find KevinH's Access-Aide plugin helpful. Simply change the alt attribute of all fleurons to fleuron and then use a regex to change the file name of all images with a fleuron alt attribute.
Doitsu is offline   Reply With Quote