Clean up unused imports and variables, remove redundant code, and add ruff as a dev dependency.

This commit is contained in:
Esa Kataja
2024-12-04 00:22:25 +02:00
parent 6e083f39e8
commit b0c4810344
3 changed files with 46 additions and 16 deletions
+8 -16
View File
@@ -1,11 +1,7 @@
#!/usr/bin/env -S uv run
import os
import shutil
import subprocess
from pathlib import Path
from typing import List, Tuple
import click
import cv2
import img2pdf
@@ -13,7 +9,6 @@ import numpy as np
from pdf2image import convert_from_path
import settings
from settings import OptimizationLevel
from PIL import Image
import concurrent.futures
@@ -23,8 +18,6 @@ class Settings:
OPTIPNG_OPTIMIZATION_LEVEL = 7
PDF_BORDER_SIZE = 50
settings = Settings()
def ensure_temp_dir():
"""Ensure temporary directory exists and return its path."""
temp_dir = "temp_processed_images"
@@ -282,7 +275,7 @@ def run_optipng(file_path: str) -> bool:
bool: True if successful, False otherwise
"""
try:
result = subprocess.run(
subprocess.run(
['optipng', f'-o{settings.OPTIPNG_OPTIMIZATION_LEVEL}', file_path],
capture_output=True,
text=True,
@@ -307,13 +300,12 @@ def optimize(level):
2: Level 1 + convert to 1-bit monochrome
3: Level 2 + optipng optimization
"""
temp_dir = ensure_temp_dir()
temp_files = get_temp_files()
if not temp_files:
print("No pages found in temporary directory. Run 'extract' first.")
return
opt_level = OptimizationLevel(level)
opt_level = settings.OptimizationLevel(level)
total_files = len(temp_files)
successful = {
@@ -325,10 +317,10 @@ def optimize(level):
print(f"Processing {total_files} images at optimization level {level}...")
# Step 1: Convert to monochrome if level >= 2
if int(opt_level) >= int(OptimizationLevel.MONOCHROME):
if int(opt_level) >= int(settings.OptimizationLevel.MONOCHROME):
print("\nConverting to monochrome...")
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = {executor.submit(convert_to_monochrome, file_path, (opt_level == OptimizationLevel.MONOCHROME)): file_path for file_path in temp_files}
futures = {executor.submit(convert_to_monochrome, file_path, (opt_level == settings.OptimizationLevel.MONOCHROME)): file_path for file_path in temp_files}
for future in concurrent.futures.as_completed(futures):
file_path = futures[future]
try:
@@ -343,7 +335,7 @@ def optimize(level):
# Step 2: Always trim whitespace
print("\nTrimming whitespace from images...")
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = {executor.submit(trim_whitespace, file_path, (opt_level == OptimizationLevel.TRIM)): file_path for file_path in temp_files}
futures = {executor.submit(trim_whitespace, file_path, (opt_level == settings.OptimizationLevel.TRIM)): file_path for file_path in temp_files}
for future in concurrent.futures.as_completed(futures):
file_path = futures[future]
try:
@@ -356,7 +348,7 @@ def optimize(level):
print(f"{os.path.basename(file_path)} generated an exception: {exc}")
# Step 3: Run optipng if level = 3
if opt_level == OptimizationLevel.FULL:
if opt_level == settings.OptimizationLevel.FULL:
if check_optipng_installed():
print("\nOptimizing PNG files with optipng...")
with concurrent.futures.ThreadPoolExecutor() as executor:
@@ -377,9 +369,9 @@ def optimize(level):
# Print summary
print("\nOptimization complete!")
print(f"Successfully trimmed: {successful['trim']}/{total_files} images")
if int(opt_level) >= int(OptimizationLevel.MONOCHROME):
if int(opt_level) >= int(settings.OptimizationLevel.MONOCHROME):
print(f"Successfully converted to monochrome: {successful['monochrome']}/{total_files} images")
if opt_level == OptimizationLevel.FULL and check_optipng_installed():
if opt_level == settings.OptimizationLevel.FULL and check_optipng_installed():
print(f"Successfully optimized with optipng: {successful['optipng']}/{total_files} images")
@cli.command()