Created
November 22, 2018 08:43
-
-
Save dogukancagatay/2f74f397ceff2b4180fc90ec8f154315 to your computer and use it in GitHub Desktop.
Checks and separates corrupt images
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# pip install Pillow | |
import os | |
import shutil | |
from PIL import Image | |
IMAGES_PATH = './images' | |
CORRUPT_IMAGES_PATH = './corrupt_images' | |
EXTENSIONS = ('.jpg', '.png') | |
os.makedirs(CORRUPT_IMAGES_PATH, exist_ok=True) | |
for filename in os.listdir(IMAGES_PATH): | |
if filename.endswith(EXTENSIONS): | |
image_path = os.path.join(IMAGES_PATH, filename) | |
try: | |
img = Image.open(image_path) # open the image file | |
img.verify() # verify that it is, in fact an image | |
except (IOError, SyntaxError) as e: | |
print('Bad file:', filename) # print out the names of corrupt files | |
shutil.move(image_path, os.path.join(CORRUPT_IMAGES_PATH, filename)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment