Created
June 2, 2015 18:57
-
-
Save magsol/b1126a79628adf3b3c11 to your computer and use it in GitHub Desktop.
FERET binary to PNG image parser
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 argparse | |
import bz2 | |
import os | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description = 'FERET Parser', | |
epilog = 'lol moar p1cz', add_help = 'How to use', | |
prog = 'python feret.py <args>') | |
# Required parameters. | |
parser.add_argument("-i", "--input", required = True, | |
help = "Path to the top-level directory of images. Search is recursive.") | |
# Default parameters. | |
parser.add_argument("-o", "--output", default = None, | |
help = "Path to an output directory where .png images will be written. \ | |
If None, script is set for search only. [DEFAULT: None]") | |
args = vars(parser.parse_args()) | |
# Start walking. | |
images = 0 | |
for dirname, dirnames, filenames in os.walk(args['input']): | |
# Are there any .bz2 files here? | |
for f in filenames: | |
if '.bz2' in f: | |
images += 1 | |
if args['output'] is not None: | |
# Decompress it, write it as an image. | |
handle = bz2.BZ2File(os.path.join(dirname, f), "rb") | |
raw = None | |
try: | |
raw = handle.read() | |
except Exception as e: | |
print e | |
quit() | |
handle.close() | |
name, ext, _ = f.split(".") | |
towrite = open("%s%s.%s" % (args['output'], name, ext), "wb") | |
towrite.write(raw) | |
towrite.close() | |
print 'Found %s images under parent directory "%s".' % (images, args['input']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment