Extract delete corrupt images

This commit is contained in:
Andras Schmelczer 2024-07-08 07:36:56 +01:00
commit 564134145a
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
3 changed files with 26 additions and 17 deletions

View file

@ -0,0 +1,26 @@
import logging
from pathlib import Path
from typing import List
from PIL import Image
import PIL.Image
from tqdm import tqdm
PIL.Image.MAX_IMAGE_PIXELS = None
def delete_corrupt_images(paths: List[Path]) -> None:
deleted_count = 0
for path in tqdm(paths):
if not path.exists():
logging.warning(f"{path} does not exist, skipping...")
continue
try:
Image.open(path)
except KeyboardInterrupt:
logging.info("Keyboard interrupt, exiting...")
raise
except:
logging.warning(f"Failed to open {path}, deleting...")
deleted_count += 1
path.unlink()
logging.info(f"Deleted {deleted_count} corrupt images")