From: Radek Czajka Date: Wed, 22 May 2024 09:47:40 +0000 (+0200) Subject: Limit image size, fixes #4464. X-Git-Tag: 24.5 X-Git-Url: https://git.mdrn.pl/librarian.git/commitdiff_plain/88f39c4f90a67164b2c3da7c7a41df420f3b17ab?ds=inline;hp=9c9a55d7dda5595a236f11977d7f21f804d37872 Limit image size, fixes #4464. --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 171bb9b..13a3341 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ This document records all notable changes to Librarian. +## 24.5 + +- Smaller images in EPUB (600px width instead of 1200px). +- Convert PNG to JPEGs if too large in EPUBs. + ## 24.4 - Add fundraising inserts in PDF. diff --git a/setup.py b/setup.py index a11c86e..494b3f2 100755 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ def whole_tree(prefix, path): setup( name='librarian', - version='24.4.1', + version='24.5', description='Converter from WolneLektury.pl XML-based language to XHTML, TXT and other formats', author="Marek Stępniowski", author_email='marek@stepniowski.com', diff --git a/src/librarian/elements/figures/ilustr.py b/src/librarian/elements/figures/ilustr.py index 33099f6..4e848ac 100644 --- a/src/librarian/elements/figures/ilustr.py +++ b/src/librarian/elements/figures/ilustr.py @@ -8,6 +8,9 @@ from PIL import Image from ..base import WLElement +MAX_PNG_WEIGHT = 200000 + + class Ilustr(WLElement): SHOULD_HAVE_ID = True @@ -28,7 +31,7 @@ class Ilustr(WLElement): 'PNG': ('PNG', 'png', 'image/png'), }.get(img.format, ('JPEG', 'jpg', 'image/jpeg')) - width = 1200 + width = 600 if img.size[0] < width: th = img else: @@ -36,6 +39,19 @@ class Ilustr(WLElement): buffer = io.BytesIO() th.save(buffer, format=th_format) + + # Limit PNG to 200K. If larger, convert to JPEG. + if th_format == 'PNG' and buffer.tell() > MAX_PNG_WEIGHT: + th_format, ext, media_type = 'JPEG', 'jpg', 'image/jpeg' + if th.mode != 'RGB': + buffer = io.BytesIO() + th = Image.alpha_composite( + Image.new('RGBA', th.size, '#fff'), + th.convert('RGBA') + ) + th = th.convert('RGB') + th.save(buffer, format=th_format) + imgfile.close() file_name = 'image%d.%s' % ( builder.assign_image_number(),