From 0c5fd286dc5fb1582f8877ed47ed824be94f1099 Mon Sep 17 00:00:00 2001 From: Llloooggg Date: Thu, 15 May 2025 19:58:56 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D1=80=D0=B5=D0=B7=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE=20=D1=81=D0=B6=D0=B0=D1=82=D0=B8=D0=B5=20=D1=81=20Pillow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- image_compressor.py | 57 ++++----------------------------------------- 1 file changed, 5 insertions(+), 52 deletions(-) diff --git a/image_compressor.py b/image_compressor.py index 9daf69c..98e731b 100644 --- a/image_compressor.py +++ b/image_compressor.py @@ -186,43 +186,7 @@ def compress_with_external( return False, path -def compress_with_pillow(path: Path) -> Tuple[bool, Path]: - original_size = path.stat().st_size - temp_path = path.with_name(path.stem + ".pillowtmp" + path.suffix) - - try: - with Image.open(path) as img: - exif = img.info.get("exif") - img_format = img.format - quality = 85 - while quality >= 50: - img.save( - temp_path, - format=img_format, - optimize=True, - quality=quality, - exif=exif, - ) - if temp_path.stat().st_size <= TARGET_SIZE: - break - quality -= 5 - - if temp_path.exists(): - if temp_path.stat().st_size < original_size: - temp_path.replace(path) - return True, path - logging.error( - f"Не удалось сжать (не уменьшилось): {path} ({original_size // 1024} KB)" - ) - temp_path.unlink() - except Exception as e: - logging.error(f"Ошибка при сжатии {path} Pillow: {e}") - if temp_path.exists(): - temp_path.unlink() - return False, path - - -def compress_image(path: Path, use_fallback: bool = False): +def compress_image(path: Path): global processed_count, skipped_count, skipped_size_count, error_count, total_saved_bytes, total_images_original_size, total_images_new_size, processed_hashes try: @@ -271,9 +235,6 @@ def compress_image(path: Path, use_fallback: bool = False): ext = path.suffix.lower() result, final_path = compress_with_external(path, ext) - if not result and use_fallback: - result, final_path = compress_with_pillow(path) - new_size = final_path.stat().st_size total_images_new_size += new_size @@ -372,28 +333,20 @@ def main(): required = ["cjpeg-static.exe", "cwebp.exe"] missing = [t for t in required if not get_tool_path(t).exists()] - use_fallback = False if missing: print("Не найдены:", ", ".join(missing)) - choice = input("Использовать Pillow? [y/n]: ").strip().lower() - if choice != "y": - print("Работа прервана.") - return - use_fallback = True + logging.error("Не найдены:", ", ".join(missing)) + return total_original_size = get_folder_size(input_dir) files = prepare_and_copy_files(input_dir, output_dir) total_files = len(files) print(f"Найдено {total_files} изображений.") - logging.info( - f"Начато. Найдено {total_files} изображений. Fallback = {use_fallback}" - ) + logging.info(f"Начато. Найдено {total_files} изображений.") with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor: - futures = [ - executor.submit(compress_image, f, use_fallback) for f in files - ] + futures = [executor.submit(compress_image, f) for f in files] for i, _ in enumerate(as_completed(futures), 1): print(f"\rОбработка: {i}/{len(files)}", end="")