def optimize(name): if abort is not None and abort.is_set(): return if error_occurred.is_set(): return try: i = next(num) if progress is not None: progress(i, _('Optimizing %s') % name) srcpath = os.path.join(report.path, name) ext = srcpath.rpartition('.')[-1].lower() if ext == 'png': optimize_png(srcpath) elif ext in ('jpg', 'jpeg'): optimize_jpeg(srcpath) except Exception: return sys.exc_info()
def process_image(self, data): if not self.process_images: return data try: return mobify_image(data) except Exception: if 'png' != what(None, data): raise with PersistentTemporaryFile(suffix='.png') as pt: pt.write(data) try: from calibre.utils.img import optimize_png optimize_png(pt.name) data = lopen(pt.name, 'rb').read() finally: os.remove(pt.name) return mobify_image(data)
def process_image(self, data): if not self.process_images: return data func = mobify_image if self.opts.mobi_keep_original_images else rescale_image try: return func(data) except Exception: if 'png' != what(None, data): raise with PersistentTemporaryFile(suffix='.png') as pt: pt.write(data) try: from calibre.utils.img import optimize_png optimize_png(pt.name) data = lopen(pt.name, 'rb').read() finally: os.remove(pt.name) return func(data)
def process_image(self, data): if not self.process_images: return data func = mobify_image if self.opts.mobi_keep_original_images else rescale_image try: return ensure_jpeg_has_jfif(func(data)) except Exception: if 'png' != what(None, data): raise with PersistentTemporaryFile(suffix='.png') as pt: pt.write(data) try: from calibre.utils.img import optimize_png optimize_png(pt.name) data = lopen(pt.name, 'rb').read() finally: os.remove(pt.name) return func(data)
def process_image(self, data): if not self.process_images: return data func = mobify_image if self.opts.mobi_keep_original_images else rescale_image try: return func(data) except Exception: from calibre.utils.magick.draw import identify_data if 'png' != identify_data(data)[-1].lower(): raise with PersistentTemporaryFile(suffix='.png') as pt: pt.write(data) try: from calibre.utils.img import optimize_png optimize_png(pt.name) data = open(pt.name, 'rb').read() finally: os.remove(pt.name) return func(data)
def test_image_compression(): from calibre.utils.img import optimize_png, optimize_jpeg, encode_jpeg with TemporaryDirectory() as tdir, CurrentDir(tdir): shutil.copyfile(I('devices/kindle.jpg'), 'test.jpg') ret = optimize_jpeg('test.jpg') if ret is not None: raise SystemExit(ret) ret = encode_jpeg('test.jpg') if ret is not None: raise SystemExit(ret) shutil.copyfile(I('lt.png'), 'test.png') ret = optimize_png('test.png') if ret is not None: raise SystemExit(ret) print('Image compression OK!')