Skip to content

Instantly share code, notes, and snippets.

@mariomartinezsz
Created September 1, 2011 02:05
Show Gist options
  • Save mariomartinezsz/1185254 to your computer and use it in GitHub Desktop.
Save mariomartinezsz/1185254 to your computer and use it in GitHub Desktop.
Python script that finds and deletes image files by extension and size
# 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