Skip to content

Instantly share code, notes, and snippets.

@Yankim
Created February 25, 2019 23:24
Show Gist options
  • Save Yankim/b494f2442f4b9c0ed5c0bd9d09a99ec4 to your computer and use it in GitHub Desktop.
Save Yankim/b494f2442f4b9c0ed5c0bd9d09a99ec4 to your computer and use it in GitHub Desktop.
clean up corrupt images
import os
from PIL import Image
import pathlib
PATH_ = pathlib.Path(__file__).parent
broken_images=[]
def check_images(PATH_):
for pic_class in os.listdir(PATH_):
for pic in os.listdir(f'{PATH_}/{pic_class}'):
try:
img = Image.open(f'{PATH_}/{pic_class}/{pic}')
img.verify()
except (IOError, SyntaxError) as e:
print('Bad file:', f'{PATH_}/{pic_class}/{pic}')
broken_images.append(f'{PATH_}/{pic_class}/{pic}')
return broken_images
img_to_del = check_images(f'{PATH_}/valid')
[os.remove(pic) for pic in img_to_del]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment