You've already forked ImageCompressor
mirror of
https://github.com/Llloooggg/ImageCompressor.git
synced 2026-03-06 03:26:23 +03:00
Исправление "ненахождения" файла
This commit is contained in:
@@ -37,6 +37,7 @@ processed_count = 0
|
|||||||
skipped_count = 0
|
skipped_count = 0
|
||||||
total_saved_bytes = 0
|
total_saved_bytes = 0
|
||||||
db_lock = threading.Lock()
|
db_lock = threading.Lock()
|
||||||
|
input_dir: Path = None
|
||||||
|
|
||||||
# --- База данных ---
|
# --- База данных ---
|
||||||
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
||||||
@@ -205,10 +206,16 @@ def compress_image(path: Path, use_fallback: bool = False):
|
|||||||
global processed_count, skipped_count, total_saved_bytes
|
global processed_count, skipped_count, total_saved_bytes
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not path.exists() or path.stat().st_size < MIN_SIZE:
|
if not path.exists():
|
||||||
skipped_count += 1
|
skipped_count += 1
|
||||||
logging.info(
|
logging.info(
|
||||||
f"Пропущено (малый размер или не найден): {path} ({path.stat().st_size // 1024}KB)"
|
f"Пропущено (не найден): {path} ({path.stat().st_size // 1024}KB)"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
if not path.exists():
|
||||||
|
skipped_count += 1
|
||||||
|
logging.info(
|
||||||
|
f"Пропущено (малый размер): {path} ({path.stat().st_size // 1024}KB)"
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -232,10 +239,13 @@ def compress_image(path: Path, use_fallback: bool = False):
|
|||||||
result, final_path = compress_with_pillow(path)
|
result, final_path = compress_with_pillow(path)
|
||||||
|
|
||||||
if not final_path.exists():
|
if not final_path.exists():
|
||||||
logging.warning(f"Файл не найден после сжатия: {final_path}")
|
logging.warning(
|
||||||
|
f"Файл не найден после сжатия: {final_path} ({original_size // 1024}KB)"
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
new_size = final_path.stat().st_size
|
new_size = final_path.stat().st_size
|
||||||
|
rel_path = final_path.relative_to(input_dir)
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
if new_size < original_size:
|
if new_size < original_size:
|
||||||
@@ -243,7 +253,7 @@ def compress_image(path: Path, use_fallback: bool = False):
|
|||||||
total_saved_bytes += saved
|
total_saved_bytes += saved
|
||||||
percent = (1 - new_size / original_size) * 100
|
percent = (1 - new_size / original_size) * 100
|
||||||
logging.info(
|
logging.info(
|
||||||
f"Сжато: {path} ({original_size//1024}KB -> {new_size//1024}KB, -{percent:.2f}%)"
|
f"Сжато: {path} ({original_size//1024}KB -> {new_size//1024}KB, {percent:.2f}%)"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
logging.info(
|
logging.info(
|
||||||
@@ -254,13 +264,15 @@ def compress_image(path: Path, use_fallback: bool = False):
|
|||||||
with db_lock:
|
with db_lock:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"INSERT INTO processed_images(hash, filename, reduced) VALUES(?, ?, ?)",
|
"INSERT INTO processed_images(hash, filename, reduced) VALUES(?, ?, ?)",
|
||||||
(h, final_path.name, new_size < original_size),
|
(h, str(rel_path), new_size < original_size),
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
processed_count += 1
|
processed_count += 1
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Ошибка при обработке {path}: {e}")
|
logging.error(
|
||||||
|
f"Ошибка при обработке {path} ({original_size // 1024}KB): {e}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# --- Основной процесс ---
|
# --- Основной процесс ---
|
||||||
@@ -274,15 +286,15 @@ def find_images(root: Path):
|
|||||||
yield Path(dirpath) / name
|
yield Path(dirpath) / name
|
||||||
|
|
||||||
|
|
||||||
def prepare_and_copy_files(input_dir: Path, output_dir: Path) -> list[Path]:
|
def prepare_and_copy_files(input_dir_: Path, output_dir: Path) -> list[Path]:
|
||||||
if input_dir.resolve() == output_dir.resolve():
|
if input_dir_.resolve() == output_dir.resolve():
|
||||||
return list(find_images(input_dir))
|
return list(find_images(input_dir_))
|
||||||
|
|
||||||
output_dir.mkdir(parents=True, exist_ok=True)
|
output_dir.mkdir(parents=True, exist_ok=True)
|
||||||
copied = []
|
copied = []
|
||||||
|
|
||||||
for image in find_images(input_dir):
|
for image in find_images(input_dir_):
|
||||||
rel_path = image.relative_to(input_dir)
|
rel_path = image.relative_to(input_dir_)
|
||||||
dest = output_dir / rel_path
|
dest = output_dir / rel_path
|
||||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||||
dest.write_bytes(image.read_bytes())
|
dest.write_bytes(image.read_bytes())
|
||||||
@@ -292,6 +304,8 @@ def prepare_and_copy_files(input_dir: Path, output_dir: Path) -> list[Path]:
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
global input_dir
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Сжатие изображений до заданного размера"
|
description="Сжатие изображений до заданного размера"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user