Revision e2a82fa9
Added by David Sorber almost 10 years ago
| software/lepton_wrapper/lepton_wrapper.py | ||
|---|---|---|
|
#!/opt/local/bin/python3
|
||
|
|
||
|
#!/usr/bin/python3
|
||
|
|
||
|
import argparse
|
||
| ... | ... | |
|
import subprocess
|
||
|
import sys
|
||
|
|
||
|
LEPTON_PATH = '/home/dsorber/devasdf/lepton/lepton'
|
||
|
LEPTON_PATH = '/Users/dsorber/Documents/lepton/lepton'
|
||
|
|
||
|
# Bash text formatting codes
|
||
|
BOLD = '\033[1m'
|
||
| ... | ... | |
|
YELLOW = '\033[33m'
|
||
|
PURPLE = '\033[35m'
|
||
|
|
||
|
def image_walker(top):
|
||
|
def file_walker(top_path, regex):
|
||
|
|
||
|
images = []
|
||
|
files = []
|
||
|
|
||
|
for dirpath, dirnames, filenames in os.walk(top):
|
||
|
for dirpath, dirnames, filenames in os.walk(top_path):
|
||
|
for filename in filenames:
|
||
|
fullpath = os.path.join(dirpath, filename)
|
||
|
if re.search('(\.jpg$)|(\.jpeg$)', fullpath, re.IGNORECASE):
|
||
|
if re.search(regex, fullpath, re.IGNORECASE):
|
||
|
#~ print(fullpath)
|
||
|
images.append(fullpath)
|
||
|
files.append(fullpath)
|
||
|
for dirname in dirnames:
|
||
|
image_walker(os.path.join(dirpath, dirname))
|
||
|
return images
|
||
|
|
||
|
def main():
|
||
|
file_walker(os.path.join(dirpath, dirname), regex)
|
||
|
return files
|
||
|
|
||
|
if len(sys.argv) < 2:
|
||
|
print('{:s}{:s}ERROR:{:s} you must supply a path'.format(BOLD, RED, ENDC))
|
||
|
return -1
|
||
|
def do_compress(top_path, remove_orig):
|
||
|
|
||
|
# Make sure the specified path to lepton is correct
|
||
|
if not (os.path.isfile(LEPTON_PATH) and os.access(LEPTON_PATH, os.X_OK)):
|
||
|
print('{:s}{:s}ERROR:{:s} unable to find lepton executable'
|
||
|
.format(BOLD, RED, ENDC))
|
||
|
return -1
|
||
|
|
||
|
#~ parser = argparse.ArgumentParser(description='Ryft Package Build Script')
|
||
|
|
||
|
#~ parser.add_argument('-ni', '--non-interactive', default=False,
|
||
|
#~ action='store_true',
|
||
|
#~ help='non-interactive mode; do not prompt user for input')
|
||
|
#~ parser.add_argument('-pn', '--package', default=None,
|
||
|
#~ type=validate_package_name,
|
||
|
#~ help='the name of the package to build')
|
||
|
#~ parser.add_argument('-pv', '--pkg-version', default=None,
|
||
|
#~ type=validate_version_argparse,
|
||
|
#~ help='the new version of the package')
|
||
|
|
||
|
images = image_walker(sys.argv[1])
|
||
|
# Find all *.jpg (or *.jpeg) images (ignoring case)
|
||
|
images = file_walker(sys.argv[1], '(\.jpg$)|(\.jpeg$)')
|
||
|
num_images = len(images)
|
||
|
|
||
|
# Counters to keep track of total compression ratio
|
||
|
total_uncompressed_bytes = 0
|
||
|
total_compressed_bytes = 0
|
||
|
|
||
|
print('Found {:d} *.jpg images'.format(num_images))
|
||
|
|
||
|
# Iterate over each image and compress it using the lepton binary
|
||
|
for idx, image in enumerate(images):
|
||
|
print('Image {:d} of {:d}:'.format(idx + 1, num_images))
|
||
|
print('\t{:s}'.format(image))
|
||
|
|
||
|
compressed_image = '{:s}.lep'.format(image)
|
||
|
cmd = '{:s} "{:s}" "{:s}"'.format(LEPTON_PATH, image, compressed_image)
|
||
|
rc = subprocess.run(cmd, shell=True)
|
||
|
if rc.returncode == 0:
|
||
|
#~ rc = subprocess.run(cmd, shell=True)
|
||
|
#~ if rc.returncode == 0:
|
||
|
rc = subprocess.call(cmd, shell=True)
|
||
|
if rc == 0:
|
||
|
print('{:s}SUCCESS{:s}'.format(BOLD, ENDC))
|
||
|
total_uncompressed_bytes += os.path.getsize(image)
|
||
|
total_compressed_bytes += os.path.getsize(compressed_image)
|
||
|
|
||
|
# Only remove original file if specified
|
||
|
if remove_orig:
|
||
|
os.remove(image)
|
||
|
else:
|
||
|
print('{:s}{:s}ERROR{:s}'.format(BOLD, RED, ENDC))
|
||
|
print('\n')
|
||
|
|
||
|
|
||
|
# Calculate and print total compression ratio
|
||
|
percent = (float(total_compressed_bytes) / total_uncompressed_bytes) * 100
|
||
|
print('Total compression is: {:4.2f}%'.format(percent))
|
||
|
|
||
|
def do_decompress(top_path, remove_orig):
|
||
|
|
||
|
# Find all *.lep files (ignoring case)
|
||
|
images = file_walker(sys.argv[1], '\.lep$')
|
||
|
num_images = len(images)
|
||
|
|
||
|
print('Found {:d} *.lep files'.format(num_images))
|
||
|
|
||
|
# Iterate over each image and decompress it using the lepton binary
|
||
|
for idx, image in enumerate(images):
|
||
|
print('Image {:d} of {:d}:'.format(idx + 1, num_images))
|
||
|
print('\t{:s}'.format(image))
|
||
|
|
||
|
cmd = '{:s} "{:s}" "{:s}"'.format(LEPTON_PATH, image, image[:-4])
|
||
|
#~ rc = subprocess.run(cmd, shell=True)
|
||
|
#~ if rc.returncode == 0:
|
||
|
rc = subprocess.call(cmd, shell=True)
|
||
|
if rc == 0:
|
||
|
print('{:s}SUCCESS{:s}'.format(BOLD, ENDC))
|
||
|
|
||
|
# Only remove original file if specified
|
||
|
if remove_orig:
|
||
|
os.remove(image)
|
||
|
else:
|
||
|
print('{:s}{:s}ERROR{:s}'.format(BOLD, RED, ENDC))
|
||
|
print('\n')
|
||
|
|
||
|
def main():
|
||
|
|
||
|
# Make sure the specified path to lepton is correct
|
||
|
if not (os.path.isfile(LEPTON_PATH) and os.access(LEPTON_PATH, os.X_OK)):
|
||
|
print('{:s}{:s}ERROR:{:s} unable to find lepton executable'
|
||
|
.format(BOLD, RED, ENDC))
|
||
|
return -1
|
||
|
|
||
|
# Parse command line arguments
|
||
|
parser = argparse.ArgumentParser(description='Lepton Wrapper')
|
||
|
|
||
|
parser.add_argument('path',
|
||
|
help='path in which to look for images')
|
||
|
|
||
|
parser.add_argument('-d', '--decompress', action='store_true',
|
||
|
default=False, help='decompress *.lep files')
|
||
|
|
||
|
parser.add_argument('-r', '--remove', action='store_true', default=False,
|
||
|
help='remove orignal files after compress/decompress')
|
||
|
|
||
|
args = parser.parse_args()
|
||
|
|
||
|
|
||
|
# Either compress or decompress depending on mode parameter
|
||
|
if args.decompress:
|
||
|
do_decompress(args.path, args.remove==True)
|
||
|
else:
|
||
|
do_compress(args.path, args.remove==True)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
Improvements to the lepton wrapper script, developed on Mac OS.