Created
September 1, 2011 02:05
-
-
Save mariomartinezsz/1185254 to your computer and use it in GitHub Desktop.
Python script that finds and deletes image files by extension and size
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
# Deletes image files by size and extension | |
import os, glob, Image, sys | |
inipath = '/your/path/' | |
fileSize = (24,24) | |
fileExt = '.png' #.png .gif .jpg | |
def scandirs(path): | |
for currentFile in glob.glob( os.path.join(path, '*') ): | |
if os.path.isdir(currentFile): | |
scandirs(currentFile) | |
basename, extension = os.path.splitext(currentFile) | |
if extension == fileExt: | |
print 'I got an image: ' + currentFile | |
if imagework(currentFile): | |
os.remove(currentFile) | |
print 'Deleting: ' + currentFile | |
def imagework(thefile): | |
try: | |
im = Image.open(thefile) | |
print im.format, im.size, im.mode | |
if im.size == fileSize: | |
return True | |
else: | |
return False | |
except IOError: | |
print 'An IO error reading ' + thefile | |
return False | |
except: | |
print 'An error reading ' + thefile | |
return False | |
scandirs(inipath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment