You've already forked ImageCompressor
mirror of
https://github.com/Llloooggg/ImageCompressor.git
synced 2026-03-05 19:16:23 +03:00
Все записи переведены на относительные пути
This commit is contained in:
@@ -78,7 +78,7 @@ def extract_exif(path: Path):
|
|||||||
return img.info.get("exif")
|
return img.info.get("exif")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.warning(
|
logging.warning(
|
||||||
f"Не удалось извлечь EXIF из {path} {path.stat().st_size // 1024} KB): {e}"
|
f"Не удалось извлечь EXIF из {path.relative_to(input_dir)} {path.stat().st_size // 1024} KB): {e}"
|
||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ def inject_exif(path: Path, exif):
|
|||||||
img_converted.save(path, format=fmt, exif=exif)
|
img_converted.save(path, format=fmt, exif=exif)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.warning(
|
logging.warning(
|
||||||
f"Не удалось вставить EXIF в {path} {path.stat().st_size // 1024} KB): {e}"
|
f"Не удалось вставить EXIF в {path.relative_to(input_dir)} {path.stat().st_size // 1024} KB): {e}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -121,7 +121,7 @@ def convert_png_to_jpeg(path: Path) -> Optional[Path]:
|
|||||||
return tmp_path
|
return tmp_path
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.warning(
|
logging.warning(
|
||||||
f"Ошибка при конвертации PNG в JPEG: {path} ({path.stat().st_size // 1024} KB): {e}"
|
f"Ошибка при конвертации PNG в JPEG: {path.relative_to(input_dir)} ({path.stat().st_size // 1024} KB): {e}"
|
||||||
)
|
)
|
||||||
if tmp_path.exists():
|
if tmp_path.exists():
|
||||||
tmp_path.unlink()
|
tmp_path.unlink()
|
||||||
@@ -142,7 +142,7 @@ def compress_with_external(
|
|||||||
return False, path
|
return False, path
|
||||||
converted_size = converted.stat().st_size
|
converted_size = converted.stat().st_size
|
||||||
logging.warning(
|
logging.warning(
|
||||||
f"Сконвертирован PNG в JPEG: {path} ({original_size // 1024} KB) -> {converted} ({converted_size // 1024} KB)"
|
f"Сконвертирован PNG в JPEG: {path.relative_to(input_dir)} ({original_size // 1024} KB) -> {converted.relative_to(input_dir)} ({converted_size // 1024} KB)"
|
||||||
)
|
)
|
||||||
if converted_size <= TARGET_SIZE:
|
if converted_size <= TARGET_SIZE:
|
||||||
return True, converted
|
return True, converted
|
||||||
@@ -175,7 +175,7 @@ def compress_with_external(
|
|||||||
]
|
]
|
||||||
else:
|
else:
|
||||||
logging.warning(
|
logging.warning(
|
||||||
f"Неподдерживаемый формат {path} ({original_size // 1024} KB)"
|
f"Неподдерживаемый формат {path.relative_to(input_dir)} ({original_size // 1024} KB)"
|
||||||
)
|
)
|
||||||
return False, path
|
return False, path
|
||||||
|
|
||||||
@@ -195,7 +195,7 @@ def compress_with_external(
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.warning(
|
logging.warning(
|
||||||
f"Ошибка при сжатии внешней утилитой {path} ({original_size // 1024} KB): {e}"
|
f"Ошибка при сжатии внешней утилитой {path.relative_to(input_dir)} ({original_size // 1024} KB): {e}"
|
||||||
)
|
)
|
||||||
if tmp_path.exists():
|
if tmp_path.exists():
|
||||||
tmp_path.unlink()
|
tmp_path.unlink()
|
||||||
@@ -209,7 +209,7 @@ def compress_with_external(
|
|||||||
return True, path
|
return True, path
|
||||||
else:
|
else:
|
||||||
logging.warning(
|
logging.warning(
|
||||||
f"Не удалось сжать внешней утилитой (не уменьшилось): {path} ({original_size // 1024} KB)"
|
f"Не удалось сжать внешней утилитой (не уменьшилось): {path.relative_to(input_dir)} ({original_size // 1024} KB)"
|
||||||
)
|
)
|
||||||
tmp_path.unlink()
|
tmp_path.unlink()
|
||||||
|
|
||||||
@@ -251,7 +251,7 @@ def compress_with_pillow(path: Path) -> Tuple[bool, Path]:
|
|||||||
tmp_path.replace(path)
|
tmp_path.replace(path)
|
||||||
return True, path
|
return True, path
|
||||||
logging.warning(
|
logging.warning(
|
||||||
f"Не удалось сжать Pillow (не уменьшилось): {path} ({original_size // 1024} KB)"
|
f"Не удалось сжать Pillow (не уменьшилось): {path.relative_to(input_dir)} ({original_size // 1024} KB)"
|
||||||
)
|
)
|
||||||
tmp_path.unlink()
|
tmp_path.unlink()
|
||||||
return False, path
|
return False, path
|
||||||
@@ -269,14 +269,14 @@ def compress_image(path: Path):
|
|||||||
|
|
||||||
if original_size < MIN_SIZE:
|
if original_size < MIN_SIZE:
|
||||||
logging.info(
|
logging.info(
|
||||||
f"Пропущено (малый размер): {path} ({original_size // 1024} KB)"
|
f"Пропущено (малый размер): {path.relative_to(input_dir)} ({original_size // 1024} KB)"
|
||||||
)
|
)
|
||||||
processed_hashes.add(h)
|
processed_hashes.add(h)
|
||||||
skipped_size_count += 1
|
skipped_size_count += 1
|
||||||
total_images_new_size += original_size
|
total_images_new_size += original_size
|
||||||
return
|
return
|
||||||
|
|
||||||
file_path_str = str(path)
|
file_path_str = str(path.relative_to(input_dir))
|
||||||
|
|
||||||
with db_lock:
|
with db_lock:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
@@ -287,17 +287,17 @@ def compress_image(path: Path):
|
|||||||
existing_paths = set(row[0].split("|"))
|
existing_paths = set(row[0].split("|"))
|
||||||
if file_path_str in existing_paths:
|
if file_path_str in existing_paths:
|
||||||
logging.info(
|
logging.info(
|
||||||
f"Пропущено (уже обработано): {path} ({original_size // 1024} KB)"
|
f"Пропущено (уже обработано): {file_path_str} ({original_size // 1024} KB)"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
existing_paths.add(file_path_str)
|
existing_paths.add(file_path_str)
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"UPDATE processed_images SET filename = ? WHERE hash = ?",
|
"UPDATE processed_images SET filename = ? WHERE hash = ?",
|
||||||
("|".join(existing_paths), h),
|
("|".join(sorted(existing_paths)), h),
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
logging.info(
|
logging.info(
|
||||||
f"Пропущено (дубликат хэша, другой путь): {path} ({original_size // 1024} KB)"
|
f"Пропущено (дубликат хэша, другой путь): {file_path_str} ({original_size // 1024} KB)"
|
||||||
)
|
)
|
||||||
processed_hashes.add(h)
|
processed_hashes.add(h)
|
||||||
skipped_count += 1
|
skipped_count += 1
|
||||||
@@ -318,7 +318,7 @@ def compress_image(path: Path):
|
|||||||
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.relative_to(input_dir)} ({original_size // 1024} KB -> {new_size // 1024} KB, {percent:.2f}%)"
|
||||||
)
|
)
|
||||||
|
|
||||||
with db_lock:
|
with db_lock:
|
||||||
@@ -329,7 +329,7 @@ def compress_image(path: Path):
|
|||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
if row:
|
if row:
|
||||||
paths = set(row[0].split("|"))
|
paths = set(row[0].split("|"))
|
||||||
paths.add(str(final_path))
|
paths.add(str(final_path.relative_to(input_dir)))
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"UPDATE processed_images SET filename = ? WHERE hash = ?",
|
"UPDATE processed_images SET filename = ? WHERE hash = ?",
|
||||||
("|".join(sorted(paths)), new_hash),
|
("|".join(sorted(paths)), new_hash),
|
||||||
@@ -337,7 +337,7 @@ def compress_image(path: Path):
|
|||||||
else:
|
else:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"INSERT INTO processed_images(hash, filename) VALUES(?, ?)",
|
"INSERT INTO processed_images(hash, filename) VALUES(?, ?)",
|
||||||
(new_hash, str(final_path)),
|
(new_hash, str(final_path.relative_to(input_dir))),
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
@@ -346,7 +346,7 @@ def compress_image(path: Path):
|
|||||||
total_saved_bytes += saved
|
total_saved_bytes += saved
|
||||||
else:
|
else:
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Не удалось сжать: {path} ({original_size // 1024} KB)"
|
f"Не удалось сжать: {path.relative_to(input_dir)} ({original_size // 1024} KB)"
|
||||||
)
|
)
|
||||||
processed_hashes.add(h)
|
processed_hashes.add(h)
|
||||||
error_count += 1
|
error_count += 1
|
||||||
@@ -354,7 +354,7 @@ def compress_image(path: Path):
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(
|
logging.error(
|
||||||
f"Ошибка при обработке {path} ({original_size // 1024} KB): {e}"
|
f"Ошибка при обработке {path.relative_to(input_dir)} ({original_size // 1024} KB): {e}"
|
||||||
)
|
)
|
||||||
error_count += 1
|
error_count += 1
|
||||||
total_images_new_size += original_size
|
total_images_new_size += original_size
|
||||||
@@ -386,6 +386,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="Сжатие изображений до заданного размера"
|
||||||
)
|
)
|
||||||
@@ -431,6 +433,7 @@ def main():
|
|||||||
|
|
||||||
total_new_size = get_folder_size(input_dir)
|
total_new_size = get_folder_size(input_dir)
|
||||||
|
|
||||||
|
print("\n\nОчистка БД...")
|
||||||
with db_lock:
|
with db_lock:
|
||||||
cursor.execute("SELECT hash, filename FROM processed_images")
|
cursor.execute("SELECT hash, filename FROM processed_images")
|
||||||
all_records = cursor.fetchall()
|
all_records = cursor.fetchall()
|
||||||
@@ -442,15 +445,15 @@ def main():
|
|||||||
db_file_list = [
|
db_file_list = [
|
||||||
f.strip() for f in filenames.split("|") if f.strip()
|
f.strip() for f in filenames.split("|") if f.strip()
|
||||||
]
|
]
|
||||||
real_file_list = [
|
real_files_list = []
|
||||||
f
|
for file in db_file_list:
|
||||||
for f in db_file_list
|
full_path = input_dir / file
|
||||||
if Path(f).exists() and file_hash(Path(f)) == h
|
if full_path.exists() and file_hash(full_path) == h:
|
||||||
]
|
real_files_list.append(file)
|
||||||
reasone = None
|
reasone = None
|
||||||
if h in stale_hashes:
|
if h in stale_hashes:
|
||||||
reasone = "не встречался хэш"
|
reasone = "не встречался хэш"
|
||||||
if not real_file_list:
|
if not real_files_list:
|
||||||
reasone = "файлы не сущестувуют"
|
reasone = "файлы не сущестувуют"
|
||||||
if reasone:
|
if reasone:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
@@ -463,11 +466,11 @@ def main():
|
|||||||
else:
|
else:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"UPDATE processed_images SET filename = ? WHERE hash = ?",
|
"UPDATE processed_images SET filename = ? WHERE hash = ?",
|
||||||
("|".join(sorted(set(real_file_list))), h),
|
("|".join(set(real_files_list)), h),
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
print(f"\n\nУдалено записей в БД: {deleted_count}")
|
print(f"Удалено записей в БД: {deleted_count}")
|
||||||
logging.info(f"Удалено записей в БД: {deleted_count}")
|
logging.info(f"Удалено записей в БД: {deleted_count}")
|
||||||
|
|
||||||
print("\nГотово.")
|
print("\nГотово.")
|
||||||
|
|||||||
Reference in New Issue
Block a user