同步完整源码 - 2026-05-25

This commit is contained in:
xiaoxue
2026-05-25 00:32:43 +08:00
commit 77248c0d06
46 changed files with 1806 additions and 0 deletions
@@ -0,0 +1 @@
__all_ = ["files", "hashing", "images"]
+27
View File
@@ -0,0 +1,27 @@
import shutil
import zipfile
from pathlib import Path
def ensure_dir(path: Path) -> None:
path.mkdir(parents=True, exist_ok=True)
def clean_dir(path: Path) -> None:
# Eliminar archivos de la carpeta esfecifica
if path.exists(): shutil.rmtree(path, ignore_errors=True)
def create_zip_from_folder(folder: Path, zip_destine: Path) -> None:
# Crear un zip con el contenido de folder
with zipfile.ZipFile(zip_destine, 'w', compression=zipfile.ZIP_DEFLATED) as zf:
for file in folder.rglob("*"):
if file.is_file(): zf.write(file, arcname=file.relative_to(folder))
def is_small_kb(path: Path, min_kb: int) -> bool:
# Validacion booleana si la imagen es mas chica que el parametro min_kb configurado
return (path.stat().st_size / 1024) < min_kb
def copy_folder(src: Path, dest: Path) -> None:
# Copia una carpeta al destino, si el destino existe lo remplaza
if dest.exists(): shutil.rmtree(dest, ignore_errors=True)
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(src, dest)
@@ -0,0 +1,4 @@
import hashlib
def md5_bytes(data: bytes) -> str:
return hashlib.md5(data).hexdigest()
+32
View File
@@ -0,0 +1,32 @@
from pathlib import Path
from typing import Optional, Tuple
from PIL import Image
def get_image_size(path: Path) -> Optional[Tuple[int, int]]:
# Devolver el ancho y el alto de las imagenes, En caso de no poder abrir regresa None
try:
with Image.open(path) as im:
return im.size
except Exception:
return None
def fails_dimension_filter(dims: Optional[Tuple[int, int]], min_w: int, min_h: int) -> bool:
# En caso de que dims sea None se omiten validaciones
if min_w <= 0 and min_h <= 0: return False
if dims is None: return False
w, h = dims
if min_w > 0 and w < min_w: return True
if min_h > 0 and h < min_h: return True
return False
def normalize_ext(ext: str) -> str:
# Normalizacion de extenciones para imagenes
ext = (ext or "bin").lower().strip(".")
if ext in ("jpeg", "jpe"):
return "jpg"
if ext == "tif":
return "tiff"
return ext