1
0
Fork 0
mirror of https://github.com/prise6/smart-iss-posts synced 2024-05-01 13:22:43 +02:00

Ajout du script resize

This commit is contained in:
François Vieille 2018-08-06 01:46:09 +02:00
parent c54226eca5
commit ded9753af9
3 changed files with 53 additions and 3 deletions

View file

@ -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

View file

@ -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()

View file

@ -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'