diff --git a/Makefile b/Makefile index 10160e8..de178c8 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,11 @@ data: requirements sync_collections: $(PYTHON_INTERPRETER) src/data/sync_collections.py +## Resize collection +resize_collections: + $(PYTHON_INTERPRETER) src/data/resize_collections.py + + ## Delete all compiled Python files clean: find . -type f -name "*.py[co]" -delete diff --git a/src/data/resize_collections.py b/src/data/resize_collections.py new file mode 100644 index 0000000..e6acca8 --- /dev/null +++ b/src/data/resize_collections.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +import click +import logging +import os +import sys +from pathlib import Path +from dotenv import find_dotenv, load_dotenv +from PIL import Image + +load_dotenv(find_dotenv()) + +@click.command() +@click.argument('RESIZE_WIDTH', type=int, default=os.getenv('RESIZE_WIDTH')) +@click.argument('RESIZE_HEIGHT', type=int, default=os.getenv('RESIZE_HEIGHT')) +def main(resize_width, resize_height): + """ Resize image + """ + + logger.info('Resize collections to {}x{}'.format(resize_width, resize_height)) + + try: + imgs_path = os.path.join(str(project_dir), 'data', 'external', 'collections') + [resize_one_img(os.path.join(imgs_path, img_path), resize_width, resize_height) for img_path in os.listdir(imgs_path)] + except: + logger.error(sys.exc_info()[0]) + exit() + + +def resize_one_img(img_path, resize_width, resize_height): + + logger.info('Resize {}'.format(img_path)) + size = (resize_width, resize_height) + outfile = os.path.join(str(project_dir), 'data', 'interim', 'collections', os.path.basename(img_path)) + try: + im = Image.open(os.path.join(str(project_dir), img_path)) + im.thumbnail(size) + im.save(outfile, "JPEG") + except IOError: + logger.info('Cannot resize {}'.format(img_path)) + +if __name__ == '__main__': + log_fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + logging.basicConfig(level=logging.INFO, format=log_fmt) + + project_dir = Path(__file__).resolve().parents[2] + logger = logging.getLogger(__name__) + + main() diff --git a/src/data/sync_collections.py b/src/data/sync_collections.py index 9ad381f..e0a7156 100644 --- a/src/data/sync_collections.py +++ b/src/data/sync_collections.py @@ -25,7 +25,6 @@ def main(r_collections_project): logger.error(sys.exc_info()[0]) exit() - return(1) def get_unique_imgs(r_collections_project): @@ -58,8 +57,6 @@ def cp_imgs(r_collections_project, imgs): os.path.join(str(project_dir), "data", "external", "collections", img) ) - return(1) - if __name__ == '__main__': log_fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'