3 #Identify false image files, for example a file that has a gif header and a php body
4 #author: stack@inventati.org
12 #Log wrong image files found
16 #Echo a message if verbose is enabled
21 #Check if a given file is an image, trying not to be fooled
22 def checkfimage(fpath):
23 #imghdr is fooled with false images, so use it to check if file would be a valid image file
24 if imghdr.what(fpath):
26 #PIL Image is not fooled
32 #Generate the file paths to traverse, or a single path if a file name was given
34 if os.path.isdir(sys.argv[1]):
35 for root, dirs, files in os.walk(sys.argv[1]):
37 yield os.path.join(root, name)
41 if __name__ == "__main__":
43 sys.exit('Usage: %s path logfile [-v]' % sys.argv[0])
45 if not os.path.exists(sys.argv[1]):
46 sys.exit('ERROR: path %s was not found!' % sys.argv[1])
49 fdlog = open(sys.argv[2],"w")
51 sys.exit('ERROR: unable to open logfile' % sys.argv[2])
53 debug = 1 if len(sys.argv) == 4 and sys.argv[3] == "-v" else None
56 for fpath in getfiles(sys.argv[1]):
57 verbose("Checking: " + fpath)
59 if not checkfimage(fpath):
61 verbose("ERR not an image: " + fpath)
63 print ("Checked %d files."% nfiles)