Implement parallel processing for optimization steps using ThreadPoolExecutor.
This commit is contained in:
+37
-23
@@ -15,6 +15,7 @@ from pdf2image import convert_from_path
|
|||||||
import settings
|
import settings
|
||||||
from settings import OptimizationLevel
|
from settings import OptimizationLevel
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
import concurrent.futures
|
||||||
|
|
||||||
class Settings:
|
class Settings:
|
||||||
TRIM_PADDING_PIXELS = 20
|
TRIM_PADDING_PIXELS = 20
|
||||||
@@ -326,37 +327,50 @@ def optimize(level):
|
|||||||
# Step 1: Convert to monochrome if level >= 2
|
# Step 1: Convert to monochrome if level >= 2
|
||||||
if int(opt_level) >= int(OptimizationLevel.MONOCHROME):
|
if int(opt_level) >= int(OptimizationLevel.MONOCHROME):
|
||||||
print("\nConverting to monochrome...")
|
print("\nConverting to monochrome...")
|
||||||
for i, file_path in enumerate(temp_files, 1):
|
with concurrent.futures.ThreadPoolExecutor() as executor:
|
||||||
print(f"[{i}/{total_files}] Converting {os.path.basename(file_path)}...", end='', flush=True)
|
futures = {executor.submit(convert_to_monochrome, file_path, (opt_level == OptimizationLevel.MONOCHROME)): file_path for file_path in temp_files}
|
||||||
# Only optimize if this is the final step (level 2)
|
for future in concurrent.futures.as_completed(futures):
|
||||||
if convert_to_monochrome(file_path, is_final=(opt_level == OptimizationLevel.MONOCHROME)):
|
file_path = futures[future]
|
||||||
successful['monochrome'] += 1
|
try:
|
||||||
print(" ")
|
if future.result():
|
||||||
else:
|
successful['monochrome'] += 1
|
||||||
print(" ")
|
print(f"Converted {os.path.basename(file_path)}")
|
||||||
|
else:
|
||||||
|
print(f"Failed to convert {os.path.basename(file_path)}")
|
||||||
|
except Exception as exc:
|
||||||
|
print(f"{os.path.basename(file_path)} generated an exception: {exc}")
|
||||||
|
|
||||||
# Step 2: Always trim whitespace
|
# Step 2: Always trim whitespace
|
||||||
print("\nTrimming whitespace from images...")
|
print("\nTrimming whitespace from images...")
|
||||||
for i, file_path in enumerate(temp_files, 1):
|
with concurrent.futures.ThreadPoolExecutor() as executor:
|
||||||
print(f"[{i}/{total_files}] Processing {os.path.basename(file_path)}...", end='', flush=True)
|
futures = {executor.submit(trim_whitespace, file_path, (opt_level == OptimizationLevel.TRIM)): file_path for file_path in temp_files}
|
||||||
# Only optimize if this is the final step (level 1)
|
for future in concurrent.futures.as_completed(futures):
|
||||||
if trim_whitespace(file_path, is_final=(opt_level == OptimizationLevel.TRIM)):
|
file_path = futures[future]
|
||||||
successful['trim'] += 1
|
try:
|
||||||
print(" ")
|
if future.result():
|
||||||
else:
|
successful['trim'] += 1
|
||||||
print(" ")
|
print(f"Trimmed {os.path.basename(file_path)}")
|
||||||
|
else:
|
||||||
|
print(f"Failed to trim {os.path.basename(file_path)}")
|
||||||
|
except Exception as exc:
|
||||||
|
print(f"{os.path.basename(file_path)} generated an exception: {exc}")
|
||||||
|
|
||||||
# Step 3: Run optipng if level = 3
|
# Step 3: Run optipng if level = 3
|
||||||
if opt_level == OptimizationLevel.FULL:
|
if opt_level == OptimizationLevel.FULL:
|
||||||
if check_optipng_installed():
|
if check_optipng_installed():
|
||||||
print("\nOptimizing PNG files with optipng...")
|
print("\nOptimizing PNG files with optipng...")
|
||||||
for i, file_path in enumerate(temp_files, 1):
|
with concurrent.futures.ThreadPoolExecutor() as executor:
|
||||||
print(f"[{i}/{total_files}] Optimizing {os.path.basename(file_path)}...", end='', flush=True)
|
futures = {executor.submit(run_optipng, file_path): file_path for file_path in temp_files}
|
||||||
if run_optipng(file_path):
|
for future in concurrent.futures.as_completed(futures):
|
||||||
successful['optipng'] += 1
|
file_path = futures[future]
|
||||||
print(" ")
|
try:
|
||||||
else:
|
if future.result():
|
||||||
print(" ")
|
successful['optipng'] += 1
|
||||||
|
print(f"Optimized {os.path.basename(file_path)}")
|
||||||
|
else:
|
||||||
|
print(f"Failed to optimize {os.path.basename(file_path)}")
|
||||||
|
except Exception as exc:
|
||||||
|
print(f"{os.path.basename(file_path)} generated an exception: {exc}")
|
||||||
else:
|
else:
|
||||||
print("\nNote: optipng not found. Skipping PNG optimization.")
|
print("\nNote: optipng not found. Skipping PNG optimization.")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user