Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Calibre > Recipes

Notices

Reply
 
Thread Tools Search this Thread
Old 08-03-2023, 08:42 AM   #1
snarkophilus
Wannabe Connoisseur
snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.
 
Posts: 426
Karma: 2516674
Join Date: Apr 2011
Location: Geelong, Australia
Device: Kobo Libra 2, Kobo Aura 2, Sony PRS-T1, Sony PRS-350, Palm TX
compress_news_images and png images?

Would it be much work to make compress_news_images work for PNG files too? One news site I use has some 3-10MB PNG images that would be nice to resize like is already possible for JPG files.

I see that scale_image() in src/calibre/utils/img.py looks like it can deal with PNG files, but rescale_image() in src/calibre/web/fetch/simple.py only tries to rescale JPEG/JPG files. It looks like we convert anything that isn't PNG/JPEG/JPEG to these formats, so could it be as simple as changing the rescale check from
Code:
                    if self.compress_news_images and itype in {'jpg','jpeg'}:
to
Code:
                    if self.compress_news_images:
?

(I know enough python to be dangerous, but probably not be useful!)
snarkophilus is offline   Reply With Quote
Old 08-03-2023, 09:38 AM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,027
Karma: 27109258
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Look at the implementationof rescale_images() in fetch/utils.py you will see it works by using a compression_quality parameter which is decreased in steps to get the desired file size. There is no such parameter in PNG since it is a lossless format.
kovidgoyal is offline   Reply With Quote
Advert
Old 08-03-2023, 11:04 AM   #3
snarkophilus
Wannabe Connoisseur
snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.
 
Posts: 426
Karma: 2516674
Join Date: Apr 2011
Location: Geelong, Australia
Device: Kobo Libra 2, Kobo Aura 2, Sony PRS-T1, Sony PRS-350, Palm TX
Ah, of course! I forgot that PNG was lossless.

I grabbed a couple of these large PNGs and using ImageMagick's convert program and could shrink these to a much smaller files that are still more than usable for a news recipe. These are converted with convert -resize 100000@ :

Code:
img15.png PNG 1975x465 1975x465+0+0 8-bit sRGB 631111B 0.000u 0:00.000
img15_small.png PNG 651x153 651x153+0+0 8-bit sRGB 70669B 0.000u 0:00.000
img1_u1.png PNG 3840x2160 3840x2160+0+0 8-bit sRGB 6.93234MiB 0.000u 0:00.000
img1_u1_small.png PNG 421x237 421x237+0+0 8-bit sRGB 168503B 0.000u 0:00.000

-rw-rw-rw-  1 simonb   631111 Aug  1 06:01 img15.png
-rw-r--r--  1 simonb    70669 Aug  4 00:38 img15_small.png
-rw-rw-rw-  1 simonb  7269085 Aug  1 06:01 img1_u1.png
-rw-r--r--  1 simonb   168503 Aug  4 00:38 img1_u1_small.png
I see that calibre used to have a dependancy on ImageMagick but no longer does. Do we have any other options for doing something like what the convert program does?
snarkophilus is offline   Reply With Quote
Old 08-03-2023, 12:50 PM   #4
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,027
Karma: 27109258
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
You can resize images as much as you like, using the function in utils/img.py to load a QImage and then using the QImage::scaled() function.
kovidgoyal is offline   Reply With Quote
Old 08-03-2023, 09:40 PM   #5
snarkophilus
Wannabe Connoisseur
snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.snarkophilus ought to be getting tired of karma fortunes by now.
 
Posts: 426
Karma: 2516674
Join Date: Apr 2011
Location: Geelong, Australia
Device: Kobo Libra 2, Kobo Aura 2, Sony PRS-T1, Sony PRS-350, Palm TX
Ok, thanks. I'll have a look and see if I can hook that into a recipe.
snarkophilus is offline   Reply With Quote
Advert
Old 11-05-2024, 03:02 PM   #6
MasterPlexus
Enthusiast
MasterPlexus began at the beginning.
 
Posts: 27
Karma: 10
Join Date: Nov 2024
Device: Tolino 4HD
Quote:
Originally Posted by snarkophilus View Post
Ok, thanks. I'll have a look and see if I can hook that into a recipe.
Hi. Did you succeed? I'm also interested to shrink png's, as my magazine is quite too big with them

A short reusable example would be great!
MasterPlexus is offline   Reply With Quote
Old 01-25-2025, 08:12 AM   #7
epubli
Member
epubli began at the beginning.
 
Posts: 23
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
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PNG images lost in conversion Sarmat89 Conversion 7 08-21-2020 05:24 PM
Converting pdf to png images roger64 Workshop 8 09-06-2019 05:58 PM
PNG images losing quality. piau9000 Calibre 6 02-03-2018 01:02 PM
How to replace PNG images by SVG in epub? j.p.s ePub 38 08-12-2017 04:27 AM
No glyph available error and cannot compress png images crouton Editor 1 01-06-2017 10:02 PM


All times are GMT -4. The time now is 05:34 AM.


MobileRead.com is a privately owned, operated and funded community.