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 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!')
def compress_worker(input_queue, output_queue, jpeg_quality): while True: task = input_queue.get() if task is None: break book_id, path = task try: if jpeg_quality >= 100: stderr = optimize_jpeg(path) else: stderr = encode_jpeg(path, jpeg_quality) except Exception: import traceback stderr = traceback.format_exc() if stderr: output_queue.put((book_id, stderr)) else: try: sz = os.path.getsize(path) except OSError as err: sz = str(err) output_queue.put((book_id, sz))