Edit book/book polishing: When compressing images and the compressed image is larger thant he original, use the original image. Fixes #1741063 [Compress images in Edit Ebook results in larger images](https://bugs.launchpad.net/calibre/+bug/1741063)

This commit is contained in:
Kovid Goyal 2018-01-03 20:49:55 +05:30
parent f4272115bf
commit 406ae29c1b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -54,8 +54,14 @@ class Worker(Thread):
func = partial(encode_jpeg, quality=self.jpeg_quality)
path = self.container.get_file_path_for_processing(name)
before = os.path.getsize(path)
with lopen(path, 'rb') as f:
old_data = f.read()
func(path)
after = os.path.getsize(path)
if after >= before:
with lopen(path, 'wb') as f:
f.write(old_data)
after = before
self.results[name] = (True, (before, after))
@ -89,12 +95,14 @@ def compress_images(container, report=None, names=None, jpeg_quality=None, progr
name = force_unicode(name, filesystem_encoding)
if ok:
before, after = res
if before != after:
before_total += before
after_total += after
if report:
before_total += before
after_total += after
if report:
if before != after:
report(_('{0} compressed from {1} to {2} bytes [{3:.1%} reduction]').format(
name, human_readable(before), human_readable(after), (before - after)/before))
else:
report(_('{0} could not be further compressed').format(name))
else:
report(_('Failed to process {0} with error:').format(name))
report(res)