diff --git a/src/calibre/ebooks/oeb/polish/images.py b/src/calibre/ebooks/oeb/polish/images.py index f20e49d1c6..49e75bc52c 100644 --- a/src/calibre/ebooks/oeb/polish/images.py +++ b/src/calibre/ebooks/oeb/polish/images.py @@ -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)