Created
February 25, 2019 23:24
-
-
Save Yankim/b494f2442f4b9c0ed5c0bd9d09a99ec4 to your computer and use it in GitHub Desktop.
clean up corrupt images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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