View Single Post
Old 01-25-2025, 09:12 AM   #7
epubli
Enthusiast
epubli began at the beginning.
 
Posts: 25
Karma: 10
Join Date: Nov 2012
Device: Pocketbook Inkpad 3
Code snippet

Here is a short code example to resize PNG images within a news recipe:

Code:
from calibre.utils.magick import Image

...
    def postprocess_html(self, soup, first):
        maxwidth=800
        # Shrink large PNG images
        for tag in soup.findAll(lambda tag: tag.name.lower()=='img' and tag.has_key('src')):
          iurl = tag['src']
          if iurl.upper().endswith('PNG'):
              img = Image()
              img.open(iurl)
              width, height = img.size
              if( width > maxwidth ):
                factor=maxwidth/width
                img.size=(int(width/16 * factor)*16, int(height/16 * factor)*16)
                img.save(iurl)
        return soup
epubli is offline   Reply With Quote