Skip to content

Commit

Permalink
Adding logging, multi input folders
Browse files Browse the repository at this point in the history
  • Loading branch information
mtlynch committed Oct 4, 2017
1 parent cc6b7ef commit f7536e1
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions processor/processor.py
@@ -1,6 +1,7 @@
#!/usr/bin/python2

import argparse
import logging
import os

from PIL import Image
Expand All @@ -10,24 +11,44 @@ def dummy():
pass


logger = logging.getLogger(__name__)


def configure_logging():
root_logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(name)-15s %(levelname)-4s %(message)s',
'%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
root_logger.addHandler(handler)
root_logger.setLevel(logging.INFO)


def main(args):
configure_logging()
if not os.path.exists(args.output_root):
os.makedirs(args.output_root)
for recipe_key in os.listdir(args.input_root):
image_path = os.path.join(args.input_root, recipe_key, 'main.jpg')
if not os.path.exists(image_path):
continue
resized_path = os.path.join(args.output_root,
recipe_key + '_thumbnail.jpg')
image = Image.open(image_path)
image.thumbnail((340, 230))
image.convert('RGB').save(resized_path, "JPEG")
for root in args.input_root:
for recipe_key in os.listdir(root):
image_path = os.path.join(root, recipe_key, 'main.jpg')
logging.info('Processing %s', recipe_key)
if not os.path.exists(image_path):
logging.warning('No image file, skipping')
continue

resized_path = os.path.join(args.output_root,
recipe_key + '_thumbnail.jpg')
image = Image.open(image_path)
image.thumbnail((340, 230))
image.convert('RGB').save(resized_path, "JPEG")
logging.info('Saved resized image to %s', resized_path)


if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='KetoHub Thumbnail Processor',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-i', '--input_root')
parser.add_argument('-i', '--input_root', default=[], action='append')
parser.add_argument('-o', '--output_root')
main(parser.parse_args())

0 comments on commit f7536e1

Please sign in to comment.