![]() |
#1 |
Wannabe Connoisseur
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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'}: Code:
if self.compress_news_images: (I know enough python to be dangerous, but probably not be useful!) |
![]() |
![]() |
![]() |
#2 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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.
|
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Wannabe Connoisseur
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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 |
![]() |
![]() |
![]() |
#4 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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.
|
![]() |
![]() |
![]() |
#5 |
Wannabe Connoisseur
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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.
|
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Enthusiast
![]() Posts: 27
Karma: 10
Join Date: Nov 2024
Device: Tolino 4HD
|
|
![]() |
![]() |
![]() |
#7 |
Member
![]() 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 |
![]() |
![]() |
![]() |
Thread Tools | Search this Thread |
|
![]() |
||||
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 |