| import os
|
| from shutil import move
|
| from PIL import Image, UnidentifiedImageError
|
|
|
| def is_image_corrupt(filepath):
|
| try:
|
| with Image.open(filepath) as img:
|
| img.verify()
|
| with Image.open(filepath) as img:
|
| img.load()
|
| except (IOError, SyntaxError, UnidentifiedImageError, ValueError, AttributeError) as e:
|
| return True, f'File is likely corrupt: {e}'
|
| return False, 'File appears to be OK.'
|
|
|
| def check_directory_for_corrupt_images(target_directories):
|
| for target_directory in target_directories:
|
| output_directory_suffix = "corrupted"
|
|
|
| for root, dirs, files in os.walk(target_directory):
|
|
|
| if root.endswith(output_directory_suffix):
|
| continue
|
|
|
| for file in files:
|
| if file.lower().endswith(('.png', '.jpg', '.jpeg', '.dds')):
|
| filepath = os.path.join(root, file)
|
| is_corrupt, message = is_image_corrupt(filepath)
|
| if is_corrupt:
|
|
|
| relative_path = os.path.relpath(root, target_directory)
|
| output_directory = os.path.join(target_directory, output_directory_suffix, relative_path)
|
| if not os.path.exists(output_directory):
|
| os.makedirs(output_directory)
|
|
|
| output_path = os.path.join(output_directory, file)
|
| move(filepath, output_path)
|
| print(f"Corrupt file found and moved: {filepath} - {message}")
|
|
|
|
|
|
|
|
|
| current_directory = os.getcwd()
|
| target_directories = [
|
| os.path.join(current_directory, "ARCHIVE")
|
| ]
|
|
|
| check_directory_for_corrupt_images(target_directories)
|
|
|
| input("Press Enter to exit...")
|
|
|
|
|
|
|